我一直以来都是以 ISAPI 模式运行 PHP 的,这种方式最大的缺点就是稳定性很差,当 PHP 出错的时候,Apache进程也死掉了。后来看到网上关于 PHP 以 FastCGI 模式运行的介绍,其中提到的种种好处(稳定、安全、高性能)让我决定尝试一下。
不过事情远不如预计的那么顺利。Google 了无数次也没找到一篇如何在 Windows 下用 Apache + FastCGI 跑 PHP 的资料,IIS + FastCGI 跑 PHP 的却是很多。其中有一篇提到 Zend 公司也推荐以 FastCGI 模式运行 PHP( [url]http://phplens.com/phpeverywhere/fastcgi-php[/url])。
通过几个小时的摸索,终于成功实现了 Apache 1.3 + FastCGI 跑 PHP。
准备工做:
首先访问 [url]http://www.fastcgi.com/dist/[/url]  下载用于 Apache 的 mod_fastcgi。我使用的是 mod_fastcgi-2.4.2-AP13.dll。下载后将这个文件复制到 Apache 的 modules 目录中。
下载 PHP(我使用的 PHP-5.0.4),并解压缩,根据须要修改 php.ini 文件。 注意不须要从 PHP 所在目录复制任何文件到 Windows 目录中。
修改配置:
而后修改 httpd.conf 文件,加入下面几行:
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
# 说明:此处的 -processes 3 表示启动三个 php-cgi.exe 进程,
# 关于 FastCgiServer 的详细参数请参考 FastCGI 文档。

AddType application/x-httpd-fastphp5 .php
Action application/x-httpd-fastphp5 "/fcgi-php5/php-cgi.exe"
# 说明:根据你的 PHP 文件所在位置修改上面两处黑体字。
修改完成后,从新启动 Apache 便可。
常见问题:
根据个人屡次试验,最容易出现的几个问题主要都是由路径引发的。所以首先检查 php.ini 和 httpd.conf 文件中的全部路径是否正确。其次就是若是系统中之前安装过 PHP,那么要将 C:\Windows\php.ini 更名或删除。
其次还要检查 PHP 文件所在目录是否有足够的权限(我都是设置为 Everyone - 彻底控制,反正开发用机不用考虑那么多安全限制)。
FastCGI 模式运行 PHP 的优势:
以 FastCGI 模式运行 PHP 有几个主要的好处。首先就是 PHP 出错的时候不会搞垮 Apache,只是 PHP 本身的进程当掉(但 FastCGI 会当即从新启动一个新 PHP 进程来代替当掉的进程)。其次 FastCGI 模式运行 PHP 比 ISAPI 模式性能更好(我原本用 ApacheBench 进行了测试,但忘了保存结果,你们有兴趣能够本身测试)。
最后,就是能够同时运行 PHP5 和 PHP4。参考下面的配置文件,分别创建了两个虚拟主机,其中一个使用 PHP5,另外一个使用 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
<VirtualHost *: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"
    <Directory "D:/www">
        IndexOptions FancyIndexing FoldersFirst
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
Listen 8080
NameVirtualHost *:8080
<VirtualHost *:8080>
    DocumentRoot d:/www
    Options Indexes FollowSymlinks MultiViews
    ServerName php4.localhost
    AddType application/x-httpd-fastphp4 .php
    Action application/x-httpd-fastphp4 "/fcgi-php4/php.exe"
    <Directory "D:/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
使用上面的配置,访问 [url]http://localhost/[/url] 就使用 PHP5,而访问 [url]http://localhost:8080/[/url] 就使用 PHP4。因此只要合理配置,就可让不一样的虚拟主机使用不一样版本的 PHP。
FastCGI 模式的一些缺点:
说完了好处,也来讲说缺点。从个人实际使用来看,用 FastCGI 模式更适合生产环境的服务器。但对于开发用机器来讲就不太合适。由于当使用 Zend Studio 调试程序时,因为 FastCGI 会认为 PHP 进程超时,从而在页面返回 500 错误。这一点让人很是恼火,因此我在开发机器上仍是换回了 ISAPI 模式。
最后,在 Windows 中以 FastCGI 模式存在潜在的安全漏洞。由于我尚未找到如何在 Windows 环境下实现 SuEXEC 的方法,所以 PHP 的进程老是以最高权限运行,这从安全角度来看显然不是个好消息。
结束语: 本文对 FastCGI 的运用还停留在很初级的阶段,仅看成抛砖引玉,但愿有更成熟的解决方案出现。