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 and found a solution - then I found it fantastic. 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

$ sudo a2ensite projects

Don't reload apache yet.

Next you need to add the apache module

$ sudo a2enmod vhost_alias

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.

$ sudo /etc/init.d/apache2 reload

This is 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.

Interesting approach. . .

Wil Sinclair wrote:

Have you considered adding it to the ZF Developers wiki? http://framework.zend.com/wiki/display/ZFUSER/Home

It's a bit freeform for now, but I'm hoping to get the time to reorganize it soon and make it more effective as a ZF developer KB.

,Wil

Added Thu, 2008-05-22 16:51

same configuration, nice to know others use that too.

Salz wrote:

Nice to see that also other people out there use this kind of development cyclo (just create an new projekt-dir, everything works). But i got an small problem, maybe you got that too. Did you try to use additional Virtualhosts configurations for your PC name?

Example: *.local is translated by VirtualDocumentRoot into /var/www/local/*, but i would love to give test.local an special configuration, i.e. /var/www/local/test/public. Did you tried it in the past? Does it work for you?

bye.

Added Wed, 2008-12-10 20:52

Forget my last comment, for

Salz wrote:

Forget my last comment, for some strange reasons it works now.

Added Wed, 2008-12-10 22:18