Blog Post RSS ?

Blogs » Web Tech » How to Test Multiple Websites on One PC With Apache Virtual Hosts
 

How to Test Multiple Websites on One PC With Apache Virtual Hosts

by Craig Buckler

Run multiple sitesIt is rare to find a web developer with responsibility for just one website. In this article, we will configure your development PC so you can test any number of websites using a dedicated domain name for each one. You will require a local installation of Apache 2.2 and, optionally, PHP and MySQL.

The Heavenly Hosts File

When you enter an address in your browser, the domain name is normally converted to an IP address by a domain name server. Just prior to that, the computer will check its own hosts file. Host files are available on nearly every operating system:

  • Windows NT, 2000, XP, 2003 and Vista: %WinDir%\system32\drivers\etc\hosts (note this location can be changed in the registry key \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DataBasePath)
  • Windows 95, 98, ME: %WinDir%\hosts
  • Linux, Unix, BSD: /etc/hosts
  • Mac OS X: /private/etc/hosts

In most cases, your hosts files will contain a single entry:


127.0.0.1  localhost

Entering ‘http://localhost/’ in your browser will resolve to the TCP/IP loop-back address 127.0.0.1 (your PC).

The hosts file can contain any number of domain mappings. Assume we want to test two sites locally: www.mysite1.com and www.mysite2.org. We can create two domains in our hosts file for testing purposes, e.g. mysite1 and mysite2:


127.0.0.1  localhost
127.0.0.1  mysite1
127.0.0.1  mysite2

Some operating systems will implement the change as soon as the hosts file is saved. Windows users should enter “nbtstat -R” at the command line. Try a reboot if all else fails.

Configuring Apache Virtual Hosts

Apache can run any number of websites on a single machine. (Note: so can the server versions of Microsoft IIS, but not the Windows Professional or VisualStudio.NET editions).

Shut down Apache and load its configuration file, \conf\httpd.conf, in a text editor. Assuming we want the domain mysite1 to use the files in D:\WebPages\mysite1 and mysite2 to use the files in D:\WebPages\mysite2, we would add the following Virtual Host definitions to the bottom of the file:


# Virtual hosts
NameVirtualHost *:80

# Any unspecified domain
<VirtualHost *:80>
	DocumentRoot D:/WebPages
</VirtualHost>

# mysite1 domain
<VirtualHost *:80>
	ServerName mysite1
	DocumentRoot D:/WebPages/mysite1
</VirtualHost>

# mysite2 domain
<VirtualHost *:80>
	ServerName mysite2
	DocumentRoot D:/WebPages/mysite2
</VirtualHost>

Save the file and restart Apache.

Testing Your Sites

Copy your website files to D:\WebPages\mysite1 and D:\WebPages\mysite2 accordingly. You can now enter either http://mysite1/ or http://mysite2/ in your browser to test the appropriate site.

Bonus tip for PHP coders
It can be useful to know whether the site is running on the live or development environment, e.g. debugging messages are not shown on the live site. You can detect the domain using code such as:


<?php
$debug == ($_SERVER['HTTP_HOST'] == 'mysite1');
if ($debug) echo "debug message";
?>

See also:

Do you have any other tips for testing multiple websites on a single PC?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Ping.fm
  • Twitthis

Related posts:

  1. How to Install Apache Web Server on Windows Professional web developers need a web server and Apache is...
  2. How to Install PHP on Windows In his final installation tutorial, Craig provides a step-by-step guide...
  3. How to Build the Best Browser Test Suite Professional web developers need to test their sites in multiple...
  4. Build Your Own Dev Server with VirtualBox What's the best way to test your web site on...
  5. How to Install PHP 5.3 on Windows PHP 5.3 is the most significant update since version 5.0....

This post has 23 responses so far

Sponsored Links

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.

Follow SitePoint on...