我不能让我的虚拟主机在Apache中正常工作

虽然我知道这个话题已经在其他一些线程里讨论过了,在这个线程里,如何在Apache中configuration虚拟主机。 事情是在寻找可能的答案后,我在这里的问题,在谷歌研究和阅读Apache官方文档我不能让我的问题得到解决。

首先,我在CentOS 7上安装了一个运行在Apache 2.4上的WordPress安装,并设置了下面的虚拟主机:

<VirtualHost *:80> ServerName subdomain1.domain.com ServerAlias www.subdomain1.domain.com DocumentRoot /some/path/to/wordpress <Directory "/some/path/to/wordpress"> DirectoryIndex index.php Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted </Directory> <Files ".ht*"> Require all denied </Files> ErrorLog "logs/subdomain1-error.log" CustomLog "logs/subdomain1-access.log" combined LogLevel warn </VirtualHost> 

它工作得很好,但我决定添加一个Moodle安装(因为服务器没有太多的stream量),然后如果我浏览铬subdomain1.domain.com或subdomain2.domain.com没关系,Chrome始终去subdomain2.domain.com(Moodle网站)。

我在Chrome和Firefox上尝试了相同的结果。 Moodle虚拟主机是:

 <VirtualHost *:80> ServerName subdomain2.domain.com ServerAlias www.subdomain2.domain.com DocumentRoot /some/path/to/moodle <Directory "/some/path/to/moodle"> DirectoryIndex index.php Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted </Directory> <Files ".ht*"> Require all denied </Files> ErrorLog "logs/subdomain2-error.log" CustomLog "logs/subdomain2-access.log" combined LogLevel warn </VirtualHost> 

而且,Apacheconfiguration文件httpd.conf包含这些VirtualHosts使用指令:IncludeOptional conf.d / *。conf

文件/etc/hosts是:

 127.0.0.1 localhost 134.122.191.221 subdomain1.domain.com 134.122.191.221 subdomain2.domain.com 

在我的Apache虚拟主机configuration文件中我看不到任何明显的错误,所以如果有人能给我一个关于它是什么错误和任何可能的错误的提示,我将不胜感激。

非常感谢。

您正尝试设置基于名称的虚拟主机,并且缺less一个重要的configuration:

 NameVirtualHost *:80 

所以,在你的httpd.conf中添加上面的行并重新启动apache / httpd,它应该解决这个问题。

NB根据apache 2.4文档 , 这个指令已经被弃用了,不再需要了

在2.3.11之前,需要NameVirtualHost指示服务器特定的IP地址和端口组合可用作基于名称的虚拟主机。 在2.3.11及更高版本中,只要在多个虚拟主机中使用IP地址和端口组合,就会自动为该地址启用基于名称的虚拟主机。

但是我还没有找出为什么它与上述指令一起工作。