Search code examples
apache2centosdocument-rootvirtual-hosts

DocumentRoot is set to docRoot of a virtualHost?


OS: centOS 6.3 Final

I've installed the mysql and apache2(httpd) packages and changed the config in /etc/httpd/conf/httpd.conf as seen below:

<VirtualHost *:80>
    DocumentRoot /var/www/html/wordpress/
    ServerName www.asterix.int
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /var/www/html/staticSite/
    ServerName www.meins.lan
    ServerAlias www.deins.lan
    ServerAlias www.obelix.int
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /var/www/html/joomla/
    ServerName www.example.com
    ServerAlias www2.example.com
    ServerAlias www3.example.com
</VirtualHost>

All ServerName and ServerAlias are working fine and I can access the 3 pages.

But when I'm trying to access a page over the server's ip something strange (at least for me) happens. apache2 returns me the index page of the first defined virtualHost(in this example wordpress). I've tried this with all 3 virtualHosts and get the same Results.

Is this a normal behavior or what I'm doing false ?

If this is a normal behavior: Can I set the DocumentRoot exclusively for all requests to the ip ?

Thx !


Solution

  • This is the intended behavior. If you use the IP (let's say http://123.123.123.123/), Apache will use 123.123.123.123 as HTTP the hostname. Since there is no VirtualHost with a ServerName or ServerAlias of 123.123.123.123, the first VirtualHost is used.

    So if you want a VirtualHost that listens only for http://123.123.123.123/ you can simply create a VirtualHost with:

    <VirtualHost *:80>
        DocumentRoot /var/www/html/my-ip-site/
        ServerName 123.123.123.123
    </VirtualHost>