I’ve had a reasonable amount of experience setting up and using the Apache Web Server on Linux machines (almost always Debian boxes), but a couple of weeks back I wanted to set it up locally as a dev environment. As I run Windows, this involved a few changes in procedure. I felt it was worth outlining the procedure for anyone else who might find themselves in the same boat.
First off, you need to grab the latest stable binary from http://httpd.apache.org/download.cgi (at the time of writing, it’s version 2.2 that you want, specifically 2.2.16). I chose to get the one which includes OpenSSL (which you’ll need if you want to test serving secure content). Once you’ve got that, run through the install wizard. In terms of which options to choose during it, I went with installing the server as a service so it started up with Windows. Also, I chose to customise the install and selected to install all features (as opposed to taking just the typical ones). This is almost certainly unnecessary, but I did it just to make sure I wasn’t missing anything I needed.
Something to note throughout this guide is that after any change to the config files you must reload the server. The installer will have added an icon to your notification area which will allow you to do this.
The config files are located in <your_install_directory)conf. In some areas, the config files dependant on your installation directory, which can differ (for instance, if using a 32 bit operating system, you won’t have things installed in “Program Files (x86)”).
Notation used throughout the guide
The use of “->” denotes before and after of each line changed (before on the left, after on the right).Anything which is for the user to decide is given between angular brackets. In code blocks, “#” is used for any comments (just as it is within the config files).
To get Apache just serving content from its htdocs folder (which is located in the installation folder) requires the least configuration. To achieve this, just make the following changes to the defaults in the config file.
Listen 80 -> Listen <Number Of Port To Run Web Server On>
#ServerName localhost:80 -> ServerName localhost:<Number Of Port To Run Web Server On>
At this point, you should be able to navigate to http://localhost:<Number Of Port To Run Web Server On> and see a page confirming that you’ve set it up correctly (or your own index.html if you’ve replaced the one that’s in the htdocs folder by default).
Add setup for user directories
Adding the necessary config for user directories means that each user account you have on your machine will have a directory that they can place files in to be served by Apache.
#LoadModule userdir_module modules/mod_userdir.so -> LoadModule userdir_module modules/mod_userdir.so
#Include conf/extra/httpd-userdir.conf -> Include conf/extra/httpd-userdir.conf
You should now be able to navigate to http://localhost:<Number Of Port To Run Web Server On>/~<user>/ and view any content that the user places in their website directory (by default “My DocumentsMy Website”). You can change which directories are used as user directories by editing confextrahttpd-userdir.conf in the Apache installation folder.
Add setup for virtual hosts (also known as vhosts)
Virtual hosts are more useful in a development (and in most production contexts) context than user directories. They allow you to specify a folder which Apache should serve from when receiving a request on a given domain/subdomain. They also allow you to specify some settings specific to that virtual host, such as a custom log file to use (rather than just putting everything in the default log file).
#LoadModule vhost_alias_module modules/mod_vhost_alias.so -> LoadModule vhost_alias_module modules/mod_vhost_alias.so
#Include conf/extra/httpd-vhosts.conf -> Include conf/extra/httpd-vhosts.conf
NameVirtualHost *:80 -> NameVirtualHost *:<Number Of Port To Run Web Server On>
# Delete the two existing VirtualHost entries, and add your own following this template.
<VirtualHost *:<Number Of Port To Run Web Server On>>
Options Indexes FollowSymLinks Includes ExecCGI
Allow from all
CustomLog "logs/<subdomain>.localhost-access.log" common
One thing to note with a vhost, is that you’ll need to add an entry to your hosts file for the each subdomain you use, so that the domain still resolves to localhost (at least, I found that without this, it the domains didn’t resolve). To do this, simply open C:WindowsSystem32driversetchosts and add a line that looks like:
With that done, you should have it all working, and should be able to visit any of your virtual host sites at http://<subdomain>.localhost:<Number Of Port To Run Web Server On>/. If it’s not, please leave a comment as I could well have missed something crucial out (though I think I’ve covered everything, as this guide was produced by diffing my working config files against the original ones).
Things to watch out for
- In Apache’s config files, forward slashes are used in paths, as opposed to the backslashes you’d usually use on Windows.
- You’re probably best running this on something other than port 80. One thing that many people get caught out by is trying to do this on a machine which also has Skype running on it (not too unlikely on a home dev machine), which it turns out binds to port 80. To work around this, either disable this option within Skype (I recommend doing this), or run the web server on a different port. The option to disable is in Tools -> Options -> Advanced -> Connection -> Untick the checkbox “Use port 80 and 443 as alternatives for incoming connections”.
- Make sure you reload/restart the server after any config file change. This is a required step, and without it your changes will be ignored.