Using FastCGI to Host PHP Applications on IIS 6.0

Introduction

This article explains how to use the Microsoft IIS 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 application frameworks' CGI executables to interface with the web server. It differs from the standard CGI protocol in that FastCGI re-uses CGI processes for multiple requests, which provides a significant performance boost as compared to CGI. The IIS FastCGI support enables IIS to host normal CGI programs like PHP or Ruby On Rails with the FastCGI protocol and offer high-performance and stability for production deployment of such application frameworks.

Using the IIS FastCGI support involves the following:

  • The IIS web server
  • 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 launches 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 re-used for a subsequent request. This helps to avoid the high performance penalty of starting a new process for each request, which results in much better performance and scalability in a production environment.

To learn more about the FastCGI protocol, see http://www.fastcgi.com/devkit/doc/fcgi-spec.html.

Installing the FastCGI Extension for IIS 6.0 and IIS 5.1

The FastCGI extension for 32bit and 64bit Windows XP and Windows Server 2003 can be downloaded here:

http://www.iis.net/extensions/fastcgi

When you run FastCGI installer, it copies FastCGI specific files to "%WINDIR%\system32\inetsrv" folder and then registers and enables FastCGI Web Server Extension. Out of the files that the installer copies, be aware of the following:

  • 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 of file extensions to FastCGI processes. It also contains the configuration of FastCGI process pools.
  • fcgiconfig.js – This configures FastCGI extension. The script updates the fcgiext.ini file, modifies the IIS metabase and recycles the web service application pools if necessary.

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 available from http://windows.php.net/download. Since the FastCGI extension hosts and executes php processes in a single threaded manner, it is recommended that you download and install the non-thread-safe PHP build available for Windows. The non-thread-safe PHP build provides a performance boost from a lack of thread safety that is acceptable for a single-concurrency environment like FastCGI.

Configuring FastCGI Extension to Work with PHP

Once PHP is installed on Windows, you have two options for configuring FastCGI: use the configuration script provided with the installation of the FastCGI extension, or perform all the configuration tasks manually by modifying IIS configuration settings and fcgiext.ini file. Depending on the option, refer to the corresponding sections below.

Using Script for Configuring FastCGI Extension to Work with PHP

The configuration script with the name fcgconfig.js is provided with the installation of FastCGI extension and is located in %WINDIR%\system32\inetsrv. To configure FastCGI extension to work with PHP, follow the steps below:

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

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

Manually Configuring FastCGI Extension to Work with PHP

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

Create script mapping

  1. Launch inetmgr.exe.
  2. Double click the machine icon for the local computer.
  3. Right click on "Web Sites" and pick "Properties."
  4. Click the "Home Directory" tab. 
  5. Click the "Configuration…" button.
  6. Click the "Add…" button.
  7. Browse to the fcgiext.dll located in %WINDIR%\system32\inetsrv.  

Note: If you need to use FastCGI in 32 bit mode on a 64 bit machine, then browse to %WINDIR%\SysWOW64\inetsrv.

    8.  Enter .php as the Extension.

    9.  Enter Verbs as GET,HEAD,POST .

  10.  Ensure that "Script Engine" and "Verify that file exists" are checked.

    11.  Click OK.

Modify fcigext.ini file

Once the script mapping is added, modify the fcgiext.ini file:

  1. Add an extension to application mapping (php=PHP) to the [Types] section.
  2. 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 fcgiext.ini the file are saved, restart the application pool associated with the web site that hosts your PHP applications.

Configure default document in IIS

Most of the PHP applications use a file named index.php as the default application document. Configure IIS to treat this file as the default content page:

  1. Launch inetmgr.exe.
  2. Double click the machine icon for the local computer.
  3. Right click "Web Sites" and pick "Properties."
  4. Click the "Documents" tab.
  5. Click the "Add…" button and enter "index.php" for "Default Document Name:" 
  6. Click "OK."
 

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

Full PHP functionality support and performance of PHP applications running on the FastCGI extension were the main goals and focus for the IIS team. During our internal testing and in collaboration with Zend Technologies, we identified the set of configuration settings for the FastCGI extension and PHP that provide optimal functionality and performance for PHP applications when running on Windows via the FastCGI extension.

Configure your server as described below:

    1.  Modify the php.ini file as follows:

  • Set fastcgi.impersonate = 1. FastCGI under 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 *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 specifications. Setting this to 1 causes PHP CGI to fix its paths to conform to the specification.
  • Set cgi.force_redirect = 0.

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

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

To test, create and request a phpinfo.php page in your site that contains the following:

<?php

 

phpinfo();

 

?>

You see something like:


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

Getting More Information

To discuss the usage of the FastCGI Extension, or file bug reports, please use the FastCGI forums:

To get more information regarding running various PHP applications on IIS, refer to:

Related Content

Comments

Excellent article - thanks!

You should also read the "Configuring FastCGI Extension for IIS 6.0" article
at http://learn.iis.net/page.aspx/248/configuring-fastcgi-extension-for-iis60/ which has more details about configuring FastCGI and the fcgiext.ini file.

Jul 24 2008 by DiamondEagle

Hi,
I'm new to iis/php.
I followed the same procedure explained in my windows xp system.
And from my html file, i called the php script on a submit button (with action="post" method="trial.php").

Still, when i click on submit, it says "The page can not be displayed".

Can anyone help me please?

Regards,
Jaas

Mar 03 2009 by jasnif

Hi,

I am not a server guy but am a developer. I have probably spend the most frustrating 3 days of my career of 24 years trying to configure wordpress to run on IIS 6.0. After following all the instructions to the letter (and using common sense where things were left out) I still cannot get this to work. As soon as I get through step 1 and try to continue to step 2 of thwe wordpress installation I get a permissions (403) error and can go no further. I have tried everything and cannot overcome thos issue. Is there a resolution for this or not? I tried a different php application that was used in one of the artices (phpBB3) and have the exact same problem.

In a perfect world, everyone would just develop using .net but unfortunately this is not the case. Now I am wondering what to tell clients that require a mixed environment. For that matter I have a bunch of php apps I would like to at leasst check out but don't want to waste time trying to configure them.

Is there a definitive A to Z guide on how to set this stuff up properly complete with guidance on how to setup the required permissions for foders that need to execute scripts?

Regards

Gary

May 07 2009 by garymich

I was earlier using WAMP but now i started using IIS with PHP and mysql and had to setup all from scratch. I later came to know about fastcgi.. then led to this site..

No words to describe this page helped me a lot in configuring and settingup php5 for IIS and all that very efficently ..

Thank you so much.. I wish to proceed now without much trouble

Jun 13 2009 by anandn26

Nice guide! I managed to install PHP 5.3.0 in a Windows Server 2003 R2 x64

I would add information about the problems I faced:

1. I downloaded the version 5.3.0 (non thread safe) and I used the php.ini from "php-production"

2. I set in there "safe_mode" = On and "file_uploads = Off" (My PHP site didn't need that so the less services available the more secure it is)

3. If you get the error "503 Access Denied" when running PHP you might need to grant NTFS "Read/Execute" and "Directory Listing" permissions to the user running IIS (I set it to "USERS")

4. I also got an error (PHP warning) stating I must set the timezone correctly. To fix that just set the time zone in PHP.ini, I added:
date.timezone = "Europe/London"

Thanks again.

Jul 21 2009 by gelilloabad

I am attempting to get PHP5.3 installed on XP IIS5.1 with FastCGI.
When I attempt to browse to a php file on localhost, I still get the prompt to download the file.
Not really sure where to go from here, any and all assistance appreciated.

Aug 09 2009 by guillermo.debiase

I also installed PHP 5.3 on XP IIS 5.1 using the instructions provided. I also cannot display PHP files, the browser attempts to download the PHP file. What are the possible causes of this?

Aug 18 2009 by Kisama

it'd be nice if we knew what output to expect from the >cscript fcgiconfig.js commands. fcgiconfig.js(1223, 5) (null): 0xFFFFFFDF doesn't mean a thing to me and we have no way of knowing if the command was completed correctly...

Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.

C:\WINDOWS\system32\inetsrv>cscript fcgiconfig.js -add -section:"PHP" -extension
:php -path:"C:\PHP\php-cgi.exe"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\WINDOWS\system32\inetsrv\fcgiconfig.js(1223, 5) (null): 0xFFFFFFDF


C:\WINDOWS\system32\inetsrv>cscript fcgiconfig.js -set -section:"PHP" -InstanceM
axRequests:10000
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

INI successfully written.

C:\WINDOWS\system32\inetsrv>cscript fcgiconfig.js -set -section:"PHP" -Environme
ntVars:PHP_FCGI_MAX_REQUESTS:10000
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

INI successfully written.

Aug 25 2009 by captaingeek@gmail.com

also closely followed the instructions all I'm getting is http 500 errrors.

Aug 25 2009 by captaingeek@gmail.com

I also closely followed these instructions and the PHPInfo page is blank. No errors or anything. I added "Hello World" to PHPInfo page, and that is displayed in the browser.

PHP NTS 5.3 intaller (VC9)
2008 C++ Runtime
Windows 2003 Standard x64

Sep 15 2009 by Difranco

Hi,
I also have a 500 error after following all these steps...

Sep 24 2009 by Hardt

I have the same problem as guillermo and Kisama. I've installed php 5.2.11 on IIS 5.1 (Windows XP) using the web platform installer, but when I go to test I get prompted to download the php file. Very frustrating.

Oct 07 2009 by batman++

You refer to 'php-cgi.exe' but after installing PHP 5.2.11 there is only 'php-win.exe' and php.exe' present.

Are either of these the cgi version?

Oct 16 2009 by seanys

Yep, I'm going to throw my towel into the frustration hat, too. I've followed these instructions as well as every other instruction I can find in this IIS forum about installing PHP on IIS. I've followed the instructions precisely. Installed and reinstalled...with no success. My setup is XPSP2 and IIS 5.1.

When trying to run a .php file, I get "The page cannot be found". It's a 404 error. Somehow I'm guessing that this situation might have something to do with permissions, but I'm not sure.

What's even more frustrating is that there seem to be A LOT of people have similar issues with installing PHP and there doesn't seem to be anyone who had the problem coming back to the forum with their answer, after they solve the issue. This issue has to be solved somehow. It's probably just something simple.

The guys on this IIS Team don't seem what to do except keep telling us the same over and over. I'll keep working on it. I'm about to do an uninstall, again and try the "Installer Package" from PHP.net.

Let's get this figured out!

Nov 16 2009 by AntiAlias

xp and iis5.1

I have worked through the php manual install, the msi auto and now the wpi all with the same end result. Error The specified module could not be found.
help would be greatly accepted

Nov 19 2009 by jamie hollingsworth

xp and iis5.1

I have worked through the php manual install, the msi auto and now the wpi all with the same end result. Error The specified module could not be found.
help would be greatly accepted

Nov 19 2009 by jamie hollingsworth

This article is clear and fairly concise. One thing I noticed is that the output from the phpinfo() call lists that the version of PHP has "Thread Safety" enabled.

The reason for my scrutiny is that I have followed this article very closely and others and now I have a site that all too often is receiving error 0x80004005 - "The FastCGI process exited unexpectedly".

If you have more information that will help me diagnose this issue or a way to prevent it please respond.

Thanks
Dave

Dec 01 2009 by dave kitzmiller

For 403 error on POST requests (response to garymich)

I have experiment the same issue during a couple of hour.

Just check the chain "GET,HEAD,POST" ... you may not have any spaces.

If you have filled "GET, HEAD, POST" : just GET method will be authorised

Dec 03 2009 by swo75

Hi,
I also agree that this article is clear and fairly concise.

However, while following these install instructions, along with the PHP 5.3.1 (the non thread safte) instructions for Windows (using both the binaries and manual instructions which I have tried multiple times), I keep getting a HTTP 500 whenever I tried to run a php script/program from within the IE browser (v7 and v8) .

Like many of the comments from others here, I'm very frustrated, too. I've followed these instructions as well as every other instruction I can find in this IIS forum and others on installing PHP on IIS. I've followed the instructions precisely, and have installed and reinstalled, all with no success. As I mentioned, I get keeping an HTTP 500.

My setup is IIS 6 on a Windows 2003 Server with the latest updates

I have however, I can run PHP through the command prompt window.

Has anyone got this work? If could you please share the information?

Thanks
Carl

Dec 18 2009 by Chenn

Hey Guys,
I've been having a helluva time getting this workign as well, but just got things working.
My System - windows 2003 x64 server, iis6, 2008
Microsoft Visual C++ 2005 SP1 Redistributable Package
Microsoft Visual C++ 2008 Redistributable Package
Microsoft Visual C++ 2008 SP1 Redistributable Package
Running exchange 2007/Sharepoint 2007
Multiple Websites (using host header vaules)

FIRST - if you are getting the PHP CODE on your screen (ie ?php phpinfo(); ?>) MAKE SURE your PHP file is good. I created my first one in dream weaver - (Tradgic mistake - lots of superflous header info that cause php to get tripped up!!! I only found this by manually running the PHP command from the command prompt. You can test this too to make sure that PHP is WORKING and PROCESSING your file correctly.. Here's how:

TESTING PHP functionality:
Open command prompt
navigate to your php directory (I used the default c:\php)
C:\php>php -info | more

i use | more to help with scolling the output should be something like this:

phpinfo()
PHP Version => 5.3.3-dev
System => Windows NT {hostname} 5.2 build 3790 (Windows Server 2003 Enterprise
x64 Edition Service Pack 2) AMD64
Build Date => Dec 28 2009 02:15:13
Compiler => MSVC9 (Visual C++ 2008)
Architecture => x64
Configure Command => cscript /nologo configure.js "--enable-snapshot-build" "--
....

This proves that PHP is working. If you don't get this, you've got to fix your PHP install. Blow the directory away, and get a good PHP build.

At this point in time, I'd like to take a moment to mention that I got my copy of PHP from http://windows.php.net/snapshots and I used the NTS (Non Thread Safe) version built on Dec 28th at 0600. I've still got the binary and can post is if someone needs it.) I'd also like to mention that i used the .ZIP version because the installer failed for me with error 2711 (I've raised a bug on the PHP site). After downloading the zip, i unpacked it to c:\php then COPIED (keeping the original just in case) the php-production.ini to php.ini.

My mods to that file were:

uncomment:
extension_dir = "ext"
cgi.force_redirect = 1
cgi.fix_pathinfo=1
fastcgi.impersonate = 1;

modify
date.timezone = America/Montreal

- That's pretty much it! make sure it's saved in c:\php\php.ini. If you choose not to install default then modify your paths accordingly.

Not that you've confirmed PHP is working from command line, make sure you have a good phpinfo.php file.

you can test it by getting PHP to try and 'execute' your file.
do this typing this command from your php install directory:

C:\php>php -f c:\inetpub\myphptestsite\phpinfo.php | more

The output of that command should be something line this:
phpinfo()
PHP Version => 5.3.3-dev

System => Windows NT {hostname} 5.2 build 3790 (Windows Server 2003 Enterprise
x64 Edition Service Pack 2) AMD64
Build Date => Dec 28 2009 02:15:13
Compiler => MSVC9 (Visual C++ 2008)
Architecture => x64

As you can guess, the test site "myphptestsite" is installed in the default iis directory, and the file I created is called phpinfo.php and located at the root of my site.

The contents of my file looks are EXACTLY this:

?php phpinfo(); ?>


There is no HTML, NO body, or header. just that text.

IF php -info is working and this is not, then check your phpinfo.php files path, contents and permissions - outside of that, I don't know what else could be wrong.

NEXT we'll finish PHP verification by using the php-cgi file. This is the file that formats php output into web code, I think ;). I'm no expert, but was poking around and found this out.

RECAP - you've tested php -info and php -f phpinfo.php and both commands worked

From the command line, navigate to your php directory and type:
C:\php>php-cgi -f C:\inetpub\myphptestsite\phpinfo.php | more

the output should be something like this

Dec 28 2009 by wgiz

Windows 2003 x64 FastCGI/PHP PT2
Troubleshooting FastCGI
Make sure FASTCGI was installed properly.
Following the steps above download and install x64 version FastCGI (fcgisetup64.msi)
http://www.iis.net/expand/fastcgi (its on the right side)
Configure by following this instructions above.
Open a command prompt, navigate to "%WINDIR%\system32\inetsrv"

and type one of these two commands:
cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"C:\PHP\php-cgi.exe"
OR
cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"C:\PHP\php-cgi.exe" –site:1

The first command enables PHP on all of your sites, the second specifies the site to run enable FastCGI on.

This is where I left the trail a bit. I wanted to verify that the script was working correctly, so I followed the MANUAL configuration as well, but instead of executing and clicking, I VERIFIED that the script had worked correctly.I ran the FIRST command to enable PHP on all of my sites. To verify I opened the IIS admin console, then right clicked on "Web Sites" and selected Properties. Then I went to the Home Directory tab and clicked on Configuration. I scrolled through the "Application Extensions" box and sure enough, .php was there and configured correctly as per the manual directions, so I canceled the dialogs. Next I right clicked on myphptestsite and clicked on the Documents tab. I noticed that index.php was not there, so I added it. I also read somewhere that the .php mime type had to be registered, so I did that by (again) rt clicking on myphptestsite and selecting properties. This time I clicked on the http headers tab, then the mime types button and noticed that it was empty, so I added .php as an extension and text/html as a mime type. This may not be necessary however...

back on the trail!

OK - so next continue on with the "Setting FastCGI Configuration ..." section above, followed to the tee.

Now when you run http:\\www.mytestphpsite/phpinfo.php you should get that output shown above, and from the php testing. If not you can try trouble shooting FastCGI a bit. I'm going to provide BASIC troubleshooting tips:

First ensure that fastcgi is properly being called - we're going to rename the main dll responsible for processing fastcg.

First stop the www process by going to computer management and stopping the "World Wide Publishing Service". Verify it has stopped by browsing to a known page on the server. You should get an "Internet Explorer cannot display the webpage" error. This indicated that the services has stopped. You do this because fastcgi is loaded once into ram, and renaming the file after won't affect the output of the error we're trying to create.

Once it has stopped, navigate to the location where fastcgi is installed. The installer should have placed it in %WINDIR%\system32\inetsrv

There will be a file there called "fcgiext.dll"

Temporarily RENAME that file to fcgiext.dl_

Restart www publishing service (above)
Now use a web browser to call a php page
http:\\www.myphptestsite.com/phpinfo.php

The browser should fail and give you an error like this:

HTTP 500 Internal Server Error
The website cannot display the page
HTTP 500
Most likely causes:
•The website is under maintenance.
•The website has a programming error.


This is a GOOD THING because it proves that yout IIS server is looking in the right spot for your FastCGI file If you're not getting this error:

VERIFY the fcgiext.dll and ini files are located %WINDIR$%\system32\inetsrv directory. If not, reinstall FastCGI and verify permissions on the fcgiext (dll and ini) files

VERIFY the Create script mapping steps above. Specifically steps 7-10
7 - ENSURE you have selected the correct file (you'll have to name it back to dll if you changed it in the prev step
8 - make sure you've got .php in there and not just

Dec 28 2009 by wgiz

correction my email is my username here (wgiz) at rogers dot com.

Dec 28 2009 by wgiz

correction my email is my username here (wgiz) at rogers dot com.

And I've noticed that some of my first part was truncated. That sucks..

Dec 28 2009 by wgiz

I won't be coming back here, so if you need more help, reach me at my username (wgiz) at rogers dot com

Dec 28 2009 by wgiz

Submit a Comment

You must Log In to comment.