Creating additional testing sites in Apache, on a local Windows computer

It may happen that you'd like to test multiple sites on one machine. There's a number of ways to do this. Following our previous tutorial, Installing Apache to a Windows-based computer, locally, we can either dump additional folders in our Apache root, or we can create additional subdomains under localhost.

That is, Google has a number of different subdomains. News.google.com, mail.google.com, and regular old www.google.com. With an Apache and Windows change, we can replicate this as well.

First, let's open up our Apache configuration file. If you followed the steps in the Installing Apache to a Windows-based computer, locally guide, you can find this file at C:\Program Files\Apache Group\Apache\conf\httpd.conf, or via the Start menu (detailed in the previous article).

We're also going to open the Windows hosts file, in Notepad, at C:\WINDOWS\system32\drivers\etc\hosts Note that there is no extension for this file, it's just "hosts". If you're like most people, this is going to be pretty empty, perhaps only with the following line uncommented (a # at the beginning of a line is typically a comment):

127.0.0.1 localhost

Lastly, open up Windows Explorer and navigate to our Web site folder. Again, following the previous guide, this would be C:\home\website\public_html\. If an index.html file exists, open it in Notepad. If one doesn't exist, open Notepad and paste or type the following. If the file exists, add only the bold text.

<html>
<head>
<title>website</title>
</head>

<body>
<p>Hello world!</p>
</body>
</html>

Save the file if it already existed, or save the file as index.html (in the C:\home\website\public_html\ folder). Close this document and go back to C:\home\. Make a copy of the website folder, and rename the copy to website2. Go into the website2\public_html folder and open the index.html file in Notepad. Change “website” to “website2”. We now have two websites, with a noticeable difference.

In the httpd.conf file, scroll all the way to the bottom of the file. You'll notice there's a section on VirtualHost, which is what we're going to change here. Add the following additional lines.

<virtualhost 127.0.0.1>
ServerName website.localhost
DocumentRoot C:\home\website\public_html
</virtualhost>

<virtualhost 127.0.0.2>
ServerName website2.localhost
DocumentRoot C:\home\website2\public_html
</virtualhost>

Save the changes and switch to the hosts file. We're now going to change the one uncommented line, and add an additional line.

127.0.0.1 localhost website.localhost
127.0.0.2 website2.localhost

Note that the spaces are tabs. Save the changes to this file as well.

Now, we're going to restart Apache by using the Services Control Panel (under Administrative Tools). Once Apache has restarted, we're going to go to the following three sites, one after the other. Note that the title of the page has changed, even though the text on the page is the same. For the first two sites, the title should read website, while the third will read website2. http://localhost/, http://website.localhost/, http://website2.localhost/.

Note that this may not make the most sense. In this case, we've set website to be our fall-back position, or our core Web site. It may make more sense to point localhost towards C:\home\. This way, we can add additional Web sites to the C:\home\ folder and not have to setup additional VirtualHost entries. Since this makes more sense to me, we're going to go ahead and change this.

First, in the httpd.conf file, search for DocumentRoot. We're going to change the line with the second occurrence from:

DocumentRoot "C:/home/website/public_html"

to:

DocumentRoot "C:/home"

Scrolling down a bit, we're also going to change the following line:

<directory "C:/home/website/public_html">

to:

<directory "C:/home">

At the end of this same file, we're going to change the new lines we added. Change these two to:

<virtualhost 127.0.0.2>
ServerName website.localhost
DocumentRoot C:\home\website\public_html
</virtualhost>

<virtualhost 127.0.0.3>
ServerName website2.localhost
DocumentRoot C:\home\website2\public_html
</virtualhost>

In the hosts file, replace the two uncommented lines with:

127.0.0.1 localhost
127.0.0.2 website.localhost
127.0.0.3 website2.localhost

Finally, restart Apache, and hit the three URLs again - http://localhost/, http://website.localhost/, http://website2.localhost/. If we hit the first URL, we'll be presented with a directory listing, which you can click through to see the individual sites. If we hit the last two URLs, we'll see each of the site's index.html files.

Now that we've gone through it step-by-step, here's the short version of what we would need to do to add additional Web sites.

  1. Create the new folder(s) in the C:\home\ folder.
  2. Open the httpd.conf file and add a new VirtualHost for the new site.
  3. Open the hosts file and add a new IP mapping for the VirtualHost.
  4. Save all files and restart Apache.

For ease, you could create shortcuts in your C:\home\ folder for each of these three items – the httpd.conf file, the hosts file, and the Services control panel.