Setup a local Apache server with virtual hosts for PHP development on Windows

This micro-tutorial covers downloading and installing Apache HTTP server and PHP and running it locally with virtual hosts on Windows.  It’s quite straight forward for the experienced user but putting the pieces together for the beginner can sometimes be difficult.  This is a high level tutorial aimed at those newbies so that they can get started.

Shortcuts for this tute:

Keep in mind that Apache is not the “official best practice” web server for PHP when running on windows.  More on this below.  However this is usually advice given for production environments.  I’ll assume that you’re using Apache locally on Windows because it’s your development environment and you’re trying to emulate what you can of your (linux) production environment.

Step 1: Get Apache HTTP Server

Go to the Apache download page and find the appropriate flavor of install for you.  For me it’s the latest recommended version of Apache (2.2.17 at the time of writing).  Install Apache using the point and click interface.  I usually just install it recommends to my “Program Files” directory”.

Step 2: Download and install PHP

I’m using Windows so I install my binaries from http://windows.php.net/download.  There are 2 schools of thought here as you can install PHP using the MSI installer or you can download the zip file and extract it and hook it up to Apache manually.  The ZIP download invariably contains more extension DLL’s included than the MSI.  Setting up PHP from the extracted ZIP file is quite straight forward but for speed let’s use the MSI installer as it’s a nice point and click interface that does most of what you need.

Since we’re using Apache, PHP recommends that we download the VC6 Thread Safe version of the installer.  VC6 is the legacy Visual Studio 6 compiler whereas the newer VC9 (Visual Studio 9)  compiler.  The VC9 compiled versions of PHP have some performance benefits but are not available for the Apache compatible version of the MSI installer.  However for development purposes this probably isn’t going to be a grave concern.  The Installer gives you the option to select your web server and installs the appropriate Apache settings to run PHP.  Just follow the prompts.

In your Apache http.conf configuration file you’ll now see the following lines:

#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/Program Files/PHP/"
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL

PHP on Windows trivia tangent.  Microsoft has traditionally held an adversarial position when it came to PHP and open source in general.  There were many reasons for this, not least of which was that PHP provided strong competition to its own web language ASP .Net.  However as of a few years ago in 2006 Microsoft actively sought out a partnership with Zend/PHP along with other open source projects.  The aim of all of this was to try and create the most stable and efficient PHP binaries for the Windows platform that it could.  The payoff and benefit for Microsoft was of course is that it opened the door for an enormous PHP community to start using Windows as a production platform.

Soon after this partnership was formed it was announced that a singular “best practice” platform for PHP on Windows would be supported and actively enhanced.  This platform was the IIS (Internet Information Services) and Fast CGI installation combination (currently the VC9 Non-Thread-Safe binaries).  Back to the present day and 4 years later the PHP + Windows combo is an extremely stable and high performance production platform (especially with the addition of Microsofts byte code caching system WinCache).  I may blog about high performance PHP on Windows in the future as I’ve been working on some high traffic websites lately using this application stack.  See http://php.iis.net/ and the PHP on Windows Showcase for more information about PHP on Windows.

Tip: An alternative to downloading and installing vanilla versions of Apache + PHP is of course to download the Zend Server Community Edition.  To be honest this is probably the easiest way to get up and running as a developer.  It also has a very sweet benchmarking tool included and you can still configure Apache and your hosts as described below.  Another option is the popular XAMPP bundle. But let’s stick to the vanilla versions for this discussion.

If you need more information on installing PHP on Windows then go here.

Step 3: Setup your website

This depends on whether you want to keep everything under the traditional “htdocs” folder within the Apache installation. Personally since I’m on my own PC I keep all of my websites together on a different drive from where my Windows install lives.  I have somthing like:

  • D:\Webroot\<insert website name here>

I always use the domain as the top level folder name.  So for example:

  • D:\Webroot\blog.swink.com.au

Step 4: Configure Apache

Apache is pretty cool and it’s not that hard to work with for the basic stuff.  Some people shy away from the non GUI interface, but once you’re familiar with the http.conf it becomes faster to make changes.  The ability to understand and edit this configuration file opens up a plethora of tools and resources for any web developer. So locate your Apache configuration file (called http.conf) and open it.  Try it in an editor for syntax highlighting as this always helps readability.  For me this live at:

  • C:\Program Files\Apache Software Foundation\Apache2.2\conf\http.conf

First we need to tell Apache that the document root for our website live somewhere other than it’s default “htdocs” location.  Search for the term “DocumentRoot” in the http.conf.  You should find a line like the below:

DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"

Change this to your chosen location (if it’s differnt) for your (multi website) webroot.  For me that’s:

DocumentRoot "D:/WebRoot"

Secondly you’re probably going to want a few Apache modules enabled.  For me that’s at least mod_rewrite (for Zend Framework).  Search for “mod_rewrite” in the http.conf.  Remove the hash (#) from the start of the line, that is, uncomment it.

LoadModule rewrite_module modules/mod_rewrite.so

Third you’ll need to add index.php to be a recognised directory index. This means that if someone browses to a folder on your website and you have an index.php in the directory then Apache will know to serve this up automatically.  Search for “DirectoryIndex” and add index.php as an index like so:

DirectoryIndex index.html index.php

Note that the order is important and Apache will serve up the first one that is found from left to right.

The next step is to add one or more virtual host entries to your http.conf configuration file. Think of each of these as a group of encapsulated config entries for each website.  By and large any config values that you put inside the <VirtualHost></VirtualHost> tags will override the global settings in the main part of the http.conf file.  You can add a plethora of config entries, but I’m just going to add some simple ones.   Add them at the end of the http.conf file:

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
 
<VirtualHost *:80>
 
	ServerName blog.swink.local
	DocumentRoot "F:\WebRoot\blog.swink.com.au\public"
	ErrorLog "F:\WebRoot\blog.swink.com.au\logs\apache_error.log"
 
	<Directory "F:\WebRoot\blog.swink.com.au\public">
		Allow from all		
		Options +Indexes	
		AllowOverride All	
	</Directory>
 
</VirtualHost>

Now to apply the changes you’ll need to restart Apache.  This is super easy in Windows as it comes with a handy system tray application.  Simply click it and select the restart option.  If this fails don’t forget to check the Apache error log at:

  • C:\Program Files\Apache Software Foundation\Apache2.2\logs

See below for the Apache monitor tool:

Step 5: Setup your DNS

Windows has a local DNS (Domain Name System) override file that can be used to intercept requests to certain DNS entries and allows you to route them to somewhere.  In our case we want to route them to our local development server.  The file is called a “hosts” file (no extension). The location of the hosts file will vary very slightly depending on the flavor of Windows that you have installed.  However it will always be “%windows-dir%\System32\drivers\etc”.  For me on Windows 7 it’s:

  • C:\Windows\System32\drivers\etc\hosts

Open the hosts file with the text editor of your choice.  For me that’s Notepad++.  Keep in mind that this is a very important file that is used by Windows, so try not to delete it or make terrible mistakes.  You may find on some machines that it requires you to edit it as an administrator or it may be uneditable due to your uer permissions. You should see few lines in there already withe something like:

127.0.0.1       localhost

The format is easy, it’s:  <IP Address> <space(s) or tab> <DNS Name><line break> So I’m going to setup my blog locally and use:

127.0.0.1     blog.swink.local

This essentially says to the computer, “if I get a request for blog.swink.local then route the request to the IP address 127.0.0.1″.  The DNS name that you choose should not exist in the real world.  If it does then you’ll never be able to get to the external website as this override file will always point it to your local machine at 127.0.0.1!  I like to keep the name similar but also different.  Autocomplete rules.  Save the hosts file and exit.

Step 6: Test your installation

The easiest way to test your installation is to add a fresh new PHP index file in your website directory.  I always drop a “PHP Info” file into the new directory to test it.  This is simply a PHP file with one line of code that calls the build in “phpinfo()” function.  The result is a nicely formatted web page that contains a huge quantity of information about your PHP installation and it’s currently configured properties.  The code is:

echo phpinfo()

The result is something like:

Step 7: Make handy shortcuts to your config files

Finally I like to stay organized -and- I’m a lazy programmer. To stop me spending 15 seconds navigating directories I usually create a shortcut to my php.ini, hosts and http.conf files and put them in my global document root (D:\webroot).  This means that I can get to my essentials at any time with minimal effort. And thats it! Craete Your This should be enough to get you on the road to success.

Comments are closed.