Creating Local Domains
This guide will show you how to create a domain on your local machine and configure apache to route traffic to that domain to a particular directory. I’ll describe how to do this using Windows, but the process should be very similar in a linux environment.
The first step is to choose a domain name. When choosing a domain you should remember two things: It will only exist on your local machine and it will override an already existing domain. The latter is probably the most important; choosing google.com as the domain will prevent you from accessing google.com. The simplest way to avoid overriding a domain is to leave off the TLD. Personally I use subdomains of localhost. For example, if I had a project Foo that I wanted to create a domain for I’d choose foo.localhost.
The next step is to associate the domain with your loopback address (almost always 127.0.0.1 the IPv6 version is ::1). To do this you need to locate and edit your hosts file. In Windows it should be located in the directory C:\Windows\System32\drivers\etc If you are using Vista or 7 you won’t be able to edit it with Administrative Privileges. You’ll need to run whatever application you are using to edit it (notepad will do) as an administrator. To do so in notepad follow these steps:
- In your
Startmenu navigate toAll Programs > Accessories - Locate and right-click Notepad
- Click
Run as administrator - Once in Notepad select
File > Open... - Navigate to
C:\Windows\System32\drivers\etcand select the filehosts
Your hosts file should look something like this:
127.0.0.1 localhost
::1 localhost
If the domain you chose if foo.localhost then just add the entry:
127.0.0.1 foo.localhost
After you save the file the domain foo.localhost should route to 127.0.01. For now it is effectively identical to just using localhost.
It is up to apache to route traffic to foo.localhost to the right location. This process may vary depending on your configuration, but at this point it is identical to configuring a virtual domain on your server. The first step is to create a virtual host. Locate your httpd.conf file, it should be in the conf directory wherever apache is installed. If you haven’t already set up a virtual host you’ll need to add the line:
NameVirtualHost *
You’ll then need to setup a virtual host for each domain, here is an example:
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@foo.localhost
DocumentRoot "C:\srv\foo\public-html"
ServerName foo.localhost
</VirtualHost>
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot "C:\srv\public-html"
ServerName localhost
</VirtualHost>
If you entered your new domain into your browser now you may get a 503 error (forbidden). To fix this problem you’ll need to tell apache to allow access to directory you specified in the DocumentRoot entry in your virtual host:
<Directory "C:/srv">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Thats it! Your domain should be setup and ready to use.
