owen

Every so often, you want to see how some code will look on your server before you actually deploy it there. Without setting up an identical server at identical cost elsewhere, you might consider testing locally on your desktop computer.

This setup describes what I’ve found to be useful, which is a system that allows you to host multiple sites on your local system, all resolving to different domain names, and configurable by adding a simple single line to a file and creating the directory to host the site. You don’t even need to restart Apache to configure a whole new virtual host with this method.

The steps to do this are pretty simple, and all of the software is freely available. I recommend PSPad as a free text editor, which has a cool feature to let me mark these files as “favorites” to open them quickly without having to browse for them. You could also use Notepad, if you enjoy your own pain.

Download and install xampp
Xampp is a package that installs Apache, MySQL, and PHP on your Windows system. I usually install the Lite version, because it has exactly what I want, and is very easily cleaned up (no registry entries added). Follow the instructions in the distribution to get it working correctly. You should be able to connect in your browser to http://localhost when you’re done and actually see the xampp installation. If you’ve gotten that far (and getting you there is the content of the xampp install instructions, not this tutorial), then you can proceed.

Create a vhost directory
You don’t need to do this step to have a working testing server. Technically, after you install xampp, you’re done. I like setting up local vhosts because it lets me host multiple projects very easily all on my local system, and when the configuration is done, it’s stupid-easy to use and really handy.

If you want to, you can use the existing htdocs directory inside the xampp install to be the vhost directory. What we’re going to do is create a separate directory inside that directory for each site/project that we want to host locally.

As long as you know the directory you want to use to hold all of the subdirectories, we’ll move on…

Add mod_vhost_alias to the Apache config
mod_vhost_alias lets us dynamically host virtual hosts without having to change the httpd.conf every time. We’ll configure Apache to look in the directory above for a directory that matches the name of the requested host. So if you try to load “www.mysite.com”, then Apache will look in htdocs/www.mysite.com for the files to serve for that site.

To configure this, first load up your http.conf in a text editor. You can find this file in xampp at {xamppdir}/apache/conf/httpd.conf

Make sure that the mod_vhost_alias module is loaded. Look for this line:
LoadModule vhost_alias_module modules/mod_vhost_alias.so

It should not have a hash (#) in front of it. Save the change if necessary.

Also, look for this line in your httpd.conf:
Include conf/extra/httpd-vhosts.conf

This should also be enabled, as we will be editing and using that file.

Load your httpd-vhost.conf file. You can find this file in xampp at {xamppdir}/apache/conf/extra/httpd-vhost.conf

You can leave commented out every line in this file, which is the default. Somewhere in that file, add these lines:
NameVirtualHost *:80
UseCanonicalName Off
VirtualDocumentRoot “path_here/%0”

Replace path_here with the path that you’ve chosen in the prior step. Be sure that there is a slash between the path and the %0 so that Apache doesn’t get confused.

If you’re not going to use the htdocs directory that is the default location, then you’ll need to make sure that you have a <Directory> directive that allows Apache to access the files at that location, since xampp wisely denies access to everywhere by default. Such a directive looks like this:

<Directory “path_here">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Save your changes to that file, and restart Apache.

Create your vhost directory and HOSTS info

This is the cool part. First, imagine what you want your project name to be. Say it’s “foo”. In the htdocs directory (or wherever you have configured Apache to point), create a directory named “foo”.

Now, open your HOSTS file in a text editor. Your HOSTS file is usually located at C:\windows\system32\drivers\etc\hosts

If you haven’t edited that file or used some crazy anti-adware software that edits this file, it should be pretty plain. What you want to do is add a new line to the end of the file, like this:

127.0.0.1 foo

This tells windows that when you request the domain “foo”, it should return the IP address 127.0.0.1. 127.0.0.1 is your computer. So when you request the URL “http://foo” Windows should send your request to your locally installed Apache server. And since Apache is now configured to serve requests for virtual hosts out of directories that have the same name as those virtual hosts, you should be all set to create as many new virtual hosts as you want on your own Windows PC and access them indiscriminately.

Whenever you want to add a new “site” to your system, add a directory to the htdocs directory using the name of the domain you want to use, then add that domain to your HOSTS file with the 127.0.0.1 IP address. You should not even need to restart Apache, it should just start resolving the domain to your local Apache instance, and Apache will find the appropriate directory to serve files from.

If you change the HOSTS file often within a single coding session, your PC might get a little hung up by caching this value. You can usually free up the DNS cache by running this on the command line:

ipconfig /flushdns

If you temporarily want to use your own desktop server as a staging site for your remote server, you can temporarily add the domain of your remote server to your HOSTS file with the 127.0.0.1 address.

With the HOSTS file, you could even point some domain at a different IP address than what it normally resolves to, which is really helpful when you’re moving your site to a different host. I currently have this setting set for Asymptomatic, since I’m in the midst of transferring the site to a new server. Note how easily I can switch between them and still use the same domain on both, even though one IP is not yet public.

Every system I know of (windows, linux, mac) all have a HOSTS file, it’s just a matter of finding it and tweaking it to make it do what you need.

And that’s the process. Enjoy!