Running PHP/MapScript as FastCGI Under Windows and IIS

As mentioned before, a recent run in with a busload of traffic made me decide to switch from running PHP as CGI to FastCGI, and I’m a better person for it.

Here are some instructions on setting up PHP/MapServer to run as FastCGI under Windows/IIS. The instructions are very similar to the ones I posted on setting up PHP/MapServer as CGI - the only change is a slightly different IIS setting and an addition to your registry. If you already have PHP and MapServer set up, skip to the IIS and registry settings section, and copy the isapi_fcgi.dll from the following PHP zip folder to yours. Note that my examples will be based on IIS 5, but IIS 6 setup will be almost identical.

The first thing we’ll do is copy some folders to your machine. This zip file has PHP with its configuration file (php.ini) set up with some useful options and the Mapscript dll copied to its extensions folder. Download the zip file and extract the PHP folder to C:\ (i.e. C:\PHP).

Then copy some binaries Mapserver needs from other open source projects. Download this zip file and copy the folder to C:\ (i.e. C:\mapserver_binaries). Mapserver also needs files from another project to handle projections. Download this zip file and move the folder to C:\ (i.e. C:\proj).

Next, we’re going to add a couple of pointers to these folders to your PATH variable. Right click on My Computer and go to Properties. Go to the Advanced tab and click on Environmental Variables. In the System Variables, highlight the Path variable and hit Edit. Add this to the end of the line: “;C:\PHP;C:\mapserver_binaries” (minus the quotes). We’re telling windows to look in those folders when searching for executables.

Next, we’re going to run a .reg file to add some FastCGI settings to your registry. There’s a sample fastcgi.reg in the zipped PHP folder linked here, but here’s basically what it looks like:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\FASTCGI]

“StartServers”=dword:00000005

“IncrementServers”=dword:00000002

“MaxServers”=dword:00000019

“Timeout”=dword:00000258


[HKEY_LOCAL_MACHINE\SOFTWARE\FASTCGI.php]

“AppPath”=”c:\php\php-cgi.exe”

“BindPath”=”php-fcgi”


We’re telling FastCGI to begin with 5 processes and scale up to a maximum of 19. Then we’re specifically telling FastCGI how to handle files with a .PHP extension (you can use FastCGI for a lot of different things, not just PHP). You can find more information on different settings you can use here. Run the .reg file, and you’re set. Now we’re off to IIS.

With the IIS manager, right-click on your default web site and hit “Properties”. From there, head to the ISAPI Filters tab (yours won’t have the PHP5 ISAPI in there yet).

Next, you’ll want to click Add, give it a name of “PHP5 ISAPI”, and give it a location of “C:\PHP\isapi_fcgi.dll” (if you’ve put it somewhere else….you know).

Work your way out of that dialog and head to the “Home Directory” tab. Click the “Configuration…” button, and you’ll get a dialog like this (minus a “.php” extension if you haven’t done this before).

Click Add (or Edit if you already have a .php extension setting), and make the setting look like this:
Basically, you’re telling IIS to use “C:\PHP\isapi_fcgi.dll” for requests for .php pages.

Next, restart IIS by right clicking on your server name (generally your machine name) and selecting “All Tasks” and “Restart IIS…”.

That’s it. When IIS restarts, you should see five php-cgi.exe processes running. These were started by FastCGI and are awaiting requests. Just to check though, make a test.php with the phpinfo function ( <? phpinfo(); ?> )and see if you get a good looking page with PHP information back.

That’s it. Your PHP applications, including MapServer with PHP/MapScript, will run a good bit faster now.