I have always run PHP in ISAPI mode. The biggest disadvantage of this method is its poor stability. When PHP goes wrong, the Apache process will also die. Later, I saw an introduction on the Internet about PHP running in FastCGI mode. The various benefits mentioned (stability, security, high performance) made me decide to give it a try.
However, things were far from going as smoothly as expected. I have searched Google countless times but could not find any information on how to use Apache + FastCGI to run PHP under Windows. There are quite a few people using IIS + FastCGI to run PHP. One of the articles mentioned that Zend also recommends running PHP in FastCGI mode ( http://phplens.com/phpeverywhere/fastcgi-php ).
After several hours of exploration, I finally successfully implemented Apache 1.3 + FastCGI to run PHP.
Preparation:
First visit http://www.fastcgi.com/dist/ to download mod_fastcgi for Apache. I'm using mod_fastcgi-2.4.2-AP13.dll. After downloading, copy this file to Apache's modules directory.
Download PHP (I'm using PHP-5.0.4) and unzip it, modify the php.ini file as needed. Note that you do not need to copy any files from the PHP directory to the Windows directory.
Modify configuration:
Then modify the httpd.conf file and add the following lines:
LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP13.dll
ScriptAlias /fcgi-php5/ "d:/usr/local/php-5.0.4/"
FastCgiServer "d:/usr/local/php-5.0.4/php-cgi.exe" -processes 3
# Note: -processes 3 here means starting three php-cgi.exe processes.
# For detailed parameters of FastCgiServer, please refer to the FastCGI documentation.
AddType application/x-httpd-fastphp5 .php
Action application/x-httpd-fastphp5 "/fcgi-php5/php-cgi.exe"
# Description: Modify the two boldface words above according to the location of your PHP file.
After the modification is completed, just restart Apache.
FAQ:
According to my many experiments, the most common problems are mainly caused by paths. So first check if all paths in php.ini and httpd.conf files are correct. The second is that if PHP has been installed in the system before, then C:Windowsphp.ini needs to be renamed or deleted.
Secondly, check whether the directory where the PHP file is located has sufficient permissions (I set it to Everyone - Full Control, anyway, you don't need to consider so many security restrictions on development machines).
Advantages of running PHP in FastCGI mode:
There are several major benefits to running PHP in FastCGI mode. The first is that when PHP fails, it will not bring down Apache, but PHP's own process will crash (but FastCGI will immediately restart a new PHP process to replace the crashed process). Secondly, FastCGI mode has better performance when running PHP than ISAPI mode (I originally used ApacheBench to test, but forgot to save the results. If you are interested, you can test it yourself).
Finally, you can run PHP5 and PHP4 at the same time. Referring to the configuration file below, two virtual hosts were established, one using PHP5 and the other using PHP4.
LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP13.dll
ScriptAlias /fcgi-php5/ "d:/usr/local/php-5.0.4/"
FastCgiServer "d:/usr/local/php-5.0.4/php-cgi.exe" -processes 3
ScriptAlias /fcgi-php4/ "d:/usr/local/php-4.3.11/"
FastCgiServer "d:/usr/local/php-4.3.11/php.exe"
Listen 80
NameVirtualHost *:80
DocumentRoot d:/www
Options Indexes FollowSymlinks MultiViews
ServerName php5.localhost
AddType application/x-httpd-fastphp5 .php
Action application/x-httpd-fastphp5 "/fcgi-php5/php-cgi.exe"
IndexOptions FancyIndexing FoldersFirst
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
Listen 8080
NameVirtualHost *:8080
DocumentRootd:/www
Options Indexes FollowSymlinks MultiViews
ServerName php4.localhost
AddType application/x-httpd-fastphp4 .php
Action application/x-httpd-fastphp4 "/fcgi-php4/php.exe"
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
Using the above configuration, PHP5 is used when accessing http://localhost/ , and PHP4 is used when accessing http://localhost:8080/ . So as long as it is properly configured, different virtual hosts can use different versions of PHP.
Some disadvantages of FastCGI mode:
After talking about the advantages, let’s talk about the disadvantages. From my actual use, FastCGI mode is more suitable for servers in production environments. But it is not suitable for development machines. Because when using Zend Studio to debug the program, FastCGI will think that the PHP process has timed out and return a 500 error on the page. This was so annoying that I switched back to ISAPI mode on my development machine.
Finally, there is a potential security vulnerability in FastCGI mode on Windows. Because I haven't found a way to implement SuEXEC in a Windows environment, the PHP process always runs with the highest privileges, which is obviously not good news from a security perspective.
Conclusion:
The application of FastCGI in this article is still at a very preliminary stage, and is only used as a starting point, hoping that more mature solutions will emerge.