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.

  1. First, document where your source code is.  This will be our DocumentRoot setting.
  2. Go to the Start menu and click Run.  Type notepad “C:Windowssystem32driversetchosts” and press enter.

    Access Windows XP's hosts file
    Accessing Windows XP's hosts file
  3. At the end of the file, type, tab or space, then the virtual host name.  Using the hostname mywebsite, the hosts file would look something like this:  localhost  mywebsite
  4. Next, open your Apache configuration file (httpd.conf).  This will be located in one of a few different places.
    1. Zend Server CE: C:Program FilesZend ServerApache2confhttpd.conf
    2. Apache (default): C:Program FilesApache Software Foundationconfhttpd.conf
    3. Apache (often suggested): C:apacheconfhttpd.conf
  5. 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
  6. Open /conf/extra/httpd-vhosts.conf.  Delete the dummy example code, and replace it with the following:
    <VirtualHost *:80>
      DocumentRoot "C:htdocsmywebsitepublic"
      ServerName mywebsite
      # Optionally, turn on logging
      ErrorLog "logs/mywebsite.error.log"
      CustomLog "logs/mywebsite.access.log" common
  7. 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
  8. Next, restart your Apache server instance.  This can be done via the Apache Service Monitor or the Administrative Tools > Services section of Control Panel.

    Apache Service Monitor
    Apache Service Monitor
  9. 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.

    Accessing the Windows Command Prompt
    Accessing the Windows Command Prompt
  10. At the Windows Command Prompt, type ipconfig /flushdns.  This will purge the DNS Resolver cache.

    Results of ipconfig /flushdns
    Results of ipconfig /flushdns
  11. 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.

3 thoughts on “Setting up virtual hosts with Apache on Windows XP”

Leave a Reply

Your email address will not be published. Required fields are marked *

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