一、什么是fastcgi
fastcgi是一个与web应用程序进行内部连结调用的协议,是HTTP服务器与机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上。CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl,python等使用fastcgi的主要目的是为了减少服务器和CGI程序相关的开销,允许一台服务器一次处理更多的网页请求。
nginx和lighttpd是内建fastcgi支持的,而apache需要调用mod_fastcgiak mod_fcgid模块来实现。
具体介绍可以到http://baike.baidu.com/view/641394.htm查看或到fastcgi主页http://www.fastcgi.com/drupal/查看。
二、为什么使用mod_fastcgi代替mod_perl/mod_php/mod_python
以下是维基百科给出的答案:

Instead of creating a new process for every request, FastCGI can use a single persistent process which handles many requests over its lifetime. Processing of multiple requests simultaneously is achieved either by using a single connection with internal multiplexing (ie. multiple requests over a single connection) and/or by using multiple connections. Many such processes can exist, something that can increase stability and scalability. FastCGI also allows programs to get the web server to do certain simple operations, like reading in a file, before the request is handed over. Environment information and page requests are sent from the web server to the process over a TCP connection (for remote processes) or Unix domain sockets (for local processes). Responses are returned from the process to the web server over the same connection. The connection may be closed at the end of a response, but the web server and the process are left standing.
Many web site administrators and programmers are finding that the separation of web applications from the web server in FastCGI (and the simpler SCGI) has many desirable advantages over embedded interpreters (mod_perl, mod_php, etc.). This separation allows server and application processes to be restarted independently — an important consideration for busy web sites. It also facilitates per-application security policies — important for ISPs and web hosting companies.
三、安装
1、先安装http服务 yum install libtool httpd-devel apr-devel apr
2、下载fastcgi源码包wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
tar -zxvf mod_fastcgi-current.tar.gz
cd mod_fastcgi-2.4.6/
cp Makefile.AP2 Makefile
32位系统下的编绎安装:
make top_dir=/usr/lib/httpd
make install top_dir=/usr/lib/httpd
64位系统下的编绎安装:
make top_dir=/usr/lib64/httpd
make install top_dir=/usr/lib64/httpd
Configure mod_fastcgi
Open /etc/httpd/conf.d/mod_fastcgi.conf file
# vi /etc/httpd/conf.d/mod_fastcgi.conf
输入以下内容启用mod_fastcgi模块
LoadModule fastcgi_module modules/mod_fastcgi.so
重启http服务
# service httpd restart
如果想以fastcgi模式使用,你要先关闭mod_php模式
# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.disable
创建一个shell脚本php.fcgi在cgi-bin目录
vim /var/www/cgi-bin/php.fcgi (或者存放到你站点的虚拟目录里)
内容如下:
#!/bin/bash
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
# Tested under Red Hat Enterprise Linux / CentOS 5.x
### Set PATH ###
PHP_CGI=/usr/bin/php-cgi
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
### no editing below ###
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGISet permission, type:
# chmod +x /var/www/cgi-bin/php.fcgi
最后,对DocumentRoot目录目录设置如下,使其运行在mod_fastcgi模式下

Options -Indexes FollowSymLinks +ExecCGI
AllowOverride AuthConfig FileInfo
AddHandler php5-fastcgi .php
Action php5-fastcgi /cgi-bin/php.fcgi
DirectoryIndex index.php index.html
Order allow,deny
Allow from all

#service httpd restart
mod_fastcgi虚拟主机配置
域名: XXX.com
网站主目录: /websites/home/XXX.com/http
cgi-bin目录: /websites/home/XXX.com/cgi-bin
php.fcgi路径: /websites/home/XXX.com/cgi-bin/php.fcgi
日志文件目录: /websites/home/XXX.com/logs
具体配置如下:

ServerAdmin webmaster@XXX.com
DocumentRoot “/websites/home/XXX.com/http”
ServerName XXX.com
ServerAlias www.XXX.com
ErrorLog “/websites/home/XXX.com/logs/error.log”
CustomLog “/websites/home/XXX.com/logs/access.log” common
ScriptAlias /cgi-bin/ “/websites/home/XXX.com/cgi-bin/”

Options -Indexes FollowSymLinks +ExecCGI AllowOverride AuthConfig FileInfo AddHandler php5-fastcgi .php Action php5-fastcgi /cgi-bin/php.fcgi Order allow,deny Allow from all AllowOverride None Options None Order allow,deny Allow from all 虚拟目录创建的php.fcgi文件如下: \#!/bin/bash PHP\_CGI=/usr/bin/php-cgi PHP\_FCGI\_CHILDREN=4 PHP\_FCGI\_MAX\_REQUESTS=1000 export PHP\_FCGI\_CHILDREN export PHP\_FCGI\_MAX\_REQUESTS 赋予php.fcgi执行权限并重启http: \# chmod +x /websites/home/XXX.com/cgi-bin/php.fcgi \# service httpd restart 四、其他平台fast-cgi配置文件