TwitterFollow Me on Twitter

Virtual Hosts

Sometimes I run across things I should have known a long time ago. Sometimes, when I find those things, I say to myself, “Man, I should have figured that out. It makes so much sense.”

Virtual hosts are one such thing.

A few weeks ago I was stumbling around the Godbit forums as I do at least once a day and I was intrigued by a post about virtual hosts. I was a bit misled, thinking the topic was a web host offering service delivered on a virtual machine (more on that later.) Instead, the post revolved around the idea of creating a set of locally resolving URLs for use in the test environment so that projects can be delivered in more of a ‘root’ location.

Allow me to explain a little further.

In a local test environment, a web server combined with a scripting language (or two) and even databases can produce the same kind of results that you would receive while working on a traditional web server, perhaps just like the one at your hosting company. So for all my projects, I create a folder that is ‘served’ to my own machine. The HTML documents and PHP scripts I produce can be examined and run in place allowing me to test and tweak these files very quickly and without the need to transfer files back and forth between a remote server. Of course, once a project is complete I do make exchanges between the remote server and push the content live on the w3.

But before those files ever see the light of day, they live for quite some time on my local server. When I load a project, I usually just throw the standard local machine URL and the project folder to view my work:

http://localhost/project

Now, this works quite well most of the time, but occasionally I’ve run into some small snags because the information is delivered from a subfolder. .htaccess files typically struggle a bit here. It would be best if we could run things from a root folder, but it’s certainly not ideal to run multiple projects from that root folder. It’d be impossible to manage.

Enter virtual hosts.

By editing some information in the hosts file of my machine and editing the conf file in the Apache web server, I can create any URL I want and associate that URL with any folder or subfolder of the webserver’s /htdocs directory (the directory responsible for storing the web content that is served.) So, theoretically I can create a URL for that same project that reads more like a standard URL. In fact, it can even be:

http://www.davidproject1.com/

Yep. Even though I don’t own that domain, and would never want to anyway, I can tie that URL to the project folder and have that information served from my local server into my browser of choice.

The first trick is editing the Windows hosts file. Grab your favorite plain text editor and point your file browser to C:/WINDOWS/system32/drivers/etc/. Once there, open the hosts file. (You can select Notepad from the list of available apps if you don’t care to grab something better to edit with.) Your file will open to something like this:

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost

The first several lines of the file (the lines that begin with a hash mark) are just commentary for the file. The line with no hash in front is where the real action begins. 127.0.0.1 is the loopback1 address of your machine (not to be confused with the local area network IP or public IP addresses which are both different.) localhost is simply the domain that resolves to the loopback address of 127.0.0.1. Essentially, the loopback allows you “address” your own machine from your own machine. Yeah, cool. You can probably start to see where we are going with this. Below that last line, go ahead and add a line like this:

127.0.0.1       myspace.com

Now save the file. You can leave the hosts file open in your text editor for now.

Oh boy. This is going to be fun. Now pull up a browser and enter myspace.com into the address bar and go.

Sweet! No more MySpace. Now, the myspace.com domain just resolves to your own machine because your hosts file is saying “all requests to myspace.com should resolve locally, not via the Internet.” Woo hoo. You can really have some fun with this! (But play nice!)

Seriously now, go back and change the hosts file, remove that myspace.com nonsense (you can’t live without it), and add in some domains that are easy to remember and are related to your projects. You can even create a new domain extension (TLD) if you like. For instance, my church related projects look something like this:

127.0.0.1       http://buzzconference.ncc

That way, I don’t confuse the .com or any other URL with my local project.

Make the system your own and feel free to use regular domains or even domains without extensions, like zulu-project which would resolve locally by browsing to http://zulu-project.

Until now, your domains only resolve to the root folder of your web server, just like http://localhost does. In the next step, we’ll let Apache know that we want certain local requests routed to a particular folder. If you’ve never edited Apache’s conf file, you may want to read up on a few docs at Apache. It’s really not as difficult as it seems, but you do want to exercise some caution as with anything like this.

You can find the httpd.conf file in the /conf directory of the Apache installation. Also, some versions of Apache actually list the virtual hosts information in a separate file called https-vhosts.conf. If yours is the latter, that’s where you’ll edit. For the rest of us, pop open that httpd.conf and scroll way down. It’s probably near the bottom. The line you’ll be looking for should read NameVirtualHost 127.0.0.1. If there is a hash sign (#) commenting out the line, just remove the hash to activate that line. Now for each and every local project or site that you’d like to run, add a set of lines like this:

<VirtualHost 127.0.0.1>
    ServerName localhost
    DocumentRoot "C:&#092Program Files&#092xampp&#092htdocs"
</VirtualHost>

In each case, replace localhost with the domain that you listed in the hosts file. Then, edit the DocumentRoot to the path of the folder that the project or site resides in. An example in my case might read like this:

<VirtualHost 127.0.0.1>
    ServerName buzzconference.ncc
    DocumentRoot "C:&#092Program Files&#092xampp&#092htdocs&#092buzzconference"
</VirtualHost>

To get Apache to recognize your new settings, you’ll need to restart the Apache server. In fact, any time you change the conf file, you should restart Apache as soon as it’s possible to do so.

Now when you load your project domain into your browser, the hosts file will keep your domain local and the Apache conf file will route your request to the appropriate folder.

Virtual Hosts! A must-have little hack in the tool belt of every web developer.

Apache Virtual Host Documentation

XAMPP - Install Apache, PHP, Perl and MySQL in one easy go.

Article on Virtual Hosts

The Godbit Discussion and Other Links

Virtual hosts are possible with IIS. I don’t use IIS. But there should be lots of information out there on it if you are interested.

1Thanks to Pastor Sam for pointing out the proper semantics of the loopback address.

[tags]apache, web, server, virtual, host, domain, local, development[/tags]

8 Comments

  1. Jim Walton

    2007-02-09 0907hrs

    Gravatar

    That’s awesome!  I knew about the hosts file but what you describe takes it to a whole other level.  Very useful!  It makes sense to have a couple of test environments set up that you can re-use.

    Thanks man!

  2. Matt Kerner

    2007-02-09 1101hrs

    Gravatar

    You can do this on the Mac as well using NetInfo Manager or the following commands from the terminal:

    sudo niutil -create . /machines/hostName
    sudo niutil -createprop . /machines/hostName ip_address “127.0.0.1”
    sudo niutil -createprop . /machines/hostName serves ./local

    kill -HUP `cat /var/run/lookupd.pid`

  3. David

    2007-02-09 1408hrs

    Gravatar

    Jim: Thanks for the trackback!

    Matt: Awesome! Thanks for that info. I’m sure it will come in handy for my Mac pals. I love that CLI stuff. :)

  4. Grady Russell

    2007-02-09 1415hrs

    Gravatar

    Dave,

    X-cellent explanation. Most of us would consider virtual hosting to be too complicated to expend the effort. Your desciptions make it as easy and functional as it has always been intended. Great tool for gurus like you and old school types like your dad!

    Thanks for the insight into a useful concept

    Dave’s Dad!

  5. Pastor Sam

    2007-02-09 1758hrs

    Gravatar

    Just a note: a lot of people doing web development on their personal computer may not be doing it on a windows machine. If you’re using a Mac, or anything running a variation of linux or unix, then that HOSTS file is located at “/etc/hosts” from your root directory.

    Something REALLY cool about this technique is that you can put in your true local IP address in your hosts file.
    127.0.0.1 is actually your LOOPBACK address, not your IP address. That may sound like semantics, but it is important. Your true local IP address will probably be something like 192.168.1.101 or 10.0.0.3, and you can find it out by running the command “ipconfig” in windows or “ifconfig” in mac/linux/unix at a command line.

    Once you know your IP address you can adjust you can change other people’s HOSTS files who have computers on your network and type in:

    “192.168.1.1         http://testsite.com”

    subsituting your IP address and site name, and then on ANY of those computers on your network you can visit “testsite.com” in a browser and see your live demo site.

    Now you can see if your site looks the same in Internet Explorer 7 on one machine, or IE 6 on another, or Safari on a mac, or in firefox, or even what that old Windows 95 machine gathering dust thinks of your site. This kind of multi-platform, multi-browser testing is INVALUABLE when setting up a new site.

    Just remember to fix everyone’s HOSTS files back again when you do go live on the web!

  6. David

    2007-02-09 1918hrs

    Gravatar

    Dad: Thanks!

    Pastor Sam: Great tip for Unix / Mac users and for local development on a network of machines. Thanks for checking in! Typically, I tweak the central server that handles our web traffic and can do a similar thing on a single machine rather than on individual ones, but either approach will work. Also using a different TLD than .com will allow you to leave those machine’s hosts files setup if you don’t want to have to go back and change them out. Also, thanks for the check on the semantics of the loopback address as well. You’re right, I needed to be accurate and I wasn’t. I’ll correct.

  7. Zack Rippy

    2007-02-10 1243hrs

    Gravatar

    Great information, David.

    The HOSTS file is one of the most misunderstood (or not understood) “tools” out there. A lot of “secure computing” environments use them exclusively in place of DNS, so that the systems only know about the hostnames specifically entered into the HOSTS file. We use them quite frequently. The Apache trick is a great one as well and sure makes development a bit easier.

    Awesome post, man. Keep it up. :)

  8. Stacey

    2007-05-16 1125hrs

    Gravatar

    Very cool instructions. The ones on the Apaache Site looked freaky but yours are really cool and I’ve gotten sick of moving my files around all the time.

    One question: Do you know of image paths being screwed up? Eg if you have a tag: ‘’ will that work? I’ve got it and it can’t find the image. Once I took out the preceeding ‘/’ it found the image.

    If you know of something please let me know.

    Thank you so much for the great breakdown.

Leave a Comment

Commenting is not available in this section entry.

Twitter Status

2010-07-29 1117

Very nice. Less tab clutter. RT @nathansmith: RT @faaborg: App Tabs in Firefox 4 Beta 2: http://bit.ly/bkWIHp

2010-07-29 0000

ffmpeg and exiftool are my pals.

2010-07-28 1524

@dave_clark Working in our production suite today because we are getting set up with new live streaming from @kulabyte. (Not with the FS1.)