Apache2+PHP5+mod_fcgid

    apache2底下带有支持PHP解析的内嵌模块libapache2-mod-php5,通常都是使用这种方式支持PHP脚本的解析,如今想把二者分开,采用fastcgi的模式运行PHP脚本,这样还能够解析pl、python等脚本,而不用安装apache2自带的内嵌模块。 php

    apt-search以后发现有两个fastcgi模块:一个是mod_fcgid,一个是mod_fastcgi,用哪一个?先搞清楚这两个再说。 html

google一番发现以下: python

    1  mod_fastcgi是FastCGI.com的项目,使用的是FastCGI受权条款。mod_fcgid是Apache基金会负责的项目,使用的是Apache-2.0受权条款。所以,不只主要负责单位不一样,其受权方式也不一样。 web

    2  mod_fastcgi的受权不是标准的自由/开放源码受权条款,因此显得与其余受权条款格格不入,再加上Linux  distribution套件管理政策的因素,使得mod_fastcgi难以全面进入全部Distribution官方库。网上会发现说mod_fastcgi几乎没有再继续开发了,所以也使得网路上许多人转而推荐mod_fcgid。其实这是最大的误解。官方说法是,FastCGI protocol 是一个轻量级且成熟的通信协定,因此不太须要再额外加上新功能,而目前的工做主要在于瑕疵修复,也就几乎没有任何开发进展。 shell

   3  虽然mod_fastcgi与mod_fcgid都支援FastCGI protocol,但设计的概念不一样。mod_fcgid的理念是快速的结束FastCGI server并再快速的产生一个。这个方法与FastCGI protocol初衷不一样,FastCGI protocol是但愿可以容许long-running request。以PHP 应用为例,为了加速PHP 效能,一般会使用bytecode/opcode cache,例如APC、eAccelerator 或Xcache 等。但为了共享cache,意谓着process/request 必须在同一个shared cache中,而没法跨不一样的FastCGI or FCGI process。所以在设计上,只能容许一个PHP process启动,而后由此spawn多个children process,使得children 可以享用parent的bytecode/opcode。 apache

mod_fcgid的官方描述是: 并发

mod_fcgid is a high performance alternative to mod_cgi or mod_cgid, which starts a sufficient number instances of the CGI program to handle concurrent requests, and these programs remain running to handle further incoming requests. It is favored by the PHP developers, for example, as a preferred alternative to running mod_php in-process, delivering very similar performance.

   了解的差很少了 就开始直接动手实验吧,在debian6下相关模块都已经有了,能够直接apt安装。 app

apt-get install apache2 libapache2-mod-fcgid php5-cgi

    网上的例子大可能是配合apache2-suexec模块一块儿使用的,官网是这样描述该模块的:Allows CGI scripts to run as a specified user and Group。设置CGI程序以特定的用户及用户组运行,能够经过SuexecUserGroup指令设置,该指令只影响CGI程序,对于非CGI程序,则不遵照这个规定,仍是会以User指令指定的用户运行的。之前习惯apache以www-data用户运行,配置的时候没注意这一点,致使后面运行权限出错,因此决定不安装该模块。若是要使用该模块,则web的document目录必定要和SuexecUserGroup设置的一致。 less

修改/etc/apache2/mods-enabled/fcgid.conf文件 ide

#AddHandler    fcgid-script .fcgi
AddHandler    fcgid-script .php
WEB配置文件以下:/etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
	DocumentRoot /var/www/html
       MaxRequestsPerProcess  10000
	<Directory /var/www/html>
		Options Indexes FollowSymLinks MultiViews +ExecCGI
               FCGIWrapper /var/www/php-wrapper .php
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>
</VirtualHost>

    PHP applications are usually configured using the FcgidWrapper directive and a corresponding wrapper scriptFcgidWrapper指令在老版本的名称为FCGIWrapper

建立web document环境
mkdir -p /var/www/html
echo '<?php phpinfo(); ?>' > /var/www/html/index.php
touch /var/www/php-wrapper
chmod 755 /var/www/php-wrapper
chown www-data.www-data /var/www/ -R

php-wrapper内容以下:

#!/bin/sh
PHP_FCGI_MAX_REQUESTS=10000
export PHP_FCGI_MAX_REQUESTS
PHP_FCGI_CHILDREN=0
export PHP_FCGI_CHILDREN
exec /usr/lib/cgi-bin/php
该设置的官方解释以下
By default, PHP FastCGI processes exit after handling 500 requests, and they may exit after this module has already connected to the application and sent the next request. When that occurs, an error will be logged and 500 Internal Server Error will be returned to the client. This PHP behavior can be disabled by setting PHP_FCGI_MAX_REQUESTS to 0, but that can be a problem if the PHP application leaks resources. Alternatively, PHP_FCGI_MAX_REQUESTS can be set to a much higher value than the default to reduce the frequency of this problem. FcgidMaxRequestsPerProcess can be set to a value less than or equal to PHP_FCGI_MAX_REQUESTS to resolve the problem.
    PHP child process management (PHP_FCGI_CHILDREN) should always be disabled with mod_fcgid, which will only route one request at a time to application processes it has spawned; thus, any child processes created by PHP will not be used effectively. (Additionally, the PHP child processes may not be terminated properly.) By default, and with the environment variable setting PHP_FCGI_CHILDREN=0, PHP child process management is disabled.

解释的很清楚了,默认的PHP fastcgi进程在处理完500个请求后就会退出,这样会对已经链接并发出请求的应用形成影响,该问题能够经过设置PHP_FCGI_MAX_REQUESTS参数解决,FcgidMaxRequestsPerProcess指令是新版中的名称,在老版本的fcgid中该指令名为MaxRequestsPerProcess。还有一个PHP_FCGI_CHILDREN的问题,它使得PHP建立的子进程不能很快的生效,建议直接禁止该选项。

生效配置

/etc/init.d/apache2 restart
查看效果


注意注意:

   4  但不管如何,PHP的使用者如今已不须要在mod_fastcgi及mod_fcgid之间做选择,由于PHP 5.3.3以后提供的PHP-FPM (PHP FastCGI Process Manager),可以更有效率的解决这个问题。何况PHP-FPM使用的是标准BSD-2-Clause License (BSD两款受权),是个互惠要求性低且相容性高的受权条款。

参考:

   http://www.apachelounge.com/viewtopic.php?t=4385
   http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html
   http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#fcgidwrapper
   http://hi.baidu.com/jackbillow/item/07690b3c02ae6848033edcef

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息