Using FastCGI to Host PHP Applications on IIS 6.0

Author: Ruslan Yakushev

Published on December 05, 2007 by IIS Team

Updated on September 22, 2009 by IIS Team

Average Rating  Rate It (9)

Tags:
FastCGI
PHP
RSS

Introduction

This article explains how to use the Microsoft IIS FastCGI extension to set up and run PHP applications on Windows XP and Windows Server® 2003 operating systems.

What is FastCGI?

FastCGI is a standard protocol that allows application frameworks' CGI executables to interface with the web server. It differs from the standard CGI protocol in that FastCGI re-uses CGI processes for multiple requests, which provides a significant performance boost as compared to CGI. The IIS FastCGI support enables IIS to host normal CGI programs like PHP or Ruby On Rails with the FastCGI protocol and offer high-performance and stability for production deployment of such application frameworks.

Using the IIS FastCGI support involves the following:

  • The IIS web server
  • The IIS FastCGI extension
  • The CGI program (such as php-cgi.exe)

The web server dispatches HTTP requests to your application to the FastCGI component, which in turn launches the CGI program executable, and forwards the request for processing. Once the request is finished and the response is returned back to the server and sent to the client, the CGI process is re-used for a subsequent request. This helps to avoid the high performance penalty of starting a new process for each request, which results in much better performance and scalability in a production environment.

To learn more about the FastCGI protocol, see http://www.fastcgi.com/devkit/doc/fcgi-spec.html.

Installing the FastCGI Extension for IIS 6.0 and IIS 5.1

The FastCGI extension for 32bit and 64bit Windows XP and Windows Server 2003 can be downloaded here:

http://www.iis.net/extensions/fastcgi

When you run FastCGI installer, it copies FastCGI specific files to "%WINDIR%\system32\inetsrv" folder and then registers and enables FastCGI Web Server Extension. Out of the files that the installer copies, be aware of the following:

  • fcgiext.dll – This is the actual FastCGI handler that communicates with FastCGI enabled processes for processing requests
  • fcgiext.ini – This is the configuration file that contains mapping of file extensions to FastCGI processes. It also contains the configuration of FastCGI process pools.
  • fcgiconfig.js – This configures FastCGI extension. The script updates the fcgiext.ini file, modifies the IIS metabase and recycles the web service application pools if necessary.

Download and Install PHP

The FastCGI extension is fully compatible with the current official PHP 4.4.x and PHP 5.x distribution for Windows available from http://windows.php.net/download. Since the FastCGI extension hosts and executes php processes in a single threaded manner, it is recommended that you download and install the non-thread-safe PHP build available for Windows. The non-thread-safe PHP build provides a performance boost from a lack of thread safety that is acceptable for a single-concurrency environment like FastCGI.

Configuring FastCGI Extension to Work with PHP

Once PHP is installed on Windows, you have two options for configuring FastCGI: use the configuration script provided with the installation of the FastCGI extension, or perform all the configuration tasks manually by modifying IIS configuration settings and fcgiext.ini file. Depending on the option, refer to the corresponding sections below.

Using Script for Configuring FastCGI Extension to Work with PHP

The configuration script with the name fcgconfig.js is provided with the installation of FastCGI extension and is located in %WINDIR%\system32\inetsrv. To configure FastCGI extension to work with PHP, follow the steps below:

Open a command line window, change the current directory to %WINDIR%\system32\inetsrv, and run the configuration script to register the PHP CGI program as the one that will be processing .php extensions:

cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"C:\PHP\php-cgi.exe"

(replace C:\PHP\php-cgi.exe with the path to php-cgi.exe if you installed to a directory other than C:\PHP)

Note: If you do not want to register the PHP extension to be processed by the FastCGI component on your entire server, and only want to register it for a specific web site, then add a "–site:[siteId]" argument, e.g:

cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"C:\PHP\php-cgi.exe" –site:1

Note: For a complete list of configuration parameters supported by FastCGI extension, refer to the FastCGI extension documentation.

Manually Configuring FastCGI Extension to Work with PHP

To manually configure the IIS and FastCGI extension, you must create script mappings for the PHP extension in the IIS metabase and modify fcgiext.ini file located in %WINDIR%\system32\inetsrv.

Create script mapping

  1. Launch inetmgr.exe.
  2. Double click the machine icon for the local computer.
  3. Right click on "Web Sites" and pick "Properties."
  4. Click the "Home Directory" tab. 
  5. Click the "Configuration…" button.
  6. Click the "Add…" button.
  7. Browse to the fcgiext.dll located in %WINDIR%\system32\inetsrv.  

Note: If you need to use FastCGI in 32 bit mode on a 64 bit machine, then browse to %WINDIR%\SysWOW64\inetsrv.

    8.  Enter .php as the Extension.

    9.  Enter Verbs as GET,HEAD,POST .

  10.  Ensure that "Script Engine" and "Verify that file exists" are checked.

    11.  Click OK.

Modify fcigext.ini file

Once the script mapping is added, modify the fcgiext.ini file:

  1. Add an extension to application mapping (php=PHP) to the [Types] section.
  2. Add [PHP] section with ExePath=c:\php\php-cgi.exe (assuming you installed PHP files to C:\PHP folder).
[Types]

…

php=PHP

…

 

[PHP]

ExePath=c:\php\php-cgi.exe

After all the changes to fcgiext.ini the file are saved, restart the application pool associated with the web site that hosts your PHP applications.

Configure default document in IIS

Most of the PHP applications use a file named index.php as the default application document. Configure IIS to treat this file as the default content page:

  1. Launch inetmgr.exe.
  2. Double click the machine icon for the local computer.
  3. Right click "Web Sites" and pick "Properties."
  4. Click the "Documents" tab.
  5. Click the "Add…" button and enter "index.php" for "Default Document Name:" 
  6. Click "OK."
 

Setting FastCGI Configuration for Optimal Functionality, Security and Performance with PHP

Full PHP functionality support and performance of PHP applications running on the FastCGI extension were the main goals and focus for the IIS team. During our internal testing and in collaboration with Zend Technologies, we identified the set of configuration settings for the FastCGI extension and PHP that provide optimal functionality and performance for PHP applications when running on Windows via the FastCGI extension.

Configure your server as described below:

    1.  Modify the php.ini file as follows:

  • Set fastcgi.impersonate = 1. FastCGI under IIS supports the ability to impersonate the security tokens of the calling client. This allows IIS to define the security context under which the request runs.
  • Set cgi.fix_pathinfo=1. cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's previous behavior was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not care what PATH_INFO is. For more information on PATH_INFO, see the cgi specifications. Setting this to 1 causes PHP CGI to fix its paths to conform to the specification.
  • Set cgi.force_redirect = 0.

    2.  Set the FastCGI configuration settings for the PHP section by running fcgiconfig.js as follows:

  • Set the FastCGI process pool property InstanceMaxRequests to 10000. This setting specifies that the FastCGI extension will recycle php-cgi.exe after it has processed 10000 requests successfully.
> cscript fcgiconfig.js -set -section:"PHP" -InstanceMaxRequests:10000

    3.  Configure the FastCGI extension to set PHP_FCGI_MAX_REQUESTS environment variables for the PHP process to 10000.This setting instructs php-cgi.exe to recycle itself after it has processed 10000 requests successfully.


> cscript fcgiconfig.js -set -section:"PHP" -EnvironmentVars:PHP_FCGI_MAX_REQUESTS:10000

Note: You can configure InstanceMaxRequests and PHP_FCGI_MAX_REQUESTS to use numbers other than 10000. As a general rule, always make sure that the value of InstanceMaxRequests is less than or equal to the value of PHP_FCGI_MAX_REQUESTS.

Testing PHP CGI

After installing the FastCGI extension, and registering and configuring the PHP CGI, you can request your PHP application and use it normally.

To test, create and request a phpinfo.php page in your site that contains the following:

<?php

 

phpinfo();

 

?>

You see something like:


Note that the Server API indicates that PHP is hosted in FastCGI mode.

Getting More Information

To discuss the usage of the FastCGI Extension, or file bug reports, please use the FastCGI forums:

To get more information regarding running various PHP applications on IIS, refer to:

Related Content

Comments

  1. Submitted on Jul 24 2008 by
    DiamondEagle
    Excellent article - thanks!

    You should also read the "Configuring FastCGI Extension for IIS 6.0" article
    at http://learn.iis.net/page.aspx/248/configuring-fastcgi-extension-for-iis60/ which has more details about configuring FastCGI and the fcgiext.ini file.
  2. Submitted on Mar 03 2009 by
    jasnif
    Hi,
    I'm new to iis/php.
    I followed the same procedure explained in my windows xp system.
    And from my html file, i called the php script on a submit button (with action="post" method="trial.php").

    Still, when i click on submit, it says "The page can not be displayed".

    Can anyone help me please?

    Regards,
    Jaas
  3. Submitted on May 07 2009 by
    garymich
    Hi,

    I am not a server guy but am a developer. I have probably spend the most frustrating 3 days of my career of 24 years trying to configure wordpress to run on IIS 6.0. After following all the instructions to the letter (and using common sense where things were left out) I still cannot get this to work. As soon as I get through step 1 and try to continue to step 2 of thwe wordpress installation I get a permissions (403) error and can go no further. I have tried everything and cannot overcome thos issue. Is there a resolution for this or not? I tried a different php application that was used in one of the artices (phpBB3) and have the exact same problem.

    In a perfect world, everyone would just develop using .net but unfortunately this is not the case. Now I am wondering what to tell clients that require a mixed environment. For that matter I have a bunch of php apps I would like to at leasst check out but don't want to waste time trying to configure them.

    Is there a definitive A to Z guide on how to set this stuff up properly complete with guidance on how to setup the required permissions for foders that need to execute scripts?

    Regards

    Gary
  4. Submitted on Jun 13 2009 by
    anandn26
    I was earlier using WAMP but now i started using IIS with PHP and mysql and had to setup all from scratch. I later came to know about fastcgi.. then led to this site..

    No words to describe this page helped me a lot in configuring and settingup php5 for IIS and all that very efficently ..

    Thank you so much.. I wish to proceed now without much trouble
  5. Submitted on Jul 21 2009 by
    gelilloabad
    Nice guide! I managed to install PHP 5.3.0 in a Windows Server 2003 R2 x64

    I would add information about the problems I faced:

    1. I downloaded the version 5.3.0 (non thread safe) and I used the php.ini from "php-production"

    2. I set in there "safe_mode" = On and "file_uploads = Off" (My PHP site didn't need that so the less services available the more secure it is)

    3. If you get the error "503 Access Denied" when running PHP you might need to grant NTFS "Read/Execute" and "Directory Listing" permissions to the user running IIS (I set it to "USERS")

    4. I also got an error (PHP warning) stating I must set the timezone correctly. To fix that just set the time zone in PHP.ini, I added:
    date.timezone = "Europe/London"

    Thanks again.
  6. Submitted on Aug 09 2009 by
    guillermo.debiase
    I am attempting to get PHP5.3 installed on XP IIS5.1 with FastCGI.
    When I attempt to browse to a php file on localhost, I still get the prompt to download the file.
    Not really sure where to go from here, any and all assistance appreciated.
  7. Submitted on Aug 18 2009 by
    Kisama
    I also installed PHP 5.3 on XP IIS 5.1 using the instructions provided. I also cannot display PHP files, the browser attempts to download the PHP file. What are the possible causes of this?
  8. Submitted on Aug 25 2009 by
    captaingeek@gmail.com
    it'd be nice if we knew what output to expect from the >cscript fcgiconfig.js commands. fcgiconfig.js(1223, 5) (null): 0xFFFFFFDF doesn't mean a thing to me and we have no way of knowing if the command was completed correctly...

    Microsoft Windows [Version 5.2.3790]
    (C) Copyright 1985-2003 Microsoft Corp.

    C:\WINDOWS\system32\inetsrv>cscript fcgiconfig.js -add -section:"PHP" -extension
    :php -path:"C:\PHP\php-cgi.exe"
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    C:\WINDOWS\system32\inetsrv\fcgiconfig.js(1223, 5) (null): 0xFFFFFFDF


    C:\WINDOWS\system32\inetsrv>cscript fcgiconfig.js -set -section:"PHP" -InstanceM
    axRequests:10000
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    INI successfully written.

    C:\WINDOWS\system32\inetsrv>cscript fcgiconfig.js -set -section:"PHP" -Environme
    ntVars:PHP_FCGI_MAX_REQUESTS:10000
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    INI successfully written.
  9. Submitted on Aug 25 2009 by
    captaingeek@gmail.com
    also closely followed the instructions all I'm getting is http 500 errrors.
  10. Submitted on Sep 15 2009 by
    Difranco
    I also closely followed these instructions and the PHPInfo page is blank. No errors or anything. I added "Hello World" to PHPInfo page, and that is displayed in the browser.

    PHP NTS 5.3 intaller (VC9)
    2008 C++ Runtime
    Windows 2003 Standard x64

  11. Submitted on Sep 24 2009 by
    Hardt
    Hi,
    I also have a 500 error after following all these steps...
  12. Submitted on Oct 07 2009 by
    batman++
    I have the same problem as guillermo and Kisama. I've installed php 5.2.11 on IIS 5.1 (Windows XP) using the web platform installer, but when I go to test I get prompted to download the php file. Very frustrating.
  13. Submitted on Oct 16 2009 by
    seanys
    You refer to 'php-cgi.exe' but after installing PHP 5.2.11 there is only 'php-win.exe' and php.exe' present.

    Are either of these the cgi version?

You must Log In to comment.