Server Error, unable to connect to fastcgi server

Running PHP on IIS in CGI mode is slow and a potential security risk, using ISAPI is fast as it uses the native multi threaded architecture of IIS but has compatibility issues with some of the PECL extensions as they are not thread safe. This is where FastCGI comes in which is an extension to the CGI specification that offers far superior performance and scalability. It is a seemingly little known fact that the father of PHP on Windows, Shane Caraveo, wrote a FastCGI module for IIS way back in 2002. I personally have been using this extension since late 2003 with great success, and it is even used in the IIS Aid PHP Installer you can download from this site. It can be a little tricky to setup at first, and in this article we'll explore some of the more common reasons for being unable to connect to the FastCGI interface when using the PHP scripting language.

1. Check you FastCGI registry settings

The community version of FastCGI for IIS uses the registry to store its settings, and is the most common reason for getting the error in question. The main thing to look for here is to make sure the path specified in the path to the php.exe excutable file is set correctly. To do this start up regedit and look for the following;

HKEY_LOCAL_MACHINE\SOFTWARE\FASTCGI\.php

Then look for this key;

AppPath
Make sure that the path specified in this key is valid and if multiple PHP installs are present make sure it points to the php.exe executable you wish to use. If this path is not set correctly, then you will get the Server Error, unable to connect to fastcgi server error message every time you try and open a PHP page.

2. IIS 6 worker process recycling

If you're using IIS 6 and you have PHP configured using the default application pool, then by default IIS will recycle the worker process serving PHP every 1740 minutes. If you happen to try and open a PHP web page as the worker process is being recycled you might get this error. You can disable worker process recycling, but I wouldn't recommend it as it may well lead to stability issues.

3. Make sure you have sufficient server resources

When you are sizing your server, be aware that FastCGI can consume a lot of memory if you are running a lot of websites and/or PHP applications. When first setting the MaxServers and StartServers values in your FastCGI registry settings, be sure to make them very conservative until you can establish a baseline on how much memory each php.exe process will use. Once you have your baseline and have a good idea on how much memory each php.exe process will use, then your MaxServers value should never exceed you total available physical memory on the server divided by the amount of memory each php.exe process typically uses.

This is especially relevant if you are using a VPS to host your sites as typically memory is rather limited. In the case of using a VPS you might also want to look at having a low StartServers value and setting the IncrementServers to a conservative value. This will mean that FastCGI will only use extra memory resources when the demand is there, and won't generate too many extra php.exe processes at once to consume memory and CPU resources. You might get the Server Error, unable to connect to fastcgi server message if there are insufficient server resources (mainly memory) to create new php.exe processes, or if FastCGI is in the process of creating new php.exe processes. The former will produce more consistent errors for which there is no cure other than to add more memory or reduce the number of websites and/or PHP applications being used. The later should only be temporary and should disappear if you refresh the page.

4. Check your code, libraries and extensions

In some cases it might not be your FastCGI setup or server resources at all. Sometimes PHP applications might contain code, or reference libraries and/or extensions that are not fully compatible with the community version of FastCGI for IIS. It could also be permission related, for example if your PHP application is using COM. In this situation I would first double check the installation instructions for the PHP application in question, and if that doesn't work perhaps try configuring PHP to use normal CGI and contact the developer.

5. You might have found a bug

Nothing is perfect, and FastCGI is no different. In some cases, particularly with new releases of PHP there can be some issues with FastCGI. For instance there is a bug with the community version of FastCGI for IIS using PHP 5.2 which causes a buffer overflow when a packet length is larger then 8192 bytes. Unless you can code there is not a lot you can do here apart from report the bug on the PHP website and wait (hopefully) for a fix.

Hopefully some of the tips above can help you when using FastCGI on IIS. If you have any suggestions you'd like me to add, then please let me know and I'll be happy to add them.


Average rating
(0 votes)
|
Submitted by Dominic Ryan on Tue, 2007-02-06 04:43.

Post new comment

  • Use <!--pagebreak--> to create page breaks.
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will automatically be converted to links.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.