A particularly useful feature for anyone developing for the Web using Apache is that you can define virtual hosts for your local test environment. This brief article will tell you how to do exactly that but assumes that you have some very basic knowledge of Apache (as long as you can edit the httpd.conf you should be fine). For our example let us say that I am currently developing three applications: Spiffy; Tidee; and Kewl. The code for each of these projects lives in a folder, named after the application, under “C/wamp/www”. Note that my strive towards complete idleness means that I have used the excellent WampServer to set my development environment up. I should also say that while these instructions are written for Windows systems they should apply pretty much to Linux as well.
Now I could just change the document root in httpd.conf when I switched to working on a different project but this is a pretty unsatisfactory state of affairs. What I really want to be able to do is to type http://spiffy (or one of the alternatives) into my browser’s address bar and get the spiffy start page. And that’s where virtual hosts come into play. If you’ve never come across the term before it is the same way that Web hosts can host multiple Web sites on the same server.
Setting up virtual hosts in Apache 2.0 is simplicity itself. Just open up httpd.conf and add (or un-comment) the line
NameVirtualHost *:80
this tells Apache to listen for any requests for your virtual hosts on the default http port 80.
Don’t close httpd.conf just yet as you also need to define your hosts. The code below shows our three example apps defined as well as the default localhost. It shoudl be pretty self-explanatory but the most important elements are the ServerName (which should be the name you want to type into the address bar) and the DocumentRoot (the folder containing your app).
<VirtualHost *:80>
DocumentRoot "C:/wamp/www"
ServerName localhost
ErrorLog logs/defaultsite-error_log
CustomLog logs/defaultsite-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/spiffy"
ServerName altlocalhost
ErrorLog logs/spiffy-error_log
CustomLog logs/spiffy-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/tidee"
ServerName localhost
ErrorLog logs/defaultsite-error_log
CustomLog logs/defaultsite-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/kewl"
ServerName kewl
ErrorLog logs/kewl-error_log
CustomLog logs/kewl-access_log common
</VirtualHost>
I’ve changed the apache and error logs for two of the apps but you could leave them at default if it isn’t important to you.
That’s it for the Apache side of things. Save and close httpd.conf and we have one more job to do.
The code above will set up your Apache virtual hosts but you still need to get your browser to recognise that http://spiffy is a local host. This is another easy task and just involves editing your systems ‘hosts’ file. On Windows you can find this in “C:/windows/system32/drivers/etc. Open this file in notepad and you will see it contains a list of IP addresses and hostnames. If you have never edited the hosts file before you should see at least one entry:
127.0.0.1 localhost
For our example we would need to add the following lines:
127.0.0.1 spiffy
127.0.0.1 kewl
127.0.0.1 tidee
And that is it. That’s all it takes to get your virtual hosts up and running.
Admittedly there are lots more options for virtual hosts and if you’re interested there is a wealth of information in the Apache documentation but for most uses this is as much as you need to do.
|