I’ve been trying out a new way to run web applications on my local machine. Since I work on many different websites I like to be able to run these sites on my local machine. This lets me try out changes locally before committing them to Subversion and rolling them out to a staging or production environment. In the past I’ve simply created a new virtual host in Apache listening on its own port. The problem with this is that not only do I need to manually setup each new site, I also need to remember which site goes with which port number.
So, I’ve started using Apache’s dynamically configured mass virtual hosting. This gets me two things. First, I don’t need to make any changes in Apache’s configuration file when I want to setup a new local web application. Second, I now have an easy-to-remember naming convention for sites that are running locally. This works really well for me, but your mileage may vary depending on your specific needs. There are also other tools that can help you accomplish similar results, but I like the simplicity of this in that it doesn’t require anything other than Apache and some DNS settings.
Let me explain the end result first, and then I’ll explain how I got there. For each website I work on I use the subdomain to indicate the environment it’s running in. Using the domain example.org we may have the following environments setup:
- local.example.org – the local copy
- beta.example.org – a semi-publicly available beta
- www.example.org – the live site
The beta and www versions aren’t anything special, just normal websites. For local.example.org I need to have a DNS entry that points to the loopback address, 127.0.0.1. This can either be a publicly available DNS entry (which could be a little awkward) or simply set in your hosts file. I’ve simply made the entry in my hosts file:
127.0.0.1 local.example.org
The downside of this is that you would need to update the host file on each machine that you wanted to run the local.example.org site on. A comprimise would be to run your own recursive DNS service that only your computers used and make the DNS entries there.
This next step only needs to be done once. The dynamically configured mass virtual hosting article explains in detail how to set this up. My Apache configuration looks something like this:
VirtualDocumentRoot /path/to/workspace/%2+/public
Again, your specific configuration may be different. When making a request to http://local.example.org/ the document root would expand to /path/to/workspace/example.org/public. This assumes that you have placed all of your projects in the same location and they all have a similar directory layout (I’m using Zend Framework’s directory layout). I think there’s a way to do this in a way that’s more flexible, but I haven’t had the need. Let me know if you have any questions!
2 Comments
Bradley — I had just remembered the mass-virtual host module of Apache myself a couple days ago, and was planning on trying it out. You beat me to it. 🙂
Nice writeup!
Matthew – thanks! I’ve found this setup very helpful. Eventually I’m going to setup Jason’s computer to do the same thing. The nice thing about this is that any designer, developer, or content writer can easily have their own copy of the website running without a lot of configuration.