Step-by-Step: How to Configure Apache (for Windows) for PHP
December 28, 2003


This guide assumes that you have already installed PHP on your system.  If you have not yet done so, please take a look at this guide: Installing PHP on a Windows Webserver

Now that you've installed PHP on your server that is running Apache, you'll need to configure Apache so that it can utilize PHP properly.  In this example, we'll be configuring PHP on Apache 2.0.

First thing to do is to stop Apache.

Go to "Start" --> "Programs" --> "Apache HTTP Server" --> "Control Apache Server" --> "Stop".

You should get a message that Apache is stopping.

We now need to edit a file called "httpd.conf".  This file is located at "C:\Program Files\Apache Group\Apache2\conf".  Use any text editor such as Notepad to edit it.

Or another way you can edit httpd.conf is to go to "Start" --> "Programs" --> "Apache HTTP Server" --> "Configure Apache Server" --> Edit the Apache httpd.conf Configuration File".

 

We need to setup the ScriptAlias for PHP.  This creates a name for the directory where PHP is located.

Use the search function in your text editor and search for "ScriptAlias".  You'll find something like this:

---------------
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/cgi-bin/"
---------------

 

Assuming that you followed the default installation of PHP and installed PHP into "c:/php/", then add the following line under "ScriptAlias"

ScriptAlias /php/ "c:/php/"

If you didn't install PHP into "c:/php/", then replace "c:/php/" with the directory into which you installed PHP.

-----------------
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/cgi-bin/"
ScriptAlias /php/ "c:/php/"
-----------------

 

Basically we just connected "/php/" to "c:/php/".  So anytime the server encounters "/php/", it will be translated into "c:/php/".

Now let's work on "Action".  Use the search function in your text editor to find the phrase "Action lets you"

-----------------
# Action lets you define media types that will execute a script whenever
# a matching file is called. This eliminates the need for repeated URL
# pathnames for oft-used CGI file processors.
# Format: Action media/type /cgi-script/location
# Format: Action handler-name /cgi-script/location
#
-----------------

On a new line without a # add the following line:

Action application/x-httpd-php /php/php.exe

It should look like this:

-------------------
# Action lets you define media types that will execute a script whenever
# a matching file is called. This eliminates the need for repeated URL
# pathnames for oft-used CGI file processors.
# Format: Action media/type /cgi-script/location
# Format: Action handler-name /cgi-script/location
#
Action application/x-httpd-php /php/php.exe
-------------------

 

Now we have to let Apache know which files contain PHP code.
Use your search function to look for "AddType".

------------
# AddType allows you to add to or override the MIME configuration
# file mime.types for specific file types.
#
AddType application/x-tar .tgz
AddType image/x-icon .ico
------------

 

Add the line:

AddType application/x-httpd-php .php

It should look like this:

---------------
# AddType allows you to add to or override the MIME configuration
# file mime.types for specific file types.
#
AddType application/x-tar .tgz
AddType image/x-icon .ico
AddType application/x-httpd-php .php
---------------

 

Now, save the httpd.conf file and restart Apache. 

Now we have to write a small php file to test out if PHP actually works.

Open up Notepad and paste the code between the two lines in the page.

----------------------------
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo "<p>Hello World</p>"; ?>
 </body>
</html>
----------------------------

In Notepad, select "File" then "Save As".  Browse to your website document directory and save the file as "index.php".  Keep the "" marks.  This makes Notepad save the file with the ".php" extension instead of ".txt".

 

Now fire up your web browser and point it at the directory where you saved the file.  You should see this:

 

If you see the actual code of the page, then something is wrong.  You will need to go back and figure out what is missing.

Depending on your particular setup, you may or may not have to edit a file called php.ini.  This file is in your c:\windows directory.

That's it, your done!

Brian
 



Installing PHP on a Windows Webserver

Additional Information

Resources: Step-by-Step: