大网数据广告活动
服务器运维问答

I can't have my virtualhosts working properly in Apache

· 52CE 我爱测

Although I know that this topic has been already discussed in some other threads where it is explained how to configure Virtualhosts in Apache. The thing is after looking for possible answers to my problem here, researching in Go…

问题原文

原帖链接 · jocag · CC BY-SA 3.0

Although I know that this topic has been already discussed in some other threads where it is explained how to configure Virtualhosts in Apache. The thing is after looking for possible answers to my problem here, researching in Google and reading Apache official documentation I cannot have my problem solved.

Firstly, I had one WordPress installation running on Apache 2.4 installed in CentOS 7 and I set up the following Virtualhost:

<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>

It worked fine but then I decided to add a Moodle installation (because the server doesn't have too much traffic) and then if I browse in Chrome subdomain1.domain.com or subdomain2.domain.com it doesn't matter, Chrome always goes to subdomain2.domain.com (Moodle website).

I tried in Chrome and Firefox with same results. The moodle Virtualhost is:

<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>

Also, the Apache configuration file httpd.conf includes these VirtualHosts using the directive: IncludeOptional conf.d/*.conf

The file /etc/hosts is:

127.0.0.1   localhost 

134.122.191.221 subdomain1.domain.com

134.122.191.221 subdomain2.domain.com

I cannot see any obvious error in my Apache Virtualhosts configuration files so I would appreciate if someone could give me a hint about what it is wrong and any possible error.

Thanks a lot.

已采纳解答

原帖链接 · Diamond · CC BY-SA 3.0

You are trying to setup Name-based Virtual Host and missing an important config:

NameVirtualHost *:80

So, add the above line in your httpd.conf and restart apache/httpd, and it should solve the problem.

N.B. According to apache 2.4 documentation, this directive is deprecated and no longer needed.

Prior to 2.3.11, NameVirtualHost was required to instruct the server that a particular IP address and port combination was usable as a name-based virtual host. In 2.3.11 and later, any time an IP address and port combination is used in multiple virtual hosts, name-based virtual hosting is automatically enabled for that address.

But I'm yet to find out why it works with the above directive.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。