Configuring FastCGI to Host PHP Applications (IIS 7)

Applies To: Windows 7, Windows Server 2008, Windows Server 2008 R2, Windows Vista

Internet Information Services (IIS) 7.0 adds support for the FastCGI protocol, which improves the performance and reliability of popular application frameworks, such as PHP, hosted on an IIS Web server. FastCGI provides a high-performance alternative to the Common Gateway Interface (CGI) protocol, a standard way of interfacing external applications with Web servers.

IIS 7 for Windows Server® 2008 and Windows Vista® with Service Pack 1 (SP1) now include a built-in FastCGI component. This topic describes how to use the FastCGI module to host PHP applications on IIS 7 in Windows Server 2008 and Windows Vista with SP1.

Important: This topic provides instructions about how to install and how to use the FastCGI component on Windows Server 2008 and Windows Vista with SP1. There is no officially supported FastCGI component for Windows Vista (not SP1). It is strongly recommended that you upgrade to Windows Vista with SP1 if you have to use the FastCGI component on the Windows Vista operating system.

Enable FastCGI support in IIS 7.0 on Windows Server 2008

To enable FastCGI support in IIS 7.0 on Windows Server 2008

  1. Open Server Manager.

  2. In the right pane, under Roles Summary, click Add Roles.

  3. Use the Add Roles Wizard to select the CGI role service. This enables both the CGI and FastCGI services.

Enable FastCGI support in IIS 7.0 on Windows Vista SP1

To enable FastCGI support in IIS 7.0 on Windows Vista SP1

  1. Go to the Windows Start menu, and open the Control Panel.

  2. Double-click Programs and Features.

  3. Click Turn Windows features on or off.

  4. In the Windows Features dialog box, expand Internet Information Services, expand World Wide Web Services, expand Application Development Features, and then select CGI. Click OK.

  5. Install the update for the FastCGI module from one of the following locations. This update fixes several known compatibility issues with popular PHP applications.

Install and Configure PHP

It is recommended that you use a non-thread-safe build of PHP with IIS 7 FastCGI. A non-thread-safe build of PHP provides significant performance gains over the standard build by not doing any thread-safety checks. These checks are not necessary because FastCGI guarantees a single-threaded execution environment.

To install PHP

  1. Go to the PHP Web site and download the latest non-thread-safe binaries of PHP.

  2. Unzip the files to a directory of your choice, for example, C:\PHP. Rename the Php.ini-recommended to Php.ini

  3. Open the Php.ini file, and then uncomment and modify the settings as follows:

    1. Set fastcgi.impersonate = 1. FastCGI in 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.

    2. Set cgi.fix_pathinfo=1. cgi.fix_pathinfo provides actual PATH_INFO/PATH_TRANSLATED support for CGI. Previous PHP behavior was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to ignore that setting in PATH_INFO. For more information about PATH_INFO, see the CGI specifications. Setting the PATH_INFO value to 1 will cause PHP CGI to match its paths to the specification.

    3. Set cgi.force_redirect = 0.

    4. Set open_basedir to point to a folder or network path where the content of the Web site is located.

  4. To test whether the PHP installation is successful, type the following at a command prompt:

    C:\PHP>php –info

    If PHP was installed correctly and all its dependencies are available on the computer, then this command will output the current PHP configuration information.

Configure IIS 7.0 to Handle PHP Requests using the UI

For IIS 7 to host PHP applications, you must add a handler mapping that tells IIS to pass all requests for PHP files to the PHP application framework using the FastCGI protocol.

To add a handler mapping at the server level using the UI

  1. Open IIS Manager, and then move to the server level.

  2. Double-click Handler Mappings.

  3. In the Actions pane, click Add Module Mapping.

  4. In the Add Module Mapping dialog box, specify the configuration settings as follows:

    • Request path: *.php

    • Module: FastCgiModule

    • Executable: C:\[Path to your PHP installation]\php-cgi.exe

    • Name: PHPviaFastCGI

  5. Click OK.

  6. In the dialog box that asks you to confirm that you want to create a FastCGI application for the executable, click Yes.

  7. To verify that the handler mapping works correctly, create a Phpinfo.php file in the C:\inetpub\wwwroot folder that contains the following:

    <?php phpinfo(); ?>

  8. Open a Web browser, and then navigate to http://localhost/phpinfo.php. If everything was set up correctly, you will see the standard PHP information page.

Configure IIS 7.0 to Handle PHP Requests using the command line

You can also use Appcmd.exe to add a handler mapping.

To add a handler mapping at the server level using the command line

  1. To create a FastCGI application process pool, run the following command:

    C:\>%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCgi /+[fullPath='c:\{php_folder}\php-cgi.exe']

  2. To create the handler mapping, run the following command:

    C:\>%windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers /+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='c:\{php_folder}\php-cgi.exe',resourceType='Either']

    Note: If you are using PHP version 4.x, instead of Php-cgi.exe, you can use Php.exe.

Configure PHP and FastCGI Recycling Behavior

Make sure that FastCGI always recycles Php-cgi.exe processes before native PHP recycling starts. The configuration property instanceMaxRequests controls the FastCGI process-recycling behavior. This property specifies how many requests FastCGI will process before recycling. PHP also has a similar process-recycling functionality that is controlled by the environment variable PHP_FCGI_MAX_REQUESTS. By setting instanceMaxRequests to a value that is smaller than or equal to PHP_FCGI_MAX_REQUESTS, you can make sure that the native PHP process-recycling logic will never start.

To set these configuration properties, use the following commands:

C:\>%windir%\system32\inetsrv\appcmd set config -section:system.webServer/fastCgi /[fullPath='c:\{php_folder}\php-cgi.exe'].instanceMaxRequests:10000

C:\>%windir%\system32\inetsrv\appcmd set config -section:system.webServer/fastCgi /+[fullPath='c:\{php_folder}\php-cgi.exe'].environmentVariables.[name=’PHP_FCGI_MAX_REQUESTS’, value='10000']

Note: If these parameters have not been set, the following default settings will be used (on most PHP builds): instanceMaxRequests = 200, PHP_FCGI_MAX_REQUESTS = 500.