1

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

1 Answer 1

7

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.

6
  • Thank you, that did it. One follow up question, I tried adding NameVirtualHost *:80 to httpd.conf and the actual vhost file in /sites-available/ but I keep getting an error that says no vhost configured for NameVirtualHost *:80 when I restart Apache. Where should that line go?
    – Choy
    Oct 8, 2012 at 1:47
  • @Choy That's strange - what file are your <VirtualHost> blocks configured in? And what output do you get from apache2ctl -S? Oct 8, 2012 at 2:24
  • After running that command I get "/usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted)" followed by "*:80 is a NameVirtualHost" and then a default server pointing to /sites-enabled/default and two namevhost listings (for default and subdomain that I mentioned in the original post). The <VirtualHost> blocks are configured in sites-enabled folder as two separate files (default and subdomain).
    – Choy
    Oct 8, 2012 at 4:16
  • @Choy Can you edit your question with that output? The spacing matters for whether things are working correctly. Oct 8, 2012 at 4:47
  • Sorry about the bad formatting. I edited the the original post with the output.
    – Choy
    Oct 9, 2012 at 1:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .