FastCGI and PHP 5 on Windows

The LAMP/WAMP (Linux/Windows Apache MySQL & PHP) web server stack has evolved into a viable lightweight network server platform. One of the problems of using WAMP servers is the way CGI processes are evoked. When PHP is used as a CGI process, it is reloaded each time a PHP script is called. A new instance of PHP runs each time a script executes. To get around this limitation, web servers running a FastCGI module can keep a service loaded so it doesn’t need to load anew each time it is evoked.

There is a drawback in using PHP as a FastCGI process on Windows. Normal PHP distributions for Windows have been compiled to be thread safe. PHP for Windows supports multi-threading. That is good for a normal Windows server because Windows is a multi-threaded operating system, and may call a single instance of an application multiple times once it loaded into memory. Unfortunately that conflicts with the multi-processor programs web servers use to run FastCGI. The solution seems simple enough — use a version of PHP that is not thread-safe.

As of PHP 5, the PHP project offers a non-thread safe version of the PHP engine. Up until now, if you wanted a non-thread safe version of PHP you would need to compile it yourself. Now you can download it directly from the PHP distribution site.

The processing advantages are significant. The amount of time needed to handle a single call to PHP can drop from 25%-50%. A page that took 4-5 seconds to process in the past, may now take only 2-3 seconds.

There are caveats however. By far the most important is that FastCGI and PHP on Windows is inherently unstable. Also, some PHP accelerators that depend on Windows multi-threading will no longer work. The trade off depends on you particular application and server load characteristics. Testing is necessary to get a definitive answer about what is stable and best for your particular installation.

Example Installation of Lighttpd, FastCGI and PHP 5

using FastCGI is simple. Lighttpd has the support built-in for both for FastCGI. Lighttpd is a small footprint web server that runs under Linux and Windows. The Lighttpd distribution and is available at:

http://www.lighttpd.net/

This example uses Lighttpd version 1.4.18 for Windows compiled by the WLMP project from Hungary. PHP version 5.2.5 non-thread safe version available from the PHP distribution site at:

http://www.php.net/

Within the Lighttpd configuration file the mod_cgi module has been commented out. You will need to remove the comment. Then, a line is added to the Lighttpd configuration file that redirects PHP files to the PHP processor on port 521, and the cgi.assign statement needs to be commented out.

# cgi.assign = ( ".php" => "c:/lighttpd/php/php-cgi.exe" )
fastcgi.server = (".php" => ("localhost" =>("host" => "127.0.0.1", "port" => 521 )))

The PHP daemon needs to execute before the FastCGI server is loaded for FastCGI to recognize it. Stop Lighttpd, and load the PHP daemon on port 521. The command is:

RunHiddenConsole.exe ..\\php\\php-cgi.exe -b 127.0.0.1:521

The RunHiddenConsole.exe is an optional program available on the Lighttpd web site that will start the PHP daemon without leaving a window open on the desktop. With PHP running you can now start the Lighttpd server as a Windows service. You can stop the PHP daemon at any time by running the command:

process.exe -k php-cgi.exe >nul

If you are unable to get PHP to run using the -b option with an IP and port address, then you don’t have a non-thread safe version of PHP. They are not interchangeable. The thread-safe version of PHP will generate an error when you try to use the -b option. You should not try to run the non-thread safe version as a substitute for the thread safe version. Be forewarned that the stability of your setup will vary, and the cause of instability has not been clearly determined. Good luck!

Tags: , , , , ,

Comments are closed.