Latest Posts »
Latest Comments »
Popular Posts »

spawn-fcgi

Written by bixuan on 2009年06月12号 – 10:38

spawn-fcgi

1.概况

spawn-fcgi官方:http://redmine.lighttpd.net/projects/spawn-fcgi/wiki,可以从这里下载到最新稳定版本。
lighttpd官方:http://www.lighttpd.net/
其实spawn-fcgi是lighttpd的一个分支项目,是一个cgi进程的管理器,效率不错。

2.安装

  1. 下载:
    wget -c http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.bz2
    wget -c http://www.lighttpd.net/download/lighttpd-1.4.22.tar.bz2 #(spawn-php.sh这个文件需要从这里提取,具体在:lighttpd-1.4.22/doc/下)
  2. spawn-fcgi安装:
    tar jxvfp spawn-fcgi-1.6.2.tar.bz2
    cd spawn-fcgi-1.6.2
    ./configure
    make
    make install

3.配置

这里主要整理spawn-fcgi相关配置!
安装好后,可以通过:spawn-fcgi -h
Usage: spawn-fcgi [options] [-- <fcgiapp> [fcgi app arguments]]
spawn-fcgi v1.6.2 (ipv6) - spawns FastCGI processes
Options:
-f <path>      filename of the fcgi-application (ignored if <fcgiapp> is given)
-d <directory> chdir to directory before spawning
-a <address>   bind to IPv4/IPv6 address (defaults to 0.0.0.0)
-p <port>      bind to TCP-port
-s <path>      bind to Unix domain socket
-M <mode>      change Unix domain socket mode
-C <children>  (PHP only) numbers of childs to spawn (default: not setting
the PHP_FCGI_CHILDREN environment variable - PHP defaults to 0)
-F <children>  number of children to fork (default 1)
-P <path>      name of PID-file for spawned process (ignored in no-fork mode)
-n             no fork (for daemontools)
-v             show version
-?, -h         show this help
(root only)
-c <directory> chroot to directory
-S             create socket before chroot() (default is to create the socket in the chroot)
-u <user>      change to user-id
-g <group>     change to group-id (default: primary group of user if -u is given)
-U <user>      change Unix domain socket owner to user-id
-G <group>     change Unix domain socket group to group-id
看到详细的参数说明,不过这里为了方便,还是喜欢通过这个spawn-php.sh来管理php-cgi比较方便^_^
有个地方需要注意:spawn-php.sh这个文件有个bug,见下面红色字体部分:
# cat spawn-php.sh
#!/bin/bash
## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI=”/usr/local/bin/spawn-fcgi
## ABSOLUTE path to the PHP binary
FCGIPROGRAM=”/app/app/php5-fastcgi/bin/php-cgi
## TCP port to which to bind on localhost
FCGIPORT=”9002″
## number of PHP children to spawn
PHP_FCGI_CHILDREN=10
## maximum number of requests a single PHP process can serve before it is restarted
PHP_FCGI_MAX_REQUESTS=1000
## IP addresses from which PHP should access server connections
FCGI_WEB_SERVER_ADDRS=”192.168.11.159
pripath=”/app/wwwroot/www.ourlinux.net”
# allowed environment variables, separated by spaces
ALLOWED_ENV=”pripath PATH USER”
## if this script is run as root, switch to the following user
USERID=nobody
GROUPID=nobody
################## no config below this line
if test x$PHP_FCGI_CHILDREN = x; then
PHP_FCGI_CHILDREN=5
fi
export PHP_FCGI_MAX_REQUESTS
export FCGI_WEB_SERVER_ADDRS
ALLOWED_ENV=”$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS”
if test x$UID = x0; then
EX=”$SPAWNFCGI -a $FCGI_WEB_SERVER_ADDRS -p $FCGIPORT -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN”
else
EX=”$SPAWNFCGI -a $FCGI_WEB_SERVER_ADDRS -p $FCGIPORT -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN”
fi
# copy the allowed environment variables
E=
for i in $ALLOWED_ENV; do
E=”$E $i=${!i}”
done
# clean the environment and set up a new one
env - $E $EX
以上红色部分是要自己补上去,不然默认监听在0.0.0.0,绿色字体部分是要根据具体情况做相应的修改!蓝色字体部分是根据自己的需求加的参数,意思是:php的代码放在pripath这个变量所在的目录下,这个是为了跨机器执行php-cgi,这样可以通过proxy来调用,可以提升cpu利用率(将php-cgi进程跑在cpu消耗低的机器上)

4.后记

如果结合lvs,nginx(lighttpd等L7的负载均衡软件)、memcahced等,这样就可以很方便的实现高可用、高性能的分布式系统:)

Tags: , , , ,
Posted in 运维小技巧 | No Comments »
 Page 1 of 1  1