Multiple local websites with Apache in WindowsRecently 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:
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”. |
Self-promotionRecent Comments
Activity Stream
Popular Posts
|




August 25th, 2007
It can be easier. I use:
VirtualDocumentRoot d:/www/%0
Options Indexes FollowSymLinks +ExecCGI
I add any “website” I want as a folder into d:\www (e.g. d:\www\djg). Of course, I’ve still have to add the domain name (djg in the example) to the hosts file. I’ve got a script which looks into d:\www and updates hosts file accordingly.
Cheers.
August 25th, 2007
Comments system ate my < and > symbols. The piece of config should read:
<VirtualHost *>VirtualDocumentRoot d:/www/%0
Options Indexes FollowSymLinks +ExecCGI
</VirtualHost>
August 25th, 2007
Thanks for the tip Kankamuso!
August 26th, 2007
I haven’t tried VS.PHP. What do you think?
August 26th, 2007
Hey Dan,
To follow up on Sara’s comment, what do you think of VS.PHP? I absolutely love VS’s development environment (especially where it allows you to hide blocks of code), would you say it’s worth the $99 or $149 price tag?
Thanks
August 27th, 2007
I haven’t decided if I’m going to buy a copy when the 30 day trial expires yet. It doesn’t do a whole lot other than syntax highlighting; I don’t think the Intellisense works any better than Eclipse’s with PDT… it’s just that Eclipse is unstable and slow. I got tired of the crashing and restarting, and I’ll take any IDE that can provide syntax highlighting and works with multiple open files at once easily. The MS employee discount on MSDN was huge.. I got the premium subscription with VSTS for just a few dollars.. so while I’m now using the latest Visual Studio for C#, it was convenient to also use for PHP. I also wanted to explore the supposed ability to debug JavaScript from inside VS 2008 on PHP pages.
October 4th, 2007
This worked nicely except only the very first directory comes up. No matter how many sections I set up and add to the hosts file all of those calls to the subdirectories goes to the first one.
Too odd, any ideas?
January 23rd, 2008
Hi, I’m trying to get Kankamuso’s suggestion to work - what does the ‘%0″ do?
Is that single Virtual Host declaration all that is needed, rather than one for each site like in the article?
Thanks