Create an Apache Testing Server on Windows

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 xamppXampp 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 directoryYou 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 configmod_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!


7 Responses to Create an Apache Testing Server on Windows

  1. Diana from dianahiggins.com 2007-03-17 06:39:05

    Thank you so much for this! I know, I'm the last person you would expect to get this response from, but a friend is trying to teach my son some of this stuff and the first step was for me to install Apache, MySQL, and PHP. I managed to download them but that's as far as I got. Now I think I can complete it. :)

  2. Paul 2007-06-19 23:31:52

    One of the worst things you can do is install a "package" built by someone else. If you are going to run a testing server on your local computer, then download and configure Apache, PHP and MySQL yourself from their respective sites, guaranteeing that you get the latest version or that you get a version that will be most compatible with your production website.

    There sre few things more frustrating than having some code work fine on your testing site and then break on your production site, especially if you assume that everything is fine in production, because "it worked on my test site" and go on your merry way making further changes and complicating the debugging process later on.

    You will learn a lot about the server(s) you test and host your websites on, and this is a good thing if you are a web developer. For instance, adding a virtual host can be done the "easy way" that you describe, or the proper (and not really any more difficult) way by editing the httpd.conf file, which gives you much more flexibility in configuration to boot.

    If you are using a good design tool like Dreamweaver, you don't even need to play musical chairs with your hosts files, because it will configure and use the testing and production servers without any shennanigans.

    If you do require the shannanigans, and have edited your hosts file, such as by adding the very good and, to myself, indispensible configuration here:

    http://www.mvps.org/winhelp2002/hosts.htm

    (which needless to say tells you how to configure it on several Windows OS'es - if you are using linux just add it to /etc/hosts)

    then you should add the shennanigans after the localhost line;

    127.0.0.1 localhost.localdomain localhost

    and before any of the MVP stuff (or whatever you prefer) or other lines you have added manually to your hosts file, to make it run quicker.

  3. Joe from www.hampshiregoalball.com 2007-06-23 19:20:07

    i have followed the instructions but when i try to run http://hga i get
    Object not found!
    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

    If you think this is a server error, please contact the webmaster.

    Error 404
    hga
    06/23/07 20:17:31
    Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/4.4.5

    and the apache_start.bat says

    [sat jun 23 20:18:17 2007] [warn] NameVirtualHost *:80 has no VirtualHosts
    what do i do web.designer@hampshiregoalball.com

  4. owen from asymptomatic.net 2007-06-25 00:59:53

    @Paul: I think it can be helpful to learn by installing each package separately, but if your goal is simply to get a local testing environment up and running for development use with a separate production system, then a pre-packaged setup can be very beneficial.

    Also, I wouldn't go using anyone else's HOSTS file without knowing explicitly what it does. The one you specified is completely unnecessary. All it does is redirect the domains of some advertising sites to your local server. This can actually be detrimental if you are running a real server locally. You really shouldn't do this.

    Get some real software to accomplish that task, like the anti-adware plugins for Firefox or one of the many commercial proxy packages available from reputable companies.

    @Joe: It looks like your server is configured to serve files out of a directory that has no files in it, or perhaps you have not created the directory required ("hga") to serve files for your requested domain. Try creating that directory and putting some files in it.

  5. Frank 2007-07-04 17:38:12

    Hi, I'm trying to set myself up along the lines described here, but have a question before I begin the process (I have xampp already).

    Do the various 'sites' HAVE to be put in folders within 'htdocs'? Or can they be referred to in their (in my case) original locations. If the latter, are there any issues I need to be aware of relating to folder names, addresses, spaces, etc.?

    I only ask this as the htdocs folder is full of other stuff and is itself in a rather full location. I'd prefer to keep my site files/folders in their original uncluttered locations.

    Question in reposnse to part of Paul's comments: I have started using Dreamweaver so would be really keen to know in detail how that can help me. Could it avoid me having to set up all the rest of this stuff?

    Can you tell I'm new to 'test servers'?
    Any advice greatly appreciated!

  6. owen from asymptomatic.net 2007-07-07 15:48:40

    You don't have to set up the various sites inside of htdocs, but then you can't use mod_vhost_alias. mod_vhost_alias makes the Apache-side configuration convenient. If your directories all exist already, then you either need to move them into htdocs or set up individual VirtualHost entries in the Apache config for each site you want to serve.

    You can have spaces in the path name when setting up a vhost or vhost_alias, just be sure that you put the whole thing in quotes when you set it in the config.

    Dreamweaver will publish your site to a staging server for you for testing purposes. It's not as full-featured as keeping your development environment on a living server, and depending on the configuration (which I've always found confusing and not as useful as knowing how to configure Apache, generally) it might not put all of the required files for your web app on the staging server automatically. It definitely won't inform you if you're missing PHP requirements like DOM or PEAR, which you would know immediately by looking at your server logs.

  7. Molok from www.joomlamania.org 2007-10-19 16:43:43

    The HOSTS info saved my day!

    Thanks a lot!

    cheers

5