apache下php配置fastcgi代替mod_php

一、fastcgi的好处:

1. FastCGI 像是一个常驻 (long-live) 型的 CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去 fork 一次 (这是 CGI 最为人诟病的 fork-and-execute 模式)。
  2. FastCGI 可在任何平台上使用,Netscape Enterprise 及 IIS 都有 FastCGI 的模块可供使用,阿帕契 (Apache,以及利用 Apache 衍生出做的服务器) 上也有 mod_fastcgi 可用。
  3. FastCGI 支持 C/C++、JavaPHP、Python、Ruby、Perl,Tcl 等程序语言。
  4. FastCGI 的应用程序亦兼容于 CGI。即 FastCGI 的应用程序也可以当成 CGI 来执行。
  5. 现有的 CGI 程序要改写成 FastCGI 非常简单,最少可能只需要多加入三行程序代码
  6. FastCGI 的侦错方式与 CGI 大同小异,只要带入程序所需的环境变量及参数,即可在命令列模式执行或侦错。
  7. FastCGI 应用程序的写作方式与 CGI 类似,除了几项原则要特别注意外,FastCGI 的写作方式跟 CGI 几乎一样,与学习 Web Server API 比较起来, FastCGI 简单多了。
  8. FastCGI 支授分布式运算 (distributed computing),即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。

二、性能比较

有两种观点,有说使用fastcgi替代mod_php后,速度更快了,效率fastcgi更胜一筹;但是我从实际情况来看,应该是mod_php比fastcgi要快一点的。老外有个Drupal爱好者写了一篇测试文章《mod_php or FastCGI?》(http://buytaert.net/drupal-performance?page=1),里面提到"When switching from mod_php to FastCGI we observe a 63% slowdown for anonymous visitors, and a 18% slowdown for authenticated visitors.",图标如下:

apache下php配置fastcgi代替mod_php

apache下php配置fastcgi代替mod_php

fastcgi官方的说法:

Of course, the answer is that it depends upon the application. A more complete answer is that FastCGI often wins by a significant margin, and seldom loses by very much.

个人观点:

如果在性能相差不大的情况下,还是更推荐使用fastcgi,那样的话可以更灵活,更安全。使用fastcgi,你的web server 可以比较简单的切换,可以测试不同的服务器,Apache,lightty,ngix 等等,不需要有代码的修改。再者,如果想换脚本的实现,如不用php,而是改成perl,python之类的,web服务器也不需要任何的改动。

三、配置

1.编译安装php,不能带–with-apxs2=,带上--enable-fastcgi
2.安装mod_fcgid
wgethttp://jaist.dl.sourceforge.net/sourceforge/mod-fcgid/mod_fcgid.2.2.tgz

tar zxvf mod_fcgid.2.2.tgz
cd mod_fcgid.2.2
make
make install

( )

vi /usr/local/apache2/conf/httpd.conf,加入:

### fastcgi ###
LoadModule fcgid_module modules/mod_fcgid.so
ScriptAlias /fcgi-bin/ "/usr/local/php/bin/"
AddHandler php-fastcgi .php
Action php-fastcgi /fcgi-bin/php-cgi
AddType application/x-httpd-php .php

<IfModule mod_fcgid.c>
#    AddHandler fcgid-script. .php .py .pl .fcgi
    AddHandler fcgid-script. .php .fcgi   ### 暂时只配置支持.php
    IdleTimeout 300
    ProcessLifeTime 1800
    MaxProcessCount 100
    DefaultMinClassProcessCount 3
    DefaultMaxClassProcessCount 8
    IPCConnectTimeout 15
    IPCCommTimeout 300
    MaxRequestsPerProcess 100
    # RewriteCond %{REQUEST_URI} ^/fcgi-bin/.* [NC]
    # RewriteRule .*/php$ /nonexistent.file [L]
</IfModule>

# <Files ~ (/.php)>
#    SetHandler fcgid-script
#    FCGIWrapper /usr/local/php/bin/php .php
#    Options +ExecCGI
#    allow from all
#</Files>
### fastcgi ###

把:
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>
改成:
<Directory />
    Options FollowSymLinks +ExecCGI
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

( )

把具体VirtualHost对应的Directory做修改如下:

<Directory PATH>
    Options FollowSymLinks
    AllowOverride FileInfo Options All
    Order allow,deny
    Allow from all
</Directory>
改成:
<Directory PATH>
    FCGIWrapper /var/www/php/bin/php .php
    Options FollowSymLinks +ExecCGI
    AllowOverride FileInfo Options All
    Order allow,deny
    Allow from all
</Directory>

/usr/local/php/bin/php -v 如果看不到类似于PHP 5.2.11 (cgi-fcgi) (built:的话,则可以:mv php php-cli
cp php-cgi php

重启Apache

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值