PHP5.3+Apache2+FastCGI+PHP-FPM配置

###前言

本文将利用PHPApache2PHP-FPMmod_fastcgi构建可行的WEB运行环境。

###安装apache2的mod_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
make top_dir=/path/to/apache2
make install

/path/to/apache2为你的Apache安装位置。

编辑httpd.conf增加如下配置:

LoadModule fastcgi_module modules/mod_fastcgi.so

###安装autoconf2.13

必须确保已经安装了autoconf2.13,否则在执行之后的./buildconf --force命令时候会出现以下错误:

Forcing buildconf using default Zend directory buildconf: 
checking installation… buildconf: autoconf version 2.59 (ok) 
buildconf: Your version of autoconf likely contains buggy cache code. 
Running cvsclean for you. To avoid this, install autoconf-2.13.

所以先安装autoconf2.13:

wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.13.tar.gz
tar -zxvf autoconf-2.13.tar.gz
cd autoconf-2.13
./configure --prefix=/path/to/autoconf
make && make install
export PHP_AUTOCONF=/path/to/autoconf/bin/autoconf
export PHP_AUTOHEADER=/path/to/autoconf/bin/autoheader

###安装libevent

wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
tar -zxvf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable/
 ./configure
make && make install

###安装PHP

wget http://cn.php.net/distributions/php-5.3.2.tar.gz
tar -zxvf php-5.3.2.tar.gz

给PHP打PHP-FPM的补丁:

cd php-5.3.2
svn co http://svn.php.net/repository/php/php-src/branches/PHP_5_3/sapi/fpm sapi/fpm

开始编译:

 ./buildconf --force
./configure --prefix=/path/to/php5.3.2/ --with-config-file-path=/path/to/php5.3.2/ \
--enable-fpm --enable-mbstring --enable-xml --enable-fastcgi
make && make install

###启动

cd /path/to/php5.3.2/etc/
mv php-fpm.default.conf php-fpm.conf
/path/to/php5.3.2/sbin/php-fpm

编辑php-fpm.conf,把下面几行前的注释符号去掉:

pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 100

###配置Apache

cd /var
mkdir fcgi-bin
cd fcgi-bin
ln -s php-cgi /path/to/php/bin/php #你的php路径,其中要注意权限问题。

如果用Apache的内部的FPM(mod_fastcgi),则编辑httpd.conf,添加如下配置:

#定义目录映射
ScriptAlias /fcgi-bin/ "/var/fcgi-bin/" 

#配置fastcgi server
FastCgiServer /var/fcgi-bin/php-cgi -processes 10

SetHandler fastcgi-script
Options FollowSymLinks
Order allow,deny
Allow from all

#增加MIME类型
AddType application/x-httpd-php .php 

#.php结尾的请求都要用php-fastcgi来处理 
AddHandler php-fastcgi .php

#设置php-fastcgi的处理器
Action php-fastcgi /fcgi-bin/php-cgi 

如果用[PHP-FPM][2]来管理CGI的话,编辑httpd.conf添加如下配置:

#定义目录映射
ScriptAlias /fcgi-bin/ "/var/fcgi-bin/" 

#配置fastcgi外部server,127.0.0.1:9000地址为php-fpm的监听地址。
FastCgiExternalServer /var/fcgi-bin/php-cgi -host 127.0.0.1:9000

SetHandler fastcgi-script
Options FollowSymLinks
Order allow,deny
Allow from all

#增加MIME类型
AddType application/x-httpd-php .php

#.php结尾的请求都要用php-fastcgi来处理  
AddHandler php-fastcgi .php         

#设置php-fastcgi的处理器
Action php-fastcgi /fcgi-bin/php-cgi 

使用[PHP-FPM][2]是有很多优点的,比如可以平滑地重新加载php.ini文件而不用重启FastCGI进程,对于访问量大的网站来说是很重要的。

###重启Apache

重启Apache,查看phpinfo,如果服务器信息存在如下类似信息,那么说明安装成功:

Apache/2.2.11 (Unix) mod_fastcgi/2.4.6

如果出现403的错误,查看下/var/fcgi-bin/是否有足够的权限。

执行以下操作应该可以看见N个PHP-FPM进程在运行。

$ ps aux|grep php

参考:

http://php-fpm.org/wiki/Documentation

(完)
comments powered by Disqus
Powered by GitHub  &&  Jekyll | CC BY-NC-SA