November 21, 2009

Installing PHP 5 on IIS 5.1/IIS 6.0 with FastCGI

Rumor has it that a PHP+FastCGI installation provides a significant performance boost compared to a regular PHP+CGI installation. Some claim that it is even better than a PHP+ISAPI installation as it allows PHP to run just the way it is supposed to be without the inherent performance penalty associated with CGI. However the installation process turned out to be too much time consuming. I have therefore decided to document the process for those who have yet to install PHP.


Step 1: Install and Configure PHP 5

Download the latest (stable) PHP 5, Non Thread Safe, Zip package for Windows from:

Extract the contents of the zip file into a suitable location. In this article we will use C:\php as the PHP installation directory.

Configure the Path Variable

For PHP and its extensions to work properly, they need to access the DLLs present inside C:\php. The recommended method is to add C:\php to the Path environment variable. Here is how:

  1. Right click on the My Computer icon on your desktop.
    • Select the Properties item on the context menu.
    • Switch to Advanced tab.
    • Click the Environment Variables button.
  2. Inside the System variables list, locate the Path variable.
    • Select it and then click the Edit button.
  3. Inspect the existing value carefully and make sure that it does not contain C:\php.
    • Add a semi-colon (;) at the end of existing value if there isn't one.
    • Add C:\php after the semi-colon.
  4. Close all dialog boxes by clicking OK.
Configure the php.ini File

Inside C:\php, create a copy of php.ini-recommended and rename to php.ini.

Edit the php.ini file and make necessary changes. This could be a time consuming task as there are just too many configuration directives to deal with. However, I recommend that you leave all directives to defaults except the following:

cgi.fix_pathinfo = 1
cgi.force_redirect = 0
fastcgi.impersonate = 1
fastcgi.logging = 0

extension_dir = C:\php\ext
session.save_path = C:\WINDOWS\Temp\
upload_tmp_dir = C:\WINDOWS\Temp\

display_errors = On
display_startup_errors = On
log_errors = On
error_log = C:\WINDOWS\Temp\phperror.txt

extension = php_mysql.dll
extension = php_mysqli.dll
extension = php_curl.dll
extension = php_gd2.dll

Step 2: Install and Configure FastCGI

Download FastCGI Extension for IIS 5.1 and 6.0 from Microsoft Download Center. At the time of writing, the latest stable version 1.0 1.5 can be downloaded from this page. Run the installer to copy the necessary files to your machine and register FastCGI Extension with Internet Information Services.

Configure the fcgiext.ini File

The FastCGI Extension can be configured via the fcgiext.ini file which is located in C:\WINDOWS\system32\inetsrv\ directory. Edit his file and make necessary changes. Here is the minimal configuration required to run PHP CGI through FastCGI:


Arguments=-c C:\php\php.ini

STEP 3: Configure IIS 5.1/6.0

To route requests for PHP files to the FastCGI handler, you must associate .php files with the FastCGI Extension in IIS. This is done via Application Extension Mapping (also known as Script Mapping). Script maps associate file extensions with a script handler that executes when that file type is requested.

To create a PHP to FastCGI script map on IIS 6.0 and IIS 5.1 follow these steps:

  1. Launch Internet Information Services Manager (run inetmgr.exe).
  2. Expand the nodes in the left hand pane and locate Web Sites.
  3. Right click on Web Sites and select Properties item on the context menu.
    • Note: If the script mapping does not propagate to the child web sites, repeat the following process for each web site below the Web Sites node.
  4. Switch to Home Directory tab.
  5. Click the Configuration... button.
  6. Make sure that the Application Mappings list does not contain .php extension. Click the Add... button.
    • Enter C:\WINDOWS\system32\inetsrv\fcgiext.dll as the Executable.
    • Enter .php as the Extension.
    • Limit the Verbs to GET,HEAD,POST.
    • Check the Script engine and Verify that file exists check-boxes.
  7. Close all dialog boxes by clicking OK.
Configure Default Document

For your PHP applications to work smoothly, you should add index.php, the default application document for most PHP applications, to the list of Default Documents.

Follow thse steps to configure the default document in IIS:

  1. Launch Internet Information Services Manager (run inetmgr.exe).
  2. Expand the nodes in the left hand pane and locate Web Sites.
  3. Right click on Web Sites and select Properties item on the context menu.
  4. Switch to Documents tab.
  5. Make sure that the Enable Default Document check-box is checked.
  6. Click on Add..., enter index.php as the Default Document Name and click OK.
  7. If necessary, re-arrange the priority of default documents by selecting a document and clicking the up/down arrow buttons..
  8. Close all dialog boxes by clicking OK.
    • If prompted for Inheritance Overrides, select all child nodes and click OK.
Configure Web Service Extension (IIS 6.0)

On IIS 6, the FastCGI installer should have created an IIS Web Service Extension for FastCGI executable. I recommend that you double-check its presence by inspecting the Web Service Extensions in Internet Information Services Manager and make sure that it is Allowed.

If the FastCGI Web Service Extension is not present the follow these instructions to create/re-create it:

  1. Launch Internet Information Services Manager (run inetmgr.exe).
  2. Expand the nodes in the left hand pane and locate Web Service Extensions.
  3. Right click on the Web Service Extensions and select Add a new Web service extension... item on the context menu.
  4. Enter Microsoft FastCGI (or any other suitable name) in the Extension name field.
  5. Check the Set extension status to Allowed check-box.
  6. Add C:\WINDOWS\system32\inetsrv\fcgiext.dll file to the Required files list.
  7. Close all dialog boxes by clicking OK.

Step 4: Test Your Installation

Before testing, restart IIS to make sure all configuration changes are applied. To test the PHP installation, open up the command prompt and type:

php --info

Note that if PHP is configured correctly, there is no need to change directory to C:\php. PHP CLI should fire up and produce output similar to:

PHP Version => 5.2.11

System => Windows NT TITAN 5.1 build 2600
Build Date => Sep 16 2009 19:31:25

Next, to see if PHP, FastCGI and IIS are all setup properly, simply create a file containing these lines:


and save this file as C:\Inetpub\wwwroot\test.php. Now open http://localhost/test.php in your browser to view the result. If all goes well, the phpinfo page should appear. Verify the following values:

Server API: CGI/FastCGI
Loaded Configuration File: C:\php\php.ini 
Thread Safety: disabled