Tuesday, March 24, 2009

php On Nginx Fastcgi

系统环境

  • ubuntu 8.04
  • php-cgi 5.2.4
  • nginx 0.6.34
  • lighttpd 1.4.19 (需要其中的 spawn-fcgi 程序)

相关概念和链接

  • fastcgi

    相对于cgi为每个动态内容请求创建一个独立的进程,fastcgi能使用一个单独进行为多个动态内容请求进行相应

    wikipeida中的fastcgi条目

安装过程

apt-get install xxxx

配置备注

nginx配置

  1. 备份nginx默认站点配置文件(/etc/nginx/sites-available/default) 或 以default为模板创建新文件并在 /etc/nginx/sites-enabled/ 中建立链接
  2. 修改站点配置文件中" location /" 起始段落为以下内容
    location / {
    #root /var/www/nginx-default;
    root /home/lvs/code/php/virtual_host/chino/wwwroot ; #指向php工程DocumentRoot目录
    index index.php index.html index.htm ; #添加index.php为站点默认访问索引文件
    }
  3. 修改站点配置文件中"location ~ \.php " 起始段落为以下内容
    location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000; #这个地址需要和fastcgi监听地址匹配
    fastcgi_index index.php;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /home/lvs/code/php/virtual_host/chino/wwwroot$fastcgi_script_name;
    #fastcgi_param 中,fastcgi_script_name前的值应设置为php工程 DocumentRoot
    #TODO 需要找到以变量存储php 工程 DocumentRoot的方法
    }

fastcgi环境建立

1. 创建 php fastcgi监听进程生产 脚本

    • $sudo touch /usr/bin/php-fastcgi && 插入以下内容
      #!/bin/sh
      spawn-fcgi -a 127.0.0.1 -C 7 -p 9000 -u www-data -f /usr/bin/php5-cgi
      #如果不安装lighttpd,可以下载lighttpd源码编译后提取spawn-fcgi程序
      #指定产生7个 fastcgi 监听进程

2. 创建fastcgi 启动管理脚本

    • $sudo touch /etc/init.d/php-fastcgi-spawner && 插入以下内容
#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi #需要和上一步骤中php fastcgi监听进程生产脚本位置匹配
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL

3. 如果需要系统启动自动运行:

    • debian系在/etc/rc2.d/ 中创建S字头链接
    • redhat系在/etc/rc3.d/ (文本环境 ) 或 /etc/rc5.d/ (图形环境) 创建链接

性能测试

以 $ab -c 50 -n 2000 对分别使用nginx&fastcgi和apache&mod-php 运行的同一php页面进行测试
apache 结果如下:

Server Software:        Apache/2.2.8
Server Hostname: localhost
Server Port: 80

Document Path: /index.php?module=todo&act=thread
Document Length: 11622 bytes

Concurrency Level: 50
Time taken for tests: 22.542643 seconds
Complete requests: 2000
Failed requests: 0
Write errors: 0
Total transferred: 23765490 bytes
HTML transferred: 23284290 bytes
Requests per second: 88.72 [#/sec] (mean)
Time per request: 563.566 [ms] (mean)
Time per request: 11.271 [ms] (mean, across all concurrent requests)
Transfer rate: 1029.52 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 23.0 0 156
Processing: 61 556 126.6 552 1421
Waiting: 1 539 127.7 531 1397
Total: 157 560 121.6 553 1421

Percentage of the requests served within a certain time (ms)
50% 553
66% 584
75% 611
80% 630
90% 695
95% 761
98% 871
99% 998
100% 1421 (longest request)

nginx结果如下

Server Software:        nginx/0.6.34
Server Hostname: localhost
Server Port: 8077

Document Path: /index.php?module=todo&act=thread
Document Length: 11622 bytes

Concurrency Level: 50
Time taken for tests: 5.945355 seconds
Complete requests: 2000
Failed requests: 0
Write errors: 0
Total transferred: 23626000 bytes
HTML transferred: 23244000 bytes
Requests per second: 336.40 [#/sec] (mean)
Time per request: 148.634 [ms] (mean)
Time per request: 2.973 [ms] (mean, across all concurrent requests)
Transfer rate: 3880.68 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 12.8 0 133
Processing: 12 144 26.2 144 309
Waiting: 10 138 25.9 137 302
Total: 32 146 22.3 144 309

Percentage of the requests served within a certain time (ms)
50% 144
66% 146
75% 149
80% 150
90% 155
95% 172
98% 230
99% 271
100% 309 (longest request)

参考链接

No comments: