Configuring a Local Server on Windows

I will probably use this post as a reference for myself next time I have to set up a local development environment. It’s a well-learned fact that a properly set-up local server will reduce hiccups in development and ease in the transition to a production server.

XAMPP Post-installation Configuration

I use the XAMPP package on my laptop. This typically comes well configured (e.g. PHP memory limit is 128mb!), but there are a couple things it doesn’t do:

  1. Add the path to php.exe to the environment variables. This lets you run the php executable from the command line, which you might need if you plan on using anything that is scripted in or compiles with php (e.g., the symfony framework).
    To add the path, go to the folder where you installed XAMPP, inside which is a php folder (where php.exe resides). Copy this path (for me it is D:\xampp\php). Then, right-click on (My) Computer, select Properties -> Advanced system settings -> System Properties -> Advanced -> Environment Variables. In the System variables section, find and select Path, then click Edit. Add a semi-colon if there isn’t one at the end of the Variable value list and paste the path to php.exe after it.
  2. Repeat for perl. The perl binary was in D:\xampp\perl\bin for me.
Paths Trouble

I had a lot of trouble getting WordPress to play nice with local paths in Windows (the slashes go the wrong way when using $_SERVER variables or path functions), so next time I will definitely just set up my development server on a Linux virtual machine and carry that around with me. Apache on Cygwin is another option.

Setting up Windows hosts and libraries

For each project in its own domain or subdomain on a production server, I set up my “hosts” file to recognize a local mirror–such as “hokuten.dev” and “davidosomething.dev.” The “hosts” file can be found in C:\Windows\System32\drivers\etc. I’m using Windows 7, on which there is a feature called “Libraries” that collects folders together. The “etc” folder is one of the folders I keep in a library I created called Development.
The hosts file is usually write-protected by default. I turn this off and don’t think of it as a security risk as much as it is a convenience. You can edit the hosts file with any text editor. My file is typically already filled with entries generated by Spybot – Search & Destroy and other programs. These programs use the hosts file to redirect requests to those addresses to localhost, where they are undefined and load nothing as a result. Using the same syntax as those entries, I create a few of my own at the top of the file:

127.0.0.1	albanypdpsi.dev
127.0.0.1	davidosomething.dev
127.0.0.1	hokuten.dev
127.0.0.1	metabook.dev
127.0.0.1	localhost

This basically means “redirect all requests to hokuten.dev to the ip 127.0.0.1.” 127.0.0.1 is the ip your computer sets as its own local ip. So now, if I type hokuten.dev into the browser, I circle back to localhost, where this host name is not recognized.

Setting up Virtual Hosts

Logically, the next step is to make the Apache service recognize this host. To do this, I add a virtual host to the “httpd-vhosts.conf” file located in the D:\xampp\apache\conf\extra folder. I also add D:\xampp\apache\conf to my Development library so I can access the vhosts file easily.
When adding virtual hosts, the name “localhost” stops working. You can turn it back on by adding the following as a virtual host:

<VirtualHost *:80>
ServerName localhost
</VirtualHost>

Here is part of my virtual hosts configuration file:

# Virtual Hosts
NameVirtualHost *:80

################################################################################
# localhost
################################################################################
<VirtualHost *:80>
ServerName localhost
</VirtualHost>

################################################################################
# hokuten.net
################################################################################
<VirtualHost *:80>
	ServerName hokuten.dev
	ServerAlias www.hokuten.dev
	ServerAdmin localhost@hokuten.dev
	DocumentRoot "D:/www/hokuten.net"
	<Directory "D:/www/hokuten.net">
		AllowOverride all
		Allow from all
		Options all
	</Directory>
	ErrorDocument 404 /404.shtml
</VirtualHost>

This is working, so if it doesn’t for you, check the Listen directive in httpd.conf and your firewall settings. Also, I turn off a few settings in httpd.conf, such as index colorization and user directories (see the includes section at the bottom of the file).

Share this article:

  • del.icio.us
  • StumbleUpon
  • Digg
  • Reddit
  • DZone
  • Mixx
  • Twitter
  • email

Leave a Comment

*

*
<pre>, <code>, and <blockquote> allowed.