Install FastCGI in IIS 7 for PHP

PHP for 7 IIS in FastCGI Install

Enabling FastCGI Support in IIS 7.0 (Windows Server 2008)

Add the CGI role service by going to Server Manager -> Roles -> Add Role Services. This enables both the CGI and FastCGI services:

 

Install and Configure PHP

It is recommended to use a non-thread safe build of PHP with IIS 7.0 FastCGI. A non-thread safe build of PHP provides significant performance gains over the standard build by not doing any thread-safety checks, which are not necessary, since FastCGI ensures a single threaded execution environment.

  1. Download the latest non-thread safe zip package with binaries of PHP from http://www.php.net/downloads.php.
  2. Unpack the files to a directory of your choice (e.g. C:\PHP). Rename the php.ini-recommended to php.ini.
  3. Open the php.ini file, then uncomment and modify settings as follows:
    • Set fastcgi.impersonate = 1. FastCGI under IIS supports the ability to impersonate security tokens of the calling client. This allows IIS to define the security context that the request runs under.
    • 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 specs. Setting this to 1 will cause PHP CGI to fix its paths to conform to the spec
    • Set cgi.force_redirect = 0.
    • Set open_basedir to point to a folder or network path where the content of the web site(s) is located.
    • Set extension_dir to point to a location where PHP extensions reside. Typically, for PHP 5.2.X that would be set as extension_dir = "./ext"
    • Enable the required PHP extension by un-commenting corresponding lines, for example:

      extension=php_mssql.dll
      extension=php_mysql.dll
  4. To test if the PHP installation is successful, run the following from the command line prompt:

C:\PHP>php –info

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

Configure IIS 7.0 to Handle PHP Requests

In order for IIS 7.0 to host PHP applications, it is necessary to add a handler mapping that tells IIS to pass all PHP specific requests to the PHP application framework via FastCGI protocol.

Using IIS Manager

Open IIS Manager and then select and open “Handler Mappings” at the server level:

Select the “Add Module Mapping” action and specify the configurations settings as below:

  • Request path: *.php
  • Module: FastCgiModule
  • Executable: "C:\[Path to your PHP installation]\php-cgi.exe"
  • Name: PHP via FastCGI

Click OK. A dialog box appears asking if you want to create a FastCGI application for this executable. Click Yes.

Test that the handler mapping works correctly by creating a phpinfo.php file in the C:\inetpub\wwwroot folder that contains the following code:

<?php phpinfo(); ?>

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

NOTE: If you do not see "FastCgiModule" in the "Modules:" drop-down list then it means that the module is not registered or not enabled. To check if FastCGI module is registered open the IIS configuration file %WINDIR%\windows\system32\config\applicationHost.config and check that the following line is present in <globalModules> section:

<add name="FastCgiModule" image="%windir%\System32\inetsrv\iisfcgi.dll" />

Also, in the same file, check that the FastCGI module is added to the <modules> section:

<add name="FastCgiModule" />

PHP Process Recycling Behavior

Make sure that FastCGI always recycles the php-cgi.exe processes before the native PHP recycling kicks in. The FastCGI process recycling behavior is controlled by the configuration property instanceMaxRequests. This property specifies how many requests the FastCGI process will process before recycling. PHP also has a similar process recycling functionality that is controlled by an environment variable PHP_FCGI_MAX_REQUESTS. By setting instanceMaxRequests to be smaller or equal to PHP_FCGI_MAX_REQUESTS, you can ensure that the native PHP process recycling logic will never kick in.

The FastCGI settings can be configured either by using IIS Manager or by using the command line tool appcmd.

Using IIS manager

To configure FastCGI recycling settings by using IIS Manager, you need to install Administration Pack for IIS 7.0 and then select FastCGI settings at the server level:

Next select the FastCGI application that you want to configure and click "Edit..." in the Actions pane on right hand side:

In the "Edit FastCGI application" dialog, set instanceMaxRequest to 10000 and then click on the browse button next to the EnvironmentVariables setting:

Add the PHP_FCGI_MAX_REQUESTS environment variable and set its value to 10000:

 



Comments are closed