当前位置:操作系统 > Unix/Linux >>

命令行安装FastCGI模式安装php

一般php的安装都是编译为apache的一个模块,运行在apache的上下文中,权限比较高。如果不是特别注重安全性和使用一些线程不安全的模块,没有必要搞cgi。cgi模式一般比module要慢。但是,最近安装php5在make install时总是说dlname not found很是烦恼,想尝试编译成fastcgi看看效果。

一、安装php
wget http://cn2.php.net/get/php-5.2.5.tar.bz2/from/cn.php.net/mirror
tar jxvf php-5.2.5.tar.bz2
cd php-5.2.5
'./configure' '--prefix=/usr/local/php5' '--with-mysql=/usr/local/mysql' '--with-zlib-dir' '--with-freetype-dir=/usr' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--with-gd' '--enable-track-vars' '--enable-ftp'  '--with-iconv'  '--with-gettext' '--with-curl' '--enable-fastcgi' '--with-openssl'

1. 编译参数不能加 '--with-apxs2=/usr/local/apache2/bin/apxs' 否则安装出来的php执行文件是cli模式的
2. 如果系统已经安装好了zlib、freetype、jpeg和png是gd库的基础,一般系统都会带有,那么用--with-xxxx-dir=/usr就行了。gd2也不要另外下载安装了,貌似gd库的核心成员被zend挖走了,总之php源码自己带有一套更加帅气的gd库,用--with-gd就ok了

make
make install
cp php.ini-dist /usr/local/php5/lib/php.ini
二、 安装mod_fcgid

其实php官方主推fascgi,但是国内有牛人发现它的管理模式有点冗余,详情请看
http://fastcgi.coremail.cn/
俺打算采用自己人写的,嘿嘿

wget
http://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

安装好以后在apache的 modules 目录下会有一个 mod_fcgid.so

附fastcgid的安装过程
wget
http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
tar zxvf mod_fastcgi-2.4.6.tar.gz
cd mod_fastcgi-2.4.6
如果web服务器是apache2
cp Makefile.AP2 Makefile
vi Makefile
把top_dir = /usr/local/apache  修改成apache的安装目录
make
make install
三、配置apache

vi一下httpd.conf添加这样的内容

LoadModule fcgid_module modules/mod_fcgid.so
ScriptAlias /fcgi-bin/ "/usr/local/php5/bin/"
AddHandler php-fastcgi .php
Action php-fastcgi /fcgi-bin/php-cgi
AddType application/x-httpd-php .php
    Options FollowSymLinks ExecCGI
    AllowOverride None
    Order allow,deny
    allow from all

第一句话装载mod_fcgid,fastcgi用LoadModule fcgid_module modules/mod_fcgid.so这个去装载

然后建立一个/fcgi-bin/的虚拟目录到php-cgi的那个目录,分别用AddHandler和Action关联apache和php-cgi,然后AddType建立一个web文件类型php,最后用Directory定义一下访问权限。

其实建立/fcgi-bin/虚拟目录偷懒了,为了更好的安全性应该是用ln -s将php-cgi链到另外的目录,而不是将整个php安装目录暴露,诸如此类的写法

    # Path to php.ini – defaults to /etc/phpX/cgi
    DefaultInitEnv PHPRC=/usr/local/Zend/etc
    AddHandler fcgid-script .fcgi   
    #SocketPath /var/lib/apache2/fcgid/sock   
    # Communication timeout: Default value is 20 seconds
    IPCCommTimeout 60   
    # Connection timeout: Default value is 3 seconds
    #IPCConnectTimeout 3
    # Number of PHP childs that will be launched. Leave undefined to let PHP decide.
    #DefaultInitEnv PHP_FCGI_CHILDREN 3   
    # Maximum requests before a process is stopped and a new one is launched
    #DefaultInitEnv PHP_FCGI_MAX_REQUESTS 5000   
    # Define a new handler "php-fcgi" for ".php" files, plus the action that must follow
    AddHandler php-fcgi .php
    Action php-fcgi /fcgi-bin/php-fcgi-wrapper   
    # Define the MIME-Type for ".php" files
    AddType application/x-httpd-php .php   
    # Define alias "/fcgi-bin/". The action above is using this value, which means that
    # you could run another "php5-cgi" command by just changing this alias
    Alias /fcgi-bin/ /var/www/fcgi-bin.d/php5-default/   
    # Turn on the fcgid-script handler for all files within the alias "/fcgi-bin/"
   
        SetHandler fcgid-script
        Options +ExecCGI
   

重启一下apache就ok了。

通过用ab -n 10000 -c 300 测一下,发现apache狂报Timeout waiting for output from CGI script,开出的进程都没有能在30秒内完成处理,测试时用浏览器访问的话报500错误,死得比较难看。如果是模块式则不会报这样的错,而且页面还能打开,就是慢得不行

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,