WordPress – I can’t have the virtualhosts working properly in Apache

apache-2.4moodle;virtualhostWordpress

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.

Best Answer

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.