我正在使用Ubuntu 14.0建立一个新的Web服务器,在the instructions here之后用于创建虚拟主机,但是我的单个附加虚拟主机总是加载默认站点(/ var / www / html)。
这是我的vhost配置(我已经用example.com替换了我的域名,但它在其他方面是相同的):
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example/app
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
我对Ubuntu 14中安装的Apache 2.4配置没有做任何其他更改。
编辑:我正确地启用网站并在更改此虚拟主机后重新启动Apache。
当我在浏览器中访问example.com时,我获得了默认站点。我究竟做错了什么?如何让此虚拟主机加载正确的目录?
编辑:解决方案是重启命令(https://stackoverflow.com/a/23713299/225682)中缺少“sudo”。
你需要在编辑vhost后重启apache我们的服务器,你还需要添加enable vhost。启用:
sudo a2ensite example.conf (name of config file inside sites-available directory)
重启服务器:
sudo service apache2 restart
还要检查www目录的权限,也许apache的用户无法读取该目录。
您是否在配置中的某个位置启用了虚拟主机?
NameVirtualHost *:80
终于在https://stackoverflow.com/a/23713299/225682找到了答案。
必须使用sudo重新启动Apache,否则更改实际上不起作用。
那是我的故事。我有新安装:
webmaster@ubuntuserver:/srv$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
webmaster@ubuntuserver:/srv$
webmaster@ubuntuserver:/srv$ apache2 -v
Server version: Apache/2.4.18 (Ubuntu)
Server built: 2017-09-18T15:09:02
webmaster@ubuntuserver:/srv$
我使用了非常简单的虚拟主机配置:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName testsite.com
ServerAlias www.testsite.com
DocumentRoot /var/www/testsite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
尝试过许多建议,例如:
最后解决了添加:
Listen 80
到虚拟主机配置的顶部,所以它看起来像:
Listen 80
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName testsite.com
ServerAlias www.testsite.com
DocumentRoot /var/www/testsite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
最终虚拟主机开始响应并按预期运行。
有趣的是,我后来尝试重新创建问题,虚拟主机甚至没有设置Listen 80
。但是我确信使虚拟主机响应的触发器是Listen 80
设置。
无论如何,您也可以参考Apache文档:
尝试使用-t选项运行apachectl命令,这将检查所有配置是否正确。这个虚拟主机是哪个文件。您是否重新启动了apache以获取这些更改?
apachectl restart