It 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%system32driversetchosts (note this location can be changed in the registry key HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpipParametersDataBasePath)
- 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:
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, confhttpd.conf, in a text editor. Assuming we want the domain mysite1 to use the files in D:WebPagesmysite1 and mysite2 to use the files in D:WebPagesmysite2, 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:WebPagesmysite1 and D:WebPagesmysite2 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"; ?>
Do you have any other tips for testing multiple websites on a single PC?
Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.
The Principles of Beautiful Web Design, 4th Edition
Learn PHP in One Day and Learn It Well
Docker for Web Developers