Setting Up Domains for Local Projects on Windows Apache
Using the Apache VirtualHost directive and Windows' hosts file to establish domain names for local projects.
Tagged with Domains, Apache, WAMP, Windows and Web Development
Posted on 27/5/07 by Paul Herron
DocumentRoot in httpd.conf:
- # DocumentRoot: The directory out of which you will serve your
- # documents. By default, all requests are taken from this directory, but
- # symbolic links and aliases may be used to point to other locations.
- #
- DocumentRoot "C:\Design\myproject"
That directive, after restarting Apache, would make the contents of myproject available at http://localhost/. This is fine for one project, but accessing the contents of a different project directory - say, C:\Design\myotherproject - as if it were the document root would require changing the config file and restarting Apache.
VirtualHost Saves the Day
In httpd.conf, we can first use the NameVirtualHost directive to specify the IP to which name-based requests will resolve. As we're working locally, this should be the loopback IP address, 127.0.0.1. We then give a series of VirtualHost directives to specify the domain name and corresponding directory for each project.
- NameVirtualHost 127.0.0.1
- <VirtualHost 127.0.0.1>
- DocumentRoot "C:/Design/myproject"
- ServerName myproject.l
- </VirtualHost>
- <VirtualHost 127.0.0.1>
- DocumentRoot "C:/Design/myotherproject"
- ServerName myotherproject.l
- </VirtualHost>
We now need to take care of the DNS part of this process. Requests for any of our test domains should map to 127.0.0.1, and this can be enforced in the Windows hosts file, usually available at C:\WINDOWS\system32\drivers\etc\hosts.
- # Copyright (c) 1993-1999 Microsoft Corp.
- #
- # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
- #
- # This file contains the mappings of IP addresses to host names. Each
- # entry should be kept on an individual line. The IP address should
- # be placed in the first column followed by the corresponding host name.
- # The IP address and the host name should be separated by at least one
- # space.
- #
- # Additionally, comments (such as these) may be inserted on individual
- # lines or following the machine name denoted by a '#' symbol.
- #
- # For example:
- #
- # 102.54.94.97 rhino.acme.com # source server
- # 38.25.63.10 x.acme.com # x client host
- 127.0.0.1 localhost
- 127.0.0.1 myproject.l
- 127.0.0.1 myotherproject.l
All being well, this setup would make the two example projects available at http://myproject.l and http://myotherproject.l.
Apache will need a restart for the changes to take effect. It might also be necessary to restart Windows' DNS Client service, which can be found by right-clicking My Computer, selecting Manage and navigating to the Services section under Services and Applications.
Comments
Leave a Comment
Article Tags
Show all articles, or just those tagged as:
Apache (1)
CakePHP (5)
Domains (1)
Ethics and That (1)
Freeware (1)
Open Source (1)
Servage (1)
SMS (1)
Software (1)
WAMP (1)
Web Development (6)
Windows (2)
Feed
The articles RSS feed is available here.
Elsewhom
-
RichardHerring.com.
Home of the comedian and online journal-keeper -
Mi Blog.
CakePHP-related articles, downloads and demonstrations -
The Tech.
MIT's Student Newspaper -
mattheaton.com.
Bluehost Blog
Matt is CEO of Bluehost, a successful web hosting provider

pierre wrote on 1/6/07:
Thanks Paul, it now works on my local machine !!
cheers