Several times now I have seen questions in IRC or from friends on how to configure apache2 for multiple hosts and sub-domains. This is a fairly simple task, do not get tempted into using tools such as webmin to handle this kind of configuration for you. It is easy to cripple yourself by using such tools as primary configurators instead of understanding how the systems work. There are some things that you need to know before proceeding:
sites-available — A list of configuration files, one per site.
sites-enabled — A list of symlinks to the configuration files in sites-available, only the ones that you want to have enabled.
a2ensite — Command that will create the proper symlinks in the sites-enabled directory from the sites-available directory.
a2dissite — Command that will remove the symlinks when
Navigate to your /etc/apache2/sites-available/ directory and list the contents:
storrgie@ARISTAEUS:/etc/apache2/sites-available$ ls
default default-ssl
If you are on a default install then you likely will get a very similar output to what I have. Alright its time to create an entry for a domain, and a sub-domain under that domain. Start with an entry for agdunn.net by typing:
sudo nano agdunn.net
Now for what I use, which is a pretty bare bones configuration:
<VirtualHost *:80>
DocumentRoot “/var/www/web.andrewdunn”
ServerName agdunn.net
<Directory “/var/www/web.andrewdunn”>
allow from all
Options +Indexes
</Directory>
ServerAlias *.agdunn.net
</VirtualHost>
Now notice that this entry has a ServerAlias which is a wildcard, that means that for any sub-domain that someone types my server will point it right through to the web.andrewdunn directory. Now we need to set up cases for my sub-domains, for instance, this blog. So start with anĀ entry for blog.agdunn.net by typing:
sudo nano blog.agdunn.net
Now another very bare bones configuration:
<VirtualHost *:80>
DocumentRoot “/var/www/blogs/init”
ServerName blog.agdunn.net
<Directory “/var/www/blogs/init”>
allow from all
Options +Indexes
</Directory>
</VirtualHost>
Now lets enable these two sites and move on, make sure you are in the /etc/apache2/sites-available/ directory and type:
sudo a2ensite agdunn.net
sudo a2ensite blog.agdunn.net
Now lets move over to the /etc/apache2/sites-enabled/ directory and check out the symlinks there:
storrgie@ARISTAEUS:/etc/apache2/sites-enabled$ ls -la
total 8
drwxr-xr-x 2 root root 4096 2009-04-16 23:31 .
drwxr-xr-x 7 root root 4096 2009-04-26 21:47 ..
lrwxrwxrwx 1 root root 26 2009-03-08 20:58 000-default -> ../sites-available/default
lrwxrwxrwx 1 root root 48 2009-03-12 12:32 blog.agdunn.net -> /etc/apache2/sites-available/blog.agdunn.net
lrwxrwxrwx 1 root root 49 2009-04-16 23:28 agdunn.net -> /etc/apache2/sites-available/agdunn.net
Here is where the lapse sometimes occurs, we have our sites enabled and everything should work right? Well not all the time, we need to ensure that apache reads the /etc/apache2/sites-enabled/ directory in the order that we please. Notice the “000-” prefix on the default config file. The ordering of config files is very important, especially for sub-domains. So lets go ahead and move these two sites so that they are numbered in an order that is more conducive to our operation:
sudo mv agdunn.net 019-agdunn.net
sudo mv blog.agdunn.net 018-blog.agdunn.net
The number I just chose was arbitrary, just make sure that the sub-domains come before the master domain entry.
Now restart apache and your sub-domain should work just fine, this is assuming you have all your DNS matters taken care of properly.
I would suggest installing vlogger – automatically splits up the access logs so you don’t have to read through one long one.