Configuring apache2 to work with subdomains on Ubuntu 11.04
This post will be absolute techno-babble for most of my readers and if you don’t understand the title then it’s probably best that you skip the rest…. I’ll post something with a wider audience appeal soon, however I needed to document these steps for my own future use (as my memory sucks!) and hopefully someone may also find this post useful!
That said, I like to play around with web development and have a myriad of projects on my little Ubuntu 11.04 LAMP “server” and until today I’ve used subdirectories to separate my individual projects, however this has lead to problems in the past when deploying a site from the development environment to the big www. So now I’m moving to subdomains
After an hour of Google-ing I cobbled together a simple solution that works for me. I work primarily from a Windows XP machine which has an entry in the hosts file that points the domain “dev” to the linux box. Rather than carry on using http://dev/holiday/ to access my new photo site I wanted to use http://holiday.dev/ – so here is how I did it…
The first thing to do is to add an entry for the new “host” into your Windows hosts file, which can be found in C:\WINDOWS\system32\drivers\etc (assuming that your install is based on C:\Windows).
Open the hosts file with notepad and add a new entry for your subdomain. If you are running Windows 7 then you will need to run Notepad as an Administrator.
As you can see my Ubuntu servers’ IP address is 192.168.0.206 and its hostname is dev - all that I need to do to enable the new subdomain from my Windows PC is to duplicate the existing record and changed the host name to holiday.dev

The next step is to create a new host record on the Ubuntu machine – to do this enter the following into the Ubuntu console (noting that you should use your own subdomain here):
sudo gedit /etc/apache2/sites-available/holiday
And when prompted with a blank document enter:
<VirtualHost *:80> DocumentRoot /var/www/holiday/ ServerName holiday.dev <Directory /var/www/holiday/> Options Indexes FollowSymLinks MultiViews Includes AllowOverride AuthConfig FileInfo Order allow,deny Allow from all </Directory> </VirtualHost>
Save the document and all that is left to do is to create the symlinks in sites-enabled to allow the site to be served with the following command:
sudo a2ensite holiday
And a quick restart of apache with the following command:
sudo /etc/init.d/apache2 restart
and you are good to go!
