PHP, MySQL, JavaScript, Windows 7, Linux
Setting up virtual hosts with Apache on Windows XP
In this tutorial, we will discuss how to setup virtual hosts for your development environment (or production, if you wish) on Windows XP. This will allow you to use URLs such as http://mywebsite/ instead of http://localhost/mywebsite/, which is much more realistic.
- First, document where your source code is. This will be our DocumentRoot setting.
- Go to the Start menu and click Run. Type notepad “C:\Windows\system32\drivers\etc\hosts” and press enter.
- At the end of the file, type 127.0.0.1, tab or space, then the virtual host name. Using the hostname mywebsite, the hosts file would look something like this:
127.0.0.1 localhost 127.0.0.1 mywebsite
- Next, open your Apache configuration file (httpd.conf). This will be located in one of a few different places.
- Zend Server CE: C:\Program Files\Zend Server\Apache2\conf\httpd.conf
- Apache (default): C:\Program Files\Apache Software Foundation\conf\httpd.conf
- Apache (often suggested): C:\apache\conf\httpd.conf
- Find the following lines and uncomment the one starting with Include. (Note: To uncomment the line, remove the # character.)
# Virtual hosts Include conf/extra/httpd-vhosts.conf
- Open /conf/extra/httpd-vhosts.conf. Delete the dummy example code, and replace it with the following:
<VirtualHost *:80> ServerAdmin youremail@yourhost.com DocumentRoot "C:\htdocs\mywebsite\public" ServerName mywebsite # Optionally, turn on logging ErrorLog "logs/mywebsite.error.log" CustomLog "logs/mywebsite.access.log" common </VirtualHost>
- After turning on the VirtualHost feature, the main document root will be disabled, so we have to add it as a VirtualHost.
<VirtualHost *:80> DocumentRoot "C:\htdocs" ServerName localhost </VirtualHost>
- Next, restart your Apache server instance. This can be done via the Apache Service Monitor or the Administrative Tools > Services section of Control Panel.
- It is possible that we will still not be able to view our newly created virtual host; however, we have a way around this. Go to the Start menu, and click Run. Type cmd and press enter.
- At the Windows Command Prompt, type ipconfig /flushdns. This will purge the DNS Resolver cache.
- Open http://mywebsite/ in your favorite browser.
If you have any trouble, feel free to comment and we’ll do our best to help out.




September 14, 2009 - 3:47 pm
Thanks for this incredibly timely article. I’ve been struggling with an issue that this should fix for me.
September 15, 2009 - 12:08 am
Happy to help! I hope you can get that issue resolved. Let us know how it goes.
-Chris