apachephp配置fastcgi代替mod_php

                                   (大红色字体表示关键部分)

环境rhed5.4                        

所需包 

httpd-2.2.17.tar.gz      php-5.2.13.tar.gz     mod_fastcgi-2.4.6.tar.gz php-5.2.13-fpm-0.5.13.diff.gz

 

apache 的安装

     #cd /usr/local/src/tarbag

     # wget http://mirror.bjtu.edu.cn/apache//httpd/httpd-2.2.17.tar.gz

     # tar -zxvf http-2.2.17.tar.gz -C  ../software

     # cd ../software/http-2.2.17

 #./configure --prefix=/usr/local/apache2.2.17 --with-mpm=worker --enable-so --enable-rewrite --enable-headers --enable-expires  --enable-mods-shared=all 

 

     # make

     # make install

 

apache安装  mod_fastcgi  模块

   #cd /usr/local/src/tarbag

  #wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz

  #tar -zxvf mod_fastcgi-2.4.6.tar.gz -C ../softwar

  #cd ../software/mod_fastcgi-2.4.6

  #cp Makefile.AP2 Makefile

  编辑Makefile文件

  #vim Makefile

  修改 apache的安装路径

  top_dir=/usr/local/apache2.2.17

  #make

  #make install

  查看在apachemodules的目录下是否有mod_fastcgi.so模块

  有,则说明成功

 

Php的安装

  #cd /usr/local/src/tarbag

  #wget http://museum.php.net/php5/php-5.2.13.tar.gz

  #tar -zxvf  php-5.2.13.tar.gz  -C  ../software

  #cd ../software/php-5.2.13.tar.gz

  #wget  http://php-fpm.org/downloads/php-5.2.13-fpm-0.5.14.diff.gz

  在下一步要给PHP 打个补丁

  #gzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d  /usr/local/src/software/php-5.2.13  -p1

  (这条的命令的作用解压php-5.2.13-fpm-0.5.13.diff.gz /usr/local/src/software/php-5.2.13 的顶层目录打上个补丁)

   #./configure --prefix=/usr/local/php5.2.13 --with-config-file-path=/usr/local/php5.2.13/etc          --enable-fastcgi --enable-force-cgi-redirect --enable-fpm --with-bz2 --with-curl --with-curlwrappers --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local  --with-zlib --enable-zend-multibyte --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql=/usr/local/mysql

#make

#make install

#cp -a php.ini-dist /usr/local/php2.2.13/etc/php.ini

 

编辑php-fpm的配置文件 /usr/local/php5.2.13/etc/php-rmp.conf

将这两行的注释去掉

<!--    <value name="user">nobody</value>   -->

<!--    <value name="group">nobody</value>  -->

否则会出现如下错误

[ERROR] fpm_unix_conf_wp(), line 124: please specify user and group other than root, pool 'default'

 

这里试下是否安装rmp成功

#/usr/local/php5.2.13/sbin/php-fpm start

netstat -ntlp 查看9000端口是否开启,开启则代表插件安装成功

 

先关闭php

#/usr/local/php5.2.13/sbin/php-fpm stop

 

下面有两种方法来启用php

  1.利用apache来管理php进程

   #mkdir /usr/local/apache2.2.17/fcgi-bin

   #ln -s /usr/local/php/bin/php-cgi /usr/local/apache2.2.17/fcgi-bin/php-cgi

   如果用apache的内部的fpm(fastcgi process manager)

   LoadModule fastcgi_module modules/mod_fastcgi.so  添加模块

   #<IfModule fastcgi_module>   

     ScriptAlias /fcgi-bin/ "/usr/local/apache2.2.17/fcgi-bin/"  程序目录的别名

     FastCgiServer /usr/local/apache2.2.17/fcgi-bin/php-cgi -processes 10   

      开启10php进程

     AddType application/x-httpd-php .php  添加php的格式 

     AddHandler php-fastcgi .php     .php的文件都由php-fastcgi来处理

     Action php-fastcgi /fcgi-bin/php-cgi   php-fastcgi的程序的定义

     <Directory "/usr/local/apache2.2.17/fcgi-bin/">   设置放这个的程序目录的权限

            SetHandler fastcgi-script       所有的文件由fastcgi-script来处理

            Options FollowSymLinks   

            Order allow,deny   

            Allow from all   

       </Directory>   

    </IfModule>  

 

 

   2.如果用php-fpm来管理php进程

     必须打开开启php-fpm进程

      LoadModule fastcgi_module modules/mod_fastcgi.so  添加模块

      <IfModule fastcgi_module>   

          ScriptAlias /fcgi-bin/ "/usr/local/apache2.2.17/fcgi-bin/" 

          # php进程管理交给127.0.0.19000,这正是php_fpm的监听端口   

          FastCgiExternalServer /usr/local/apache2.2.17/fcgi-bin/php-cgi -host 127.0.0.1:9000    

          AddType application/x-httpd-php .php   

          AddHandler php-fastcgi .php   

          Action php-fastcgi /fcgi-bin/php-cgi   

          <Directory "/usr/local/apache2.2.17/fcgi-bin/">   

               SetHandler fastcgi-script   

               Options FollowSymLinks   

               Order allow,deny   

               Allow from all   

              </Directory>   

      </IfModule>

 

重启apache

  

 

下面我们来做下性能的测试.

  1. 使用fastCGI

     

[root@localhost http]# /usr/local/apache2.217/bin/ab -c 50 -n 800 http://lin.com/test.php

总共请求800次,每次的并发数是50

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking lin.com (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Completed 600 requests

Completed 700 requests

Completed 800 requests

Finished 800 requests

 

 

Server Software:        Apache/2.2.3

软件版本

Server Hostname:        lin.com

主机名

Server Port:            80

端口

Document Path:          /test.php

文件路径

Document Length:        706 bytes

文件大小

 

Concurrency Level:      50

并发数

Time taken for tests:   33.278 seconds

测试总用时

Complete requests:      800

完成的请求

Failed requests:        725

   (Connect: 0, Receive: 0, Length: 725, Exceptions: 0)

失败请求

Write errors:           0

Total transferred:      768926 bytes

总共传输字节

HTML transferred:       568926 bytes

用于HTML传输的字节

Requests per second:    24.04 [#/sec] (mean)

每秒的处理的事物数

Time per request:       2079.873 [ms] (mean)

平均每个事物的响应时间

Time per request:       41.597 [ms] (mean, across all concurrent requests)

每个请求运行的时间

Transfer rate:          22.56 [Kbytes/sec] received

每秒的流量

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:      232 1424 1697.9    264    9261

Processing:   255  401 455.0    291    3758

Waiting:      255  375 416.9    290    3758

Total:        491 1825 1737.0    574   10490

 

Percentage of the requests served within a certain time (ms)

  50%    574

  66%   3524

  75%   3545

  80%   3552

  90%   3571

  95%   3862

  98%   4591

  99%   9539

 100%  10490 (longest request)

 

每次请求的响应时间的统计,在574ms一下的占50%  以此类推。