Apache + MySQL + PHP + FastCGI 配置(下)

这一部分,我们在前两部分的基础上安装 Nginx ,使其和 apache 共存。Nginx 当前稳定版本为:nginx-0.6.31


(一)安装 Nginx

/usr/sbin/groupadd -g 49 nginx
/usr/sbin/useradd -g 49 -u 49 -M -s /bin/nologin nginx -d /usr/local/nginx/html

直接使用 php-cgi FastCGI 运行方式有两个问题:

1)如果进程崩溃,难以配置重新启动
2)单进程的效率低

因此,我们可以利用 Lighttpd spawn-fcgi 来控制进程的运行。获得 spawn-fcgi 的方法如下:

wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz #
获取 Lighttpd 的源码包
tar -xvjf lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure
make

cp -a spawn-fcgi /usr/local/php/bin/ (取出 spawn-fcgi 的程序;我们只是需要这个执行程序,因此下面的编译安装就不用了)

下面我们就可以使用 spawn-fcgi 来控制 php-cgi FastCGI 进程了

/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u nginx -g nginx -P /var/run/php-fastcgi.pid -f "/usr/local/php/bin/php-cgi"

参数含义如下:

-f
指定调用 FastCGI 的进程的执行程序位置,根据系统上所装的 PHP 的情况具体设置
-a
绑定到地址 addr
-p
绑定到端口 port
-s
绑定到 unix socket 的路径 path
-C
指定产生的 FastCGI 的进程数,默认为 5(仅用于PHP
-P
指定产生的进程的 PID 文件路径
-u
-g FastCGI 使用什么身份(-u 用户 -g 用户组)运行

tar zxvf nginx-0.6.31.tar.gz
cd nginx-0.6.31

./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/var/run/nginx/nginx.pid --error-log-path=/var/log/nginx --http-log-path=/var/log/nginx --lock-path=/var/run/nginx/nginx.lock --user=nginx --group=nginx --with-pcre-opt=/usr/local/bin/pcre-config --with-http_stub_status_module --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module

make
make install

mkdir /var/log/nginx

chown nginx.root /var/log/nginx
chown nginx.root /var/run/nginx

cd ..
rm -rf nginx-0.6.31

(二)配置 Nginx

vi /etc/rc.d/init.d/nginx   (编辑一个启动 Nginx 的脚本,方便点。内容如下:)

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse /
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac



chmod 700 /etc/rc.d/init.d/nginx

vi /etc/rc.d/init.d/php-fastcgi    (编辑一个启动 php-fastcgi 的脚本,内容如下:)

#!/bin/sh
#
# php-cgi - this script starts and stops the php-cgi daemin
#
# chkconfig: - 85 15
# description: Fast CGI php
# processname: php-cgi
# config: /etc/php.ini
# pidfile: /var/run/php-cgi.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

phpcgi="/usr/local/php/bin/php-cgi"
prog=$(basename ${phpcgi})

FCGIPORT="9000"
FCGIADDR="127.0.0.1"
FCGIUSER="nginx"
FCGIGROUP="nginx"
PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=500
export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
[ -e /etc/sysconfig/php-cgi ] && . /etc/sysconfig/php-cgi

lockfile=/var/lock/subsys/php-cgi

start() {
        echo -n $"Starting $prog: "
        /usr/local/php/bin/spawn-fcgi -a $FCGIADDR -p $FCGIPORT -C $PHP_FCGI_CHILDREN -u $FCGIUSER -g $FCGIGROUP -P /var/run/php-fastcgi.pid -f "${phpcgi}" >> /dev/null 2>&1 &
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
        }

stop() {
        echo -n $"Stopping $prog: "
        killproc $prog -QUIT
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
        }

restart() {
        stop
        start
        }

force_reload() {
        restart
        }

fdr_status() {
        status $prog
        }

case "$1" in
start|stop|restart)
$1
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
exit 2
esac



chmod 700 /etc/rc.d/init.d/php-fastcgi

vi /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/fastcgi.conf

 



运行以下命令检测配置文件是否无误:

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

如下结果则为正确:

2008/06/19 10:37:52 [info] 2058#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2008/06/19 10:37:52 [info] 2058#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully


/etc/rc.d/init.d/nginx start  (启动 nginx;请先关闭 apache 服务哦,或者把 apache 的默认 80 端口更改)

检查 nginx 进程是否启动:

# ps aux|grep nginx|grep -v grep (
若看到如下几个进程,就已经启动OK,若无则要检查配置)

root      2173  0.0  0.1   5308   596 ?        Ss   10:41   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
apache    2174  0.0  0.1   5504   932 ?        S    10:41   0:00 nginx: worker process                                          
apache    2175  0.0  0.1   5504   960 ?        S    10:41   0:00 nginx: worker process                                          
apache    2177  0.0  0.1   5504   956 ?        S    10:41   0:00 nginx: worker process

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值