HOWTO: Configuring Name-based Virtual Hosts in Ubuntu 9.10 (Karmic Koala)

Setting up a VirtualHost can be performed a couple ways, one of which is direct manipulation of your httpd.conf. Instead, we will make use of the a2ensite and a2dissite tools to manage their enabling and disabling, respectively.

Start of by performing the following:

1
2
root@ubuntu:/etc/apache2/sites-available# touch www.site.org.localhost
root@ubuntu:/etc/apache2/sites-available# gedit www.site.org.localhost

Copy and paste the following into the above file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<VirtualHost *:80>
  ServerAdmin webmaster@site.org
  ServerName www.site.org.localhost
  ServerAlias site.org.localhost

  # Indexes + Directory Root.
  DocumentRoot "/var/www/projects/site/public_html"
  DirectoryIndex index.php

  <Directory "/var/www/projects/site/public_html">
    AllowOverride All
    Allow from All
  </Directory>

  # Logfiles
  ErrorLog /var/www/projects/site/logs/error.log
  CustomLog /var/www/projects/site/logs/access.log combined
</VirtualHost>

Enabling the VirtualHost:

1
2
3
4
5
6
7
8
root@ubuntu:/etc/apache2/sites-available# a2ensite www.site.org.localhost
Enabling site www.site.org.localhost.
Run '/etc/init.d/apache2 reload' to activate new configuration!
root@ubuntu:/etc/apache2/sites-available# /etc/init.d/apache2 reload
 * Reloading web server config apache2                                   [ OK ]
root@ubuntu:/etc/apache2/sites-available# /etc/init.d/apache2 restart
 * Restarting web server apache2                                   ...
waiting                                   [ OK ]

Virtual hosts can be disabled by using ‘a2dissite’. It also needs to be added to the ‘hosts’ file:

1
2
3
4
5
6
7
8
9
10
11
12
root@ubuntu:/etc/apache2/sites-available# cat /etc/hosts
127.0.0.1   localhost
127.0.1.1   ubuntu
127.0.0.1   www.site.org.localhost

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Simply point your browser to http://www.site.org.localhost to be taken to your VirtualHost.

Further Reading

(1) http://library.linode.com/lamp-guides/ubuntu-9.10-karmic/#configure_name_based_virtual_hosts (2) http://www.debian-administration.org/articles/412