Virtual host for subdomain overriding default virtual host

apache-2.2virtualhost

I have two vhosts set up in Apache. One default one to handle all requests, and one for a specific subdomain. The www directory is set up as follows:

/var/www
/var/www/subdomain

The virtual host configs are as follows:

<VirtualHost *:80>
        DocumentRoot /var/www
        <Directory />
                Options None
                AllowOverride None
        </Directory>
        <Directory /var/www>
                Options None
                AllowOverride None
                Deny from all
        </Directory>
</VirtualHost>


<VirtualHost subdomain>
        ServerName subdomain.domain.com

        DocumentRoot /var/www/subdomain
        <Directory /var/www/subdomain>
                Options None  FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

Right now I want anything to subdomain.domain.com to be served from /var/www/subdomain and any other request to be denied. However when I visit domain.com, it serves /var/www/subdomain.

Appreciate if someone can tell me what I'm doing wrong. Thanks!


EDIT with extra info when running apache2ctl -S

/usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted)
apache2: Could not reliably determine the server's fully qualified domain name, using XX.XXX.XXX.XX for ServerName
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server domain (/etc/apache2/sites-enabled/default:1)
         port 80 namevhost domain (/etc/apache2/sites-enabled/default:1)
         port 80 namevhost sub.domain.com (/etc/apache2/sites-enabled/subdomain:1)
Syntax OK

Best Answer

Your problem is here:

<VirtualHost subdomain>

Don't do that, since it's giving that vhost priority over the existing one for whichever IP that resolved to. It should be <VirtualHost *:80> matching your existing vhost. The ServerName directive is where the 'name' of the vhost should go.

You also need a NameVirtualHost *:80 somewhere, if you don't already have that.

Related Topic