Recently I’ve been working on 3 or 4 projects at the same time on my desktop. While my primary IDE has changed to Visual Studio (even for PHP with VS.PHP), the rest of my development environment is mostly the same as it’s been since December. My projects are growing in code size, and the amount of code I reuse is increasing, so it makes sense to develop locally instead of directly on a server over SSH like I used to in previous years. It’s also become inconvenient to have a single web root for testing the sites locally since URLs relative to the root (starting with /) won’t work if each project needs to be a subdirectory of localhost.
With Apache’s named virtual hosts, and the Windows hosts file, it’s easy to run multiple local websites with a single web server instance. For example, I can reach the local copy of W3Counter at http://w3counter, and the local copy of this blog at http://dg. I can even temporarily resolve the full domains of the sites I’m working on to my local machine to test code and URLs with absolute paths. There are two simple steps to setting this up:
- Add the virtual hosts to the Apache configuration:
Open up the configuration file, httpd.conf. I like to add my entries towards the end of the file. Add this line:
NameVirtualHost *:80
If your local server isn’t running on port 80, change that to what port you are running it on. Then add a VirtualHost entry for each named site you want to host locally. For example:
<VirtualHost *:80>
ServerName djg
UseCanonicalName Off
DocumentRoot c:/www/dg
</VirtualHost><VirtualHost *:80>
ServerName w3counter
UseCanonicalName Off
DocumentRoot c:/www/w3counter
</VirtualHost> - Add the host names to Windows’ hosts file so they resolve to your local machine:
Open up an Explorer window and go to the directory c:\windows\system32\drivers\etc. If you can’t see this directory, you may have system folders hidden. In the Explorer window, go to “Tools”, then “Folder Options”. In the advanced settings, uncheck “Hide protected operating system files” and check “Show hidden files and folders”.
Right click on the file named “hosts” and click on “Properties”. Uncheck the “read-only” box and click “OK”. You may need administrator rights to do that in Windows Vista.
Now that you can edit the file, open it up in Notepad. At the end of the file, add entries pointing the hosts you just added to httpd.conf to also resolve them to your local PC’s IP address (127.0.0.1). For example:
127.0.0.1 djg
127.0.0.1 w3counter
Save the hosts file and httpd.conf and restart Apache. When you type one of your new host names into a browser window, you should see the website located at the DocumentRoot directory you specified.
Remember that Windows sometimes keeps DNS cached, so if you add an actual domain to your hosts file, or want to change the IP a host resolves to, you may need to flush that DNS cache. You can do this from a command prompt with “ipconfig /flushdns”.



Kankamuso wrote —
Kankamuso wrote —
Dan wrote —
Sara wrote —
Robert Norton wrote —
Dan wrote —
Jefferson Roberts wrote —
GB wrote —