Blog

A Virtual Host per Project

Not long before my old laptop got to the end of it usable lifespan I started playing with the Zend Framework in my spare time. One of the cool things about ZF is that it wants to use friendly URLs, and a dispatcher to handle all the requests. The downside of this approach, and how ZF is organised, it works best if you use a Virtual Host per project. At first this seemed like a real pain to have to create a virtual host per project. One Saturday afternoon I worked through the Apache docs in search of a solution - then I found it! Rather than bore you with more of my views on Zend Framework, I will explain how to have a virtual host model that requires a little work up front and is very low maintenance.

It gets tedious copying and pasting virtual host config files each time you want to start a new project, so instead I let Apache do the work for me.

I added a new virtual host config file called projects to /etc/apache2/sites-available. The file contains

UseCanonicalName Off

LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon

<Directory /home/dave/Projects>
Options FollowSymLinks
AllowOverride All
</Directory>

NameVirtualHost 127.0.0.2
<VirtualHost 127.0.0.2>
    ServerName projects

    CustomLog /var/log/apache2/access_log.projects vcommon

    VirtualDocumentRoot /home/[username]/Projects/%1/application/www
    AccessFileName     .htaccess
</VirtualHost>

The important bit is the VirtualDocumentRoot directive which tells Apache to map a hostname to a path. I use an IP address from the 127.0.0.0/8 range for the virtual host, so they aren’t accessible to the outside world and I don’t have to worry about it changing every time I check locations.

All of my projects live under ~/Projects and each one gets a directory structure that looks something like this.

[projectname]
  |
  +- notes - coding notes, like grep output when refactoring etc
  |
  +- resources - any reference material or code snippets
  |
  +- application - the code for the project
     |
     +- www - document root for vhost

There are usually other paths here too, but they vary from project to project.

To make this work there are few more steps. First enable the new virtual host by running sudo a2ensite projects. Don’t reload Apache yet.

Next you need to add the Apache module with sudo a2enmod vhost_alias. Now it’s time to edit your /etc/hosts file so you can find the virtual hosts. Add a line similar to this:

127.0.0.2 projects phpgw-trunk.project [...] phpgw-stable.project

Now you can restart apache by calling sudo /etc/init.d/apache2 reload.

This is also handy for developing client sites - especially using drupal.

Now my /var/www/index.html is just an empty file.

I am getting a bit bored with adding entries to /etc/hosts all the time. If I get around to adding dnsmasq with wildcard hosts to the mix, I will post a follow up.

This setup is based on my current dev environment (Ubuntu Hardy), but it also works on older versions of Ubuntu. The steps should be similar for Debian and derivatives. For other distros, it should work, just how to make it work may be a little different. Feel free to post tips for others in the comments.