没有域名的 Apache 虚拟主机

2022-08-30 18:28:51

我有一个安装了apache2的VPS,我想访问一些PHP项目,没有域名,只有IP地址。例如:

http://162.243.93.216/projecta/index.php
http://162.243.93.216/projectb/index.php

我还有其他带有域的项目,如 example.com,在我的目录/var/www/

/html/
   info.php
/projecta/
/projectb/
/example/

当我去

http://162.243.93.216/info.php then /var/www/html/info.php is opened. 

我的文件 000-default.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

答案 1
" http://162.243.93.216/info.php then /var/www/html/info.php is opened "

我假设这已经有效(如果没有,请取消注释下面conf中显示的行)ServerAlias

您现在想要映射

http://162.243.93.216/projecta/
/var/www/projectahttp://162.243.93.216/projectb//var/www/projectb

为此,您需要使用Apache Alias指令。

将您的文件更新为:000-default.conf

<VirtualHost *:80>
    # ServerAlias 162.243.93.216
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    Alias /projecta /var/www/projecta
    Alias /projectb /var/www/projectb

    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

答案 2

创建一个新的虚拟主机文件,并按如下方式进行设置:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerAlias 192.168.1.1 
        DocumentRoot /somewhere/public_html
        <Directory /somewhere/public_html/>
         Options Indexes FollowSymLinks MultiViews ExecCGI
         AllowOverride Authconfig FileInfo
         Require all granted
        </Directory>
</VirtualHost>

添加服务器别名,它也会识别IP地址...如果要添加更多IP地址(如本地网络第二接口),可以添加更多服务器别名行...


推荐