Monday, April 20, 2009

Creating a local virtual host for testing

I have a modest little website. It's nothing too fancy, but has some php and css that make it non-trivial, enough that I'm uncomfortable making changes to it live, so I wanted to set up a secondary test server to try things out on.

Sounds easy enough, but where? A virtual host on the same server as my website seems logical enough, but I don't own the box it runs on, I get the space thanks to some generous friends and I didn't want to bug them for another favor to set up a virtual host.

Thinking about it I realized I was already running Apache on my laptop, so in theory could set up a virtual host on it. I could use /etc/hosts to create a "test hostname" pointing at my localhost I could use in URLs. So that's what I went about doing.

First, I added an entry to /etc/hosts:
$grep test.vwelch.com /etc/hosts
127.0.0.1 test.vwelch.com
Then I added the virtual host entry for test.vwelch.com to my Apache configuration so that it would recognize URLs using it and serve content from a separate location that it normally would. This is on a Mac, so the details here might vary based on your OS, but if you search for "virtual host configuration" and the name of your OS I suspect you'll find help.
$ cat /etc/apache2/other/vwelch.conf
NameVirtualHost test.vwelch.com


ServerName test.vwelch.com
DocumentRoot /usr/local/www/



Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all

Then I checked out a copy of my website from my git repository into /usr/local/www, restarted Apache ("apachectl restart") and pointed my web browser at "http://test.vwelch.com" and I had a complete copy of my website in a local playground that I could develop on.