Linux – VirtualHost with Multiple Domains Not Serving Correct DocumentRoot

Apache2linuxvirtualhost

Running CentOS 6, Apache.

I have the following in my httpd.conf:

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName domain1.example
    ServerAlias www.domain1.example
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/domain2
    ServerName domain2.example
    ServerAlias www.domain2.example
</VirtualHost>

Whenever I visit domain2.example (or www.domain2.example), I am shown domain1.example.

EDIT: More info from httpd.conf

Listen 80
#NameVirtualHost *:80

Things I have checked:

  • Apache has been restarted
  • DNS for domain1.example and domain2.example point to same IP

What am I missing?

SOLUTION:

As Patrick points out, in Apache 2.2, NameVirtualHost is required when using multiple VirtualHosts. The solution here was to uncomment the line in httpd.conf:

#NameVirtualHost *:80

to:

NameVirtualHost *:80

Best Answer

As you are using only Apache 2.2 you absolutely need to use the NameVirtualHost directive in order to let Apache know that you have multiple websites under different names but with the same IP.

The directive became deprecated in Apache 2.3, as written in this documentation: httpd.apache.org/docs/2.4/mod/core.html#namevirtualhost

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.

For your version, the guide on configuration name based virtual host is here: http://httpd.apache.org/docs/2.2/vhosts/name-based.html#using

Related Topic