Aug 9, 2011
tom

Trying to create subdomain with Apache2

Question

I’m using Ubuntu 10.04 and Apache2.2 on a Linode VPS – I’m trying to create a subdomain sub.mysite.com of mysite.com. I’ve added this entry to /etc/apache2/sites-enabled:

<VirtualHost *:80>
     ServerAdmin email@gmail.com
     ServerName mysite.com
     ServerAlias sub.mysite.com
     DocumentRoot /srv/www/sub.mysite.com/public_html/
     ErrorLog /srv/www/sub.mysite.com/logs/error.log
     CustomLog /srv/www/sub.mysite.com/logs/access.log combined
</VirtualHost>

and this line to /etc/hosts:

127.0.0.1       sub.mysite.com

and created a DNS zone file pointing to sub.mysite.com…

I can successfully ping sub.mysite.com but can’t get index.html to load in my browser…

Any ideas?

Answer

Since you’re using virtual hosts, make sure to put this subdomain container BEFORE your main virtual host container. What ever comes first trumps whatever comes after, so if your file looks like this, it won’t work:

<virtualHost *:80>
 {snip}
 ServerAlias *.mysite.com
 DocumentRoot /srv/www/mysite.com/public_html/
 {snip}
</virtualHost><VirtualHost *:80>
 {snip}
 ServerAlias sub.mysite.com
 DocumentRoot /srv/www/sub.mysite.com/public_html/
 {snip}
</VirtualHost>

Make sense? You have to flip them, so that the 2nd VirtualHost container in my example is first. If that’s not the problem, and you’ve already done this, then check the Apache error log (actually, that should be your first step, regardless).

Also, make sure file permissions are correct.

Related posts:

  1. Apache2 subdomain serve up the same conent
  2. Subdomain not being recognised in sites-available
  3. Apache VHost Configuration Precedence?
  4. apache virtual hosts – server not found for subdomain
  5. Apache2 dynamic documentroot depending on URL

Leave a comment