Using FastCGI to Host PHP Applications on IIS 7.0

Author: Mike Volodarsky

Published on December 05, 2007 by IIS Team

Updated on August 27, 2008 by IIS Team

Average Rating  Rate It (20)

Tags
PHP
FastCGI
RSS

Introduction

The FastCGI support in IIS enables popular application frameworks that support FastCGI protocol to be hosted on the IIS web server in a high-performance and reliable way. FastCGI provides a high-performance alternative to the Common Gateway Interface (CGI), a standard way of interfacing external applications with Web servers that has been supported as part of the IIS feature-set since the very first release.  

Microsoft IIS 7.0 for Windows Server® 2008 and Windows Vista SP1 now includes a built-in FastCGI component. This walkthrough leads you through the steps to use FastCGI module to host PHP applications on IIS7 in Windows Server 2008 and Windows Vista SP1.

  IMPORTANT: This article provides instructions on how to install and use FastCGI component on Windows Server 2008 and Windows Vista SP1. There is no officially supported FastCGI component for Windows Vista (not SP1). It is strongly recommended to upgrade to Windows Vista SP1 if you need to use FastCGI component on Windows Vista Operating System.

Enable FastCGI support in IIS7.0

To enable FastCGI support on IIS7.0 in Windows Server 2008:

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

  

To enable FastCGI support on IIS7.0 in Windows Vista SP1:

Add CGI feature by going to Control Panel -> Programs and Features -> Turn Windows features on or off. This enables both CGI and FastCGI services.


 IMPORTANT: Install the update for FastCGI module

The update for IIS 7.0 FastCGI module fixes several known compatibility issues with popular PHP application. Install the update from one of the following locations:

Install and Configure PHP

It is recommended to use a non-thread safe build of PHP with IIS 7.0 FastCGI. 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.

Note: There is no installer package for non-thread safe build of PHP.

    1.  Download the latest non-thread safe 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 php.ini file, then uncomment and modify settings as follows:

a. 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.

b. 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 it's paths to conform to the spec

c. Set cgi.force_redirect = 0.

d. Set open_basedir to point to a folder or network path where the content of the web site(s) is located.

    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 requests for PHP files to PHP application framework via FastCGI protocol.

To add a handler mapping at a server level

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

  

   2.  Select “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

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

    4.  Test that the handler mapping works correctly by creating a phpinfo.php file in the C:\inetpub\wwwroot folder that contains the following:
<?php phpinfo(); ?>.

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

Alternatively, the above mentioned steps can be completed by using command line tool appcmd.

    1.  To create the 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.  After that, create the handler mapping:

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 a 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 the native PHP recycling kicks in. The FastCGI process recycling behavior is controlled by the configuration property instanceMaxRequests. This property specifies how many requests 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 native PHP process recycling logic will never kick in.

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 those parameters have not been set, then the following default settings are used: instanceMaxRequests = 200, PHP_FCGI_MAX_REQUESTS = 500 (on most PHP builds).

More Information  

Popular PHP applications on Windows Server

PHP development community behefits from large set of open source web applications. Now, with FastCGI support in IIS, all those popular web applications can be hosted on Windows Server. To learn more about how to setup and use most popular PHP web applications refer to this set of articles:

http://learn.iis.net/page.aspx/271/php-applications-on-iis/

PHP on IIS7.0 in Shared Hosting Environment

There are few important configuration steps that hosting providers need to be aware of if they plan to provide PHP support on Windows. Refer to hosting guidance for more details regarding PHP configuration for shared hosting environment.

http://learn.iis.net/page.aspx/208/fastcgi-with-php/

Securing the PHP Application with Login Controls, and Forms Authentication

One of the important new features in IIS 7.0 is the integrated pipeline, which allows securing all of the web content using ASP.NET's Forms Authentication. If you want to secure the PHP application using Forms Authentication, follow these steps:

http://learn.iis.net/page.aspx/244/how-to-take-advantage-of-the-iis7-integrated-pipeline/

Increasing PHP Performance with IIS Kernel Mode Caching

Bill Staples put together an important blog entry on PHP in IIS 7.0 that contains a section with details on using IIS Kernel Mode caching with PHP applications, specifically QDIG. For more information, see Bill's blog at the following URL:

http://blogs.iis.net/bills/archive/2006/10/31/PHP-on-IIS.aspx

Related Content

Comments

  1. Submitted on May 26 2008 by
    radirk
    Hello,
    setup was easy enough and I get the phpinfo.php page displayed just fine. However, upon running C:\>%windir%\system32\inetsrv\appcmd set config -section:system.
    webServer/fastCgi /+[fullPath='c:\php\php-cgi.exe'].environmentVariables.[name='
    PHP_FCGI_MAX_REQUESTS', value='10000']
    I get this:
    ERROR ( message:Cannot find SITE object with identifier "value='10000']". ) Please advise.
    Thanks! Dirk
  2. Submitted on May 29 2008 by
    GISDOTNET
    I also receive the same error as Dirk. Is there a solution? Thanks, Aaron.
  3. Submitted on May 29 2008 by
    GISDOTNET
    I forgot to mention that I am running Vista SP1 and PHP 5.2.6. Thanks, Aaron.
  4. Submitted on May 29 2008 by
    cameronward
    I get to the point to where I'm typing php>php -info and all that comes back to me is "Access is denied". Where do I go from there?
  5. Submitted on May 29 2008 by
    cameronward
    I'm working with server 2008.
  6. Submitted on May 29 2008 by
    GISDOTNET
    cameronward, I actually had similar, yet not the exact issue, and had to run CMD as Administrator even though my account is in the administrator group. I have noticed that this needs to be done on other things as well within Vista and 2008. Hope this helps, Aaron
  7. Submitted on Jun 13 2008 by
    ntgt13
    I got the same error but resolved it by removig the space between the , and the value='10000'. ,*space*value='10000' -> ,value='10000']
  8. Submitted on Jun 15 2008 by
    Hybrid SyntaX
    hi
    thanks for your tutorial i made PHP work on vista finally !
    but i have some problems ... 1.i couldn't use any of those commands ...
    2.i couldn't find any fastcgi module ! it was "CGI Module" for me ...
    3.when i open my php page it shows what i've entered but there're some garbages along ...
    it's like 15+ line and they are like this "specified module could not be found. in Unknown on line 0 PHP Warning: PHP Startup: Unable to load dynamic library 'C:\PHP\ext\php_radius.dll' - The specified module could not be found. in Unknown on line 0 PHP Warning: PHP Startup: Unable to load dynamic library 'C:\PHP\ext\php_rar.dll' ... " i've installed PHP 5.2.6 on C:\PHP and i'm using Vista x64
    is that usual or i did something wrong ?
  9. Submitted on Jun 17 2008 by
    scotteredu
    After some fiddling I was able to get it to work, mostly. I am using PHP/MYSQL on my Vista box to demo so CMS products and when I go to install I get this: PHP Warning: session_start() [function.session-start]: open_basedir restriction in effect. File(C:\Windows\TEMP\) is not within the allowed path(s): (c:\local) in C:\local\install\index.php on line 10 PHP Fatal error: session_start() [function.session-start]: Failed to initialize storage module: files (path: ) in C:\local\install\index.php on line 10 I think this is related to the 'open_basedir' line in the config steps above. Removing the reference to my website dir (c:\local) allows the install of the first CMS am testing to carry on. My qestion is, first how unsecure is it to not have that line confi'd and second, if I do configure it like I did before - what else do I need to add to elminate the error? very informative write up. Thanks. s
  10. Submitted on Jun 24 2008 by
    ruslany
    The open_basedir is a security protection measure which is useful when the PHP scripts that you deploy on your server are not trusted. For example, this is important to use in shared hosting scenarios. If you know what your script does then you may turn off this setting. Or, alternatively you can configure the PHP session.save_path setting to use file path that is within the scope of the open_basedir restriction.
  11. Submitted on Jul 02 2008 by
    ericandrews
    This is a well written article, thank you. I am running Vista Ultimate with SP 1. When following these instruction I do not have the option to choose FastCgiModule. I followed these instructions, have CGI chose as an option but there is no option to choose FastCgiModule. Any ideas on why it isn't showing up? Thanks, Eric
  12. Submitted on Jul 07 2008 by
    ruslany
    When you enable CGI option, that will also install FastCGI module. You can check if the module is installed by going to IIS manager -> server node -> Modules.
  13. Submitted on Jul 13 2008 by
    patrickneal3
    I have followed these directions to a t, and i get nuthing no info page no feedback from command prompt, I had the subsite sat up when i starded this process is that why it is not working Default web >site >site >site with php content I have windows vista service pack 1 installed please help!!!
  14. Submitted on Jul 30 2008 by
    jetjaguar
    I was getting the same error as many others when configuring the FastCGI Recycling Behavior... Thanks ntgt13 for catching that extra space. I also have to say how impressed I am by the accuracy and usefulness of this article (considering that it's a "Microsoft" article). Thanks IIS Team.
  15. Submitted on Aug 09 2008 by
    ali.engin
    Thanks, I've installed my php following these instructions and it worked perfectly except that I can't run my pages like ? phpinfo(); ?> I must write it that way ?php phpinfo(); ?> It also doesn't let me to use ?='Hello World!'?> syntax, it must be like ?php print 'Hello World!'?> but I've many sites and codes written that way while I was using apache. Can anybody help about that? Also I don't remember that kind of problem in IIS6. May it be because of my php.ini configuration?
  16. Submitted on Aug 09 2008 by
    ali.engin
    Can I edit my first message? It was my mistake, there was an option in php.ini named short_open_tag and it should be on to use the syntax that I mentioned.
  17. Submitted on Aug 18 2008 by
    jawmusic
    I have the same problem as Eric. I installed Vista SP1, and enabled CGI but there is no mention of a FastCGI module in the modules listing. When I try to configure the handler I get an error saying that the FastCGI Module does not exist. Suggestions?
  18. Submitted on Aug 20 2008 by
    ruslany
    Can you check if the following file exists on the file system: %WINDIR%\system32\inetsrv\iisfcg.dll?
  19. Submitted on Aug 21 2008 by
    jawmusic
    It's not there. I take it it should be? Any recommendations to solve this? I've tried to install FastCGI separately, but it does not recognize a valid installation of Windows (I forget the exact error, but I assume it's because the installer is looking for something other than Vista since it's part of SP1).
  20. Submitted on Aug 21 2008 by
    jawmusic
    It's not there. I take it it should be? Any recommendations to solve this? I've tried to install FastCGI separately, but it does not recognize a valid installation of Windows (I forget the exact error, but I assume it's because the installer is looking for something other than Vista since it's part of SP1). Thanks for your help.
  21. Submitted on Aug 29 2008 by
    chris davies
    To be honest,I've tried every tutorial I can find on how to install php5 and configure it with IIS7 to no avail. I must be missing something here. I have installed PHP5/MySQL5 etc on my XPPro setup which works fine, but I just bought a new laptop which runs Windows Vista Business. I installed IIS7, which worked fine until I attempted to install PHP and now its gone all pear (excuse the pun) shaped. Any advice gratefully recieved.

You must Log In to comment.

Page view counter