Open Source

Get Apache 2 and PHP 4 working together in Windows

Installing Apache and PHP on Windows isn't easy, but it doesn't have to be a process of trial and error. Learn the tricks you'll need to get started with our guide.


Peter Brawley and Arthur Fuller

Apache 2 and PHP are popular (and cheap!) choices for creating interactive Web sites. Installing Apache 2 by itself under Windows is easy, but installing PHP 4 so it runs without restrictions with Apache 2 is tricky.

It doesn’t help that the PHP 4.3 manual’s Windows installation section is silent on installing PHP to work with Apache 2, and the section on Apache 2 installation is missing a lot of the information you need. Other published installation guides contain errors as well, leaving some installers to resort to trial-and-error methods, such as replacing PHP’s DLLs with those from another PHP version. But instead of spending hours of frustration trying to figure it all out, you can follow our simple guide through the maze.

Get the right version of Windows
First, a few words on compatibility: Apache 2 does not run on Windows 95, and it runs only marginally on Windows 98, where it cannot run at all as a service. Starting with version 4.3, PHP does not run on Windows 95 either. So, you’re going to need to be working with Windows NT, 2000, or XP before going any further.

Apache 2: Source or binary?
If you happen to have Visual C++ 5.0 or later or Microsoft Visual Studio, you certainly can build Apache from source, but that is absolutely the wrong way to get started with Apache. The build process under Windows is complex even inside the Visual Studio IDE and hair-raising from the command line. So, unless you just enjoy creating makefiles, you are better off getting a Windows Installer binary distribution, also known as an .msi file. By the time you know enough about Apache to know which customizations you need, you may be ready for the Apache build process.

If you have an earlier version of Apache 2 installed and running, bring it down and uninstall it before installing your new server. Multiple versions of Apache 2 cannot coexist.

Double-click the Apache 2 .msi file. After agreeing to the license, you’ll be presented with the dialog box shown in Figure A.

Figure A


Set the Network Domain and Server Name to something appropriate (e.g., localhost works fine assuming you aren’t installing Apache onto a remote computer), enter your e-mail address in the Administrator’s Email Address field, and leave the port 80/service option radio button selected. Selecting a Typical installation in the next dialog will provide a no-hassles working install.

Installation tip
I recommend changing the default installation directory from C:\Program Files\Apache Group to C:\Apache, or something similar that fits the 8.3 filename format. Doing so will keep you from ever having to write quotes around the path to your Apache installation.

After a few moments, the Installation Wizard will tell you that Apache 2 is installed. The only thing left to do is test it by opening your browser and browsing to localhost. You should then see the default Apache 2 Web page.

You can then delete all the sample files in your main Web server directory, which by default is C:\InstallDirectory\Apache\Apache2\htdocs. If you have an Index.html to copy here, do so, or you can create a barebones file for yourself. For the HTML uninitiated, the following code will work fine:
 
<html>
<head>title=default server page</head>
<body>Default server page</body>
</html>
 

Getting Apache 2 ready for PHP
From here on, you need a more hands-on approach than just clicking selections in dialog boxes. Get used to it, it’s the Apache way. You will be editing files to set configuration switches, and if you don’t do things just right, your changes may prevent Apache from loading. If a setting triggers an error, there is a chance that the error will be logged to the Apache 2 error log (by default that’s C:\InstallDir\Apache2\Logs\Error.log). As I said, there’s a chance but not a high likelihood. It is more likely that the error will be logged only to the Windows Event log (Start | Settings | Control Panel | Administrative Tools | Event Viewer).

Unfortunately, the Windows Event log is not a very convenient tool for debugging installation settings. It is much more satisfactory to test Apache server loading in a command line window, where you see error reports right away. So, after making the configuration changes I’m about to discuss, open up a command line window, navigate to Apache’s binary directory (C:\InstallDir\Apache2\bin), and start Apache from there.

The Apache configuration file is C:\Apache\Apache2\Conf\Httpd.conf, and it is editable from your favorite text editor. A search for a setting called DirectoryIndex will bring you to a line that looks like this:
 
DirectoryIndex index.html index.html.var # index.php
 

To enable Apache processing of PHP pages, remove the comment character (#) so that the line reads:
 
DirectoryIndex index.html index.html.var index.php
 

You’ll also need to permit the use of .htaccess files in any directory, so search for an AllowOverride setting and change it from None to All. You’ll probably want to leave the file open in your text editor after saving your changes because you’ll need to edit it again when you install PHP.

Installing PHP
Although you can download PHP’s source, you’ll want to start out with the binaries as you did with Apache. Apache 2 can run PHP programs in two ways: via a CGI interface using Php.exe or internally (and faster) using PHP’s DLLs. So, for every PHP version released, there are two Windows binary packages. The smaller of the two, an .msi package, installs the CGI executable Php.exe but is missing the modules you need to run PHP scripts via Apache DLLs. The larger .zip package contains those, and you’ll want to grab them from the Win32 section of the snaps.php.net Web site. After downloading the file, unzip it to C:\Php, preserving folder names. Unless you just like flying blind, you will also want the PHP manual, which is available in a variety of languages.

Configuring Apache 2 to run PHP4
Now for the fun part: getting Apache and PHP to play nice with each other. First, copy all dlls from C:\Php\Dlls to the Windows system directory. Next, look for the section of Apache’s configuration file (Httpd.conf) that contains a whole bunch of AddType commands, and add this one:
 
AddType application/x-httpd-php .php
 

Copy C:\Php\Php.ini-recommended to your Windows directory, rename it to Php.ini, and open it in a text editor. Edit the lines that set doc_root, extension_dir, and session.save_path so they look exactly like the following, replacing InstallDir with the name of the directory where you previously installed Apache 2:
 
doc_root = c:\apache\apache2\htdocs
extension_dir = c:\php\extensions
session.save_path = c:/temp
 

You can use a forward or backward slash for the session.save_path. The PHP manual says these path arguments need trailing backslashes, but it’s incorrect. In PHP 4.3, that is not the case. You’ll need to create C:\Temp if it doesn’t already exist.

The next step is to enable Apache to run PHP programs as modules, which involves two steps. First, copy C:\Php\Php4ts.dll to your Windows system folder. Look for the LoadModule section in Httpd.conf and add this line:
 
LoadModule php4_module “c:/php/php4apache2.dll”
 

If, for some reason, you need to enable running PHP programs in CGI mode (using Php.exe), then comment out the above line and add these lines to Httpd.conf:
 
ScriptAlias /php/ "c:/php/"
Action application/x-httpd-php "/php/php.exe"
 

Now, does it work?
After you save your changes, you’ll need to answer two questions about your Apache 2/PHP 4 setup to make sure everything works as intended: Does Apache load, and does it process PHP pages correctly?

To find out whether Apache loads, enter this in the command-line window you already opened:
 
apache –k start
 

Alternatively, if Apache is already running, then use the restart command:
 
apache –k restart
 

The advantage of starting Apache from a command line is that if there is an error, Apache will report it immediately. The problem you’re most likely to have if things don’t work will be that Apache cannot load Php4apache2.dll for some reason. If you see Apache complaining with this error, try retracing your steps to ensure that you entered all settings as I described above.

To find out whether Apache is processing PHP pages correctly, use your text editor to create a simple PHP page, called Phptest.php, containing the following line:
 
<? phpinfo(); ?>
 

Save the file to your main Web server directory (C:\InstallDirectory\Apache\Apache2\Htdocs)and then navigate to http://localhost/phptest.php in your browser. If everything is working correctly, you’ll see a long page with a PHP logo that includes a bunch of settings and credits. You’ll be able to tell whether PHP is running through CGI or inside Apache by checking the environment variable orig_script_name. If PHP is running through CGI, this variable will be /Php/Php.exe. If Apache is running the PHP script directly as modules, this variable will read /Phptest.php.

Although installing Apache 2 and PHP on Windows isn’t necessarily easy, it doesn’t have to be a process of trial and error. With these instructions, you'll be well on your way to enjoying one of the best, and certainly cheapest, Web development combinations available to developers today.

Editor's Picks