Using FastCGI to Host PHP Applications on IIS 6.0

Applies To: Windows Server 2003

This article describes how to use the Microsoft® Internet Information Services (IIS) 6.0 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 the Common Gateway Interface (CGI) executables in application frameworks to interface with the Web server. It differs from the standard CGI protocol in that FastCGI reuses CGI processes for multiple requests, which provides a significant performance boost compared to CGI. IIS FastCGI support enables IIS to host common CGI programs such as PHP or Ruby on Rails by using the FastCGI protocol and, by doing so, offer high performance and stability for production deployment of such application frameworks. IIS FastCGI support involves the following items:

  • The IIS Web server (FastCGI Extension Go Live release supports IIS 6.0)

  • 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 starts 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 reused for a subsequent request. This helps avoid the performance penalty of starting a new process for each request, which gives you much better performance and scalability in a production environment.

To learn more about the FastCGI protocol, see the FastCGI Specification.

Installing the FastCGI Extension for IIS 6.0

The FastCGI extension for 32–bit and 64–bit Windows XP and Windows Server 2003 can be downloaded here: https://www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1521.

When you run the FastCGI installer, it copies FastCGI specific files to the "%windir%\system32\inetsrv" folder and then registers and enables the FastCGI Web Server Extension. Of the files that the installer copies, the following files are of particular significance:

  • 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 for file name extensions to FastCGI processes. It also contains the configuration of FastCGI process pools.

  • fcgiconfig.js — This configures the FastCGI extension. The script updates the Fcgiext.ini file, modifies the IIS metabase, and recycles the Web service application pools if there have been changes.

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®. It is available from www.php.net/downloads. Since the FastCGI extension hosts and executes PHP processes in a single-threaded manner, you should download and install the non-thread-safe PHP build available for Windows. The non-thread-safe PHP build provides a major performance boost for a single-concurrency environment like FastCGI where a lack of thread safety is acceptable. After downloading the package, extract it to c:\php\. Navigate to c:\php\, and rename php.ini-recommended to php.ini.

You can also use the FastCGI extension with existing PHP 4.4.x or PHP 5.x installations.

Note: PHP distributions starting from PHP 5.2.1 contain the performance enhancements developed by Zend that will improve the performance of the PHP engine on Windows. These latest versions provide the optimal PHP performance on the IIS/FastCGI platform.

Zend Core provides a commercial PHP distribution that contains the PHP performance improvements for Windows.

Configuring the FastCGI Extension to Work with PHP

Once PHP is installed on Windows, you have two options for configuring FastCGI: you can use the configuration script provided with the installation of the FastCGI extension, or you can perform all the configuration tasks manually by modifying IIS configuration settings and the Fcgiext.ini file. Depending on the option that you select, see the corresponding sections in the remainder of this topic.

Option 1: Using Script for Configuring the FastCGI Extension to Work with PHP

The configuration script with the name fcgconfig.js is provided with the installation of the FastCGI extension and is located in %windir%\system32\inetsrv.

To configure the FastCGI extension to work with PHP

  1. 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 of 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 you only want to register it for a specific Web site, then add a "–site:[siteId]" argument.

    Example:

    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 the FastCGI extension, see the FastCGI extension documentation.

Option 2: Manually Configuring the FastCGI Extension to Work with PHP

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

To create script mapping

  1. Start Inetmgr.exe.

  2. Double-click the icon for the local computer.

  3. Right-click Web Sites and select Properties.

  4. Click the Home Directory tab.

  5. Click Configuration.

  6. Click Add.

  7. Browse to the fcgiext.dll located in %windir%\system32\inetsrv.

    Note: If you must use FastCGI in 32–bit mode on a 64–bit computer, then browse to %windir%\SysWOW64\inetsrv.

  8. Enter .php in the Extension field.

  9. Select Limit to and enter GET,HEAD,POST.

  10. Select Script Engine and Verify that file exists.

  11. Click OK.

To modify the fcigext.ini file

  1. Once the script mapping is added, modify the %windir%\System32\inetsrv\fcgiext.ini file.

  2. Add an extension to application mapping (php=PHP) to the [Types] section.

  3. 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 the Fcgiext.ini file are saved, restart the application pool associated with the Web site that hosts your PHP applications.

Configuring the Default Document in IIS

After configuring the FastCGI Extension to work with PHP by either of the two methods detailed earlier in this section, configure a default application document in IIS. Most PHP applications use a file that is named Index.php as the default application document. Configure IIS to treat this file as the default content page.

To configure the default document in IIS

  1. Start Inetmgr.exe.

  2. Double-click the icon for the local computer.

  3. Right-click Web Sites, and select Properties.

  4. Click the Documents tab.

  5. Click Add, and then enter index.php in the Default content page field.

  6. Click OK.

  7. If prompted for Inheritance Overrides, select the child nodes, and then click OK.

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

Full PHP functionality support and performance of PHP applications that are running on the FastCGI extension were the focus and main goal for the Microsoft IIS team. During the IIS team's internal testing and collaboration with Zend Technologies, they identified the set of configuration settings for the FastCGI extension and PHP that provides optimal functionality and performance for PHP applications when they are running on Windows and are using the FastCGI extension.

To configure your server

  1. Modify the php.ini file as follows:

    Note: Remove the ";", if it is present at the beginning of the following configuration lines.

    • Set fastcgi.impersonate = 1. FastCGI under IIS supports the ability to impersonate the security tokens of the calling client. This lets IIS 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 not care what PATH_INFO is. For more information about PATH_INFO, see the CGI specifications. Setting this to 1 causes PHP CGI to fix its paths to comply with the specification.

    • Set cgi.force_redirect = 0.

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

    Note: These commands should be run from the %windir%\system32\inetsrv\ folder.

    • 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 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 PHP CGI, you can request and use your PHP application.

To test, create and request a phpinfo.php page in your C:\inetpub\wwwroot folder that contains the following:

<?php

phpinfo();

?>

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

Installing phpBB Application for Ase with the FastCGI Extension

The phpBB Internet forum solution is a popular open-source application that provides a large and very customizable set of features. These include powerful permissions systems, private messaging, search functions, a customizable template and language system, and support for multiple databases. For more information about phpBB, see https://www.phpbb.com. For phpBB community–specific information, see https://www.phpbb.com/community/.

This section describes the steps that are required to install phpBB to work with the FastCGI extension on IIS 6.0.

The following steps assume that you have completed the setup and configuration of the FastCGI extension and PHP libraries as described in the previous sections.

Download and Unpack the Application

First, download the latest stable release of phpBB. As of January, 2009, the current release of phpBB (phpBB 3.0.2) is available at https://www.phpbb.com/downloads/?sid=ee349e0ae5950a30df04461023f7e34f. Once you download the package, uncompress it and copy all the files and folders to C:\Inetpub\wwwroot\phpBB2.

Set Up the Database

In order to use phpBB, you must have access to a database. For this walkthrough, we use Microsoft® SQL Server® 2005 Express Edition with mixed authentication. You can also use Microsoft® SQL Server® 2005, Microsoft® Office Access®, MySQL, or other databases.

Before starting the installation, create a database on the database server. Also create a database user and grant this user database ownership permission to the database.

Open Microsoft SQL Server Management Studio, and then click the New Query button. Enter the following script into the query window, and then click Execute. This script creates the database and user necessary for the phpBB application and should return "Command(s) completed successfully." in the messages pane.

USE [master]

GO

CREATE DATABASE phpbb3

GO

CREATE LOGIN [phpbb3]

WITH PASSWORD=N'phpbb3',

DEFAULT_DATABASE=[phpbb3],

CHECK_EXPIRATION=OFF,

CHECK_POLICY=OFF

GO

USE [phpbb3]

GO

CREATE USER [phpbb3] FOR LOGIN [phpbb3] WITH DEFAULT_SCHEMA=[dbo]

USE [phpbb3]

GO

sp_addrolemember 'db_owner','phpbb3'

Set Up ODBC Connection to the phpBB3 Database

To form the connection between the phpBB3 application and the database, create an ODBC connection. In the example used in this article, we use SQL Server 2005 Express Edition with an ODBC connection.

To create an ODBC connection

  1. Click Start, Administrative Tools, Data Source (ODBC).

  2. Click the System DSN tab.

  3. Click Add.

  4. Select SQL Server, and then click Finish.

    1. Name: phpbb3

    2. Description:

    3. Server: <HOSTNAME>\SQLEXPRESS

    4. Click Next.

  5. Select With SQL Server authentication using a login ID and password entered by the user.

    • Check Connect to SQL Server to obtain default settings for the additional configuration options.

    • Login ID: sa

    • Password: <sa password>

    • Click Next.

  6. Select Change the default database to:, and select phpbb3 from the drop-down menu.

  7. Click Next.

  8. Click Finish.

  9. Click Test Data Source. The results should be "TESTS COMPLETED SUCCESSFULLY!"

  10. Click OK.

  11. Click OK.

  12. Click OK.

Set Up and Configure the Application

Open your Web browser and browse to https://localhost/phpBB3/index.php. This redirects you to the phpBB setup page. Enter the required installation information and replace the Database Server Hostname with the name of your database server.

To set up and configure the application

  1. Click Install tab.

  2. Click Proceed to next step.

  3. Requirements:

    1. Files and Directories may appear as "Unwritable."

    2. Navigate to c:\inetpub\wwwroot\phpbb3\ folder.

    3. Change NTFS security for the following folders so that Internet Guest Account has write access to the following folders:

      1. Cache/

      2. Files/

      3. Store/

    4. Click Test again.

    5. All compatibility requirements should pass.

    6. Click Start Install.

  4. Database settings: (SQL Server 2005 Express, mixed authentication)

    1. Database type: MSSQL Server 2000+ via ODBC

    2. Database server hostname or DSN: phpbb3 (created earlier in ODBC configuration section)

    3. Database name: phpbb3

    4. Database username: phpbb3

    5. Database password: phpbb3

    6. Click Proceed to next step.

    7. "Successful connection" message should be displayed. Click Proceed to next step.

  5. Administrator details

    1. Administrator username: admin

    2. Administrator password: <create admin password>

    3. Confirm administrator password: <create admin password>

    4. Contact e-mail address: <enter contact e-mail>

    5. Confirm contact e-mail address: <enter contact e-mail>

    6. Click Proceed to next step.

    7. Results should be "Tests passed." Click Proceed to next step.

  6. Configuration file

    1. To download configuration file, click Download.

    2. Save config.php to c:\inetpub\wwwroot\phpbb3\ folder. Overwrite, if you are prompted,.

    3. Click Done.

    4. Click Proceed to next step.

  7. Advanced settings

    1. Leave default settings for this installation.

    2. Click Proceed to next step.

  8. Click Proceed to next step.

  9. Click Login.

  10. You will be redirected to the Welcome page. Click Logout in the left column.

Note: If your Web site is configured to allow anonymous users, then you must grant access permissions to "Internet Guest Account."

Test the phpBB Application

To test that phpBB has been installed successfully and create a forum

  1. Navigate to https://localhost/phpBB3, and then click Login.

  2. Enter the administrator username and password (this walkthrough used username: admin).

  3. Once logged in, click on the Administration Control Panel link at the bottom of the page to view the administration panel.

  4. This causes phpBB to ask you to re-authenticate. It then takes you to the administration panel.

  5. On this panel, click Manage Forums under the Quick Access group.

  6. Enter the desired forum name, and then click Create new forum.

  7. Leave the default settings on the Create new forum page, and then click Submit.

  8. You will then be redirected to the Forum permissions page. You can assign permissions later. Click the Manage forums link in the left column.

  9. You can now see your new forum listed on the Forum administration page.

Getting More Information

To discuss the FastCGI extension for IIS 6.0 or file bug reports, use the IIS.NET FastCGI forum.

To see a list of known issues with the IIS 6.0 FastCGI, see this forum: https://forums.iis.net/1103/ShowForum.aspx.

To get more information about running various PHP applications on IIS, see:

PHP support in IIS 6.0 and IIS 7.0

PHP Community forum

Articles