Getting Started with FastCGI on IIS
Microsoft's FastCGI handler, which is included with Windows Server 2008, can run with non-thread-safe versions of PHP by providing a pool of available processes and ensuring processes only handle one request at a time. This takes advantage of the performance benefits of both FastCGI and non-thread safe libraries to provide an extremely fast and efficient PHP environment on IIS 7.
By Paul Rubens
PHP applications can run blisteringly fast on IIS 7.
This may come as something of a surprise to many people: PHP running on IIS in CGI mode tends to be slow because a new process has to be started to handle each individual request coming in to the Web server. That's essentially because Windows is built on a multi-threaded architecture, while CGI was developed using a multi-process model.
One solution is to use FastCGI, an open extension to CGI that enables a single process to be reused many times for many different requests without having to shut the process down and restart it for each new request.
In the past, FastCGI handlers could not generally be configured with non-thread-safe versions of PHP, which have a performance advantage over slower thread-safe versions. But Microsoft's FastCGI handler, which is included with Windows Server 2008, can run with non-thread-safe versions of PHP by providing a pool of available processes and ensuring processes only handle one request at a time. This takes advantage of the performance benefits of both FastCGI and non-thread safe libraries to provide an extremely fast and efficient PHP environment on IIS 7.
So how do you get IIS 7 running with non-thread-safe PHP binaries in FastCGI mode?
For the purposes of this article I'll assume that IIS 7 is already installed on your Windows Server 2008 machine, and the process can be carried out in about 15 minutes in 10 simple steps.
-
The first step is to open the Server Manager by clicking on the Start button and selecting Server Manager from the Administrative Tools option. Once the Server Manager window opens, click on Roles in the left pane and then click on Web Server (IIS). You'll then need to scroll down the central pane until you come to the Role Service section. CGI should be marked as Not Installed at this stage.
-
Click on Add Role Services, and add a check to the box next to CGI. A brief description of CGI will appear to the right of the window, which includes a warning about the performance overhead that results from using CGI. For some reason, no mention is made of the fact that installing the CGI service also installs the FastCGI service.
-
At this point it is sensible to download and install an update to Microsoft's FastCGI module that resolves an issue (KB954956) in which a PHP application dependent on the REQUEST_URI server variable fails in IIS 7.0
-
The next step is to download install PHP itself, and the official release is available from http://www.php.net/downloads.php. As explained above, it's worth using the latest non-thread-safe version since the FastCGI module can take advantage of it. At the time of writing the current version is PHP 5.2.8 non-thread-safe.
After accepting the license agreement and selecting a destination folder, select IIS FastCGI as the Web Server to set up, and click through the remaining options to complete installation
-
You'll now need to modify the PHP.ini file to fine tune how PHP runs. To do this, navigate to the folder into which you installed PHP (probably C:\Program Files\PHP) and open the INI file using notepad. The options that need to be modified are commented out (thus rendering them inoperative) by default, so be sure to delete the leading semi-colon (;) as well as editing the values. Microsoft recommends the following modifications:
- 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.
- 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.
- Set cgi.force_redirect = 0.
- Set open_basedir to point to a folder or network path where the content of the Web site is located.
-
To test the PHP installation, open a command prompt and navigate to the PHP directory. Type in: PHP –info. If all is well the command box will scroll for a few seconds, finishing up as shown below:
-
The next configuration task is to make IIS 7 run PHP using FastCGI. To do this, open IIS Manager from Administrative Tools, click on the server name in the left pane, and double click on Handler Mappings.
Then click Add Module Mapping in the right pane of the Handler Mappings window.
-
Fill in the Add Module Mapping window as shown below:
| Request Path: | *.php |
| Module: | FastCgiModule |
| Executable: | "C:\Program Files\PHP\php-cgi.exe" |
| Name: | FastCGI for PHP (or anything else you want) |
and click Yes to create a FastCGI application for this executable in the popup that appears.
Note: the executable path must be put between double quotation marks as shown above if the path includes spaces, as in this example.
The FastCGI PHP mapping will now appear in the Enabled state in the Handler Mappings window.
-
To confirm that IIS 7 is ready to handle PHP via FastCGI:
-
Create a text file called info.php and copy in the following:
<?php phpinfo(); ?>
-
Save the file in the folder C:\inetpub\wwwroot
Note: You may need to add write permissions to this folder from the account you are using in order to save your text file.
-
Open your browser and go to http://localhost/info.php.
If IIS 7 is handling PHP files correctly, you should see the following:
Optional extra step:
Download and install the Administration Pack for IIS 7.0. This adds a FastCGI settings icon to the IIS Manager, which opens an applet for tweaking FastCGI settings using a graphical user interface. This can be used to prevent failures caused by PHP recycling by adjusting two environment variables as follows:
Start the FastCGI settings applet, click edit in the right hand pane, and set the InstanceMaxRequests variable to 10000. Then click the browse box in the EnvironmentVariables row and add an PHP_FCGI_MAX_REQUESTS environment variable, and set its value to 10000. By ensuring InstanceMaxRequests is not bigger than PHP_FCGI_MAX_REQUESTS, you can prevent possible failures caused by PHP (and not FastCGI) recycling the php-cgi.exe processes
-
To round off the installation procedure, it is recommended that you download and view a presentation called "Hosting PHP on IIS 7.0 - Best Practices for shared hosting," which is available for download from Microsoft.