Multiple local websites with Apache in Windows

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:

  1. 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>

  2. 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”.

AddThis Social Bookmark Button

8 Responses to “Multiple local websites with Apache in Windows”

  1. Kankamuso
    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.

  2. Kankamuso
    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>

  3. Dan Grossman
    August 25th, 2007

    Thanks for the tip Kankamuso!

  4. Sara
    August 26th, 2007

    I haven’t tried VS.PHP. What do you think?

  5. Robert Norton
    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

  6. Dan Grossman
    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.

  7. Jefferson Roberts
    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?

  8. GB
    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

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Network Activity

Visitor Boost and Targeted Visitors have received 0 order today and 0 order yesterday.

W3Counter is currently processing 0 queries per second for 13,676 websites.

Website Goodies is hosting 94,379 guestbooks, 12,586 counters and 7,427 polls.

Award Winning Hosts has collected 206 customer reviews of web hosts.