当前位置: > Linux服务器 > nginx >

CentOS上可用的nginx启动脚本

时间:2015-06-28 20:04来源:linux.it.net.cn 作者:IT

在网上找的脚本,挺好用的,已经用了一段时间了,怕以后配置新服务器还要再找所以记下来。

#!/bin/sh  
#  
# nginx - this script starts and stops the nginx daemin  
# Taken from http://www.hikaro.com  
# 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  
# pidfile:     /usr/local/nginx/logs/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="/opt/nginx/sbin/nginx"  
prog=$(basename $nginx)  

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"  

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  

CentOS上可以用的spawn-fcgi启动脚本在网上找的支持chkconfig的spawn-fcgi启动脚本,原脚本大概是写给UNIX用的,使用的是socket文件的方式,结果启动后nginx连不上fcgi,导致PHP页面无法执行。

整好服务器后原文地址已经不知道哪里去了,真是对不起原作者。下面是在CentOS上实际测试好的启动脚本

#!/bin/sh  
#  
# Startup script for Spawn-fcgi  
# chkconfig: - 85 15  
# description: Start script for the php-fcgi  
# processname: php-fcgi  

FCGI_DAEMON="/opt/spawn-fcgi/bin/spawn-fcgi"  
FCGI_PROGRAM="/opt/php/bin/php-cgi"  
FCGI_SOCKET="/tmp/spawn-fcgi.sock"  
FCGI_PIDFILE="/var/run/spawn-fcgi.pid"  
PHP_FCGI_CHILDREN=4  
PHP_FCGI_MAX_REQUESTS=1000  

export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS  

RETVAL=0  
prog="spawn-fcgi"  

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

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

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

start() {  
        echo -n $"Starting $prog: "  
        daemon $FCGI_DAEMON -a 127.0.0.1 -p 9000 -u www -g www -f $FCGI_PROGRAM -C $PHP_FCGI_CHILDREN -P $FCGI_PIDFILE  
        RETVAL=$?  
        echo  
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog  
        return $RETVAL  
}  

stop() {  
        echo -n $"Stopping $prog: "  
        rm -f $FCGI_PIDFILE $FCGI_SOCKET  
        killproc $FCGI_PROGRAM  
        RETVAL=$?  
        echo  
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog  
        return $RETVAL  
}  

case "$1" in  
        start)  
                start  
                ;;  
        stop)  
                stop  
                ;;  
        restart)  
                stop  
                start  
                ;;  
        condrestart)  
                if [ -f /var/lock/subsys/$prog ]; then  
                        stop  
                        start  
                fi  
                ;;  
        status)  
                status $lighttpd  
                RETVAL=$?  
                ;;  
        *)  
                echo $"Usage: $0 {start|stop|restart|condrestart|status}"  
                RETVAL=1  
esac  

exit $RETVAL  

把上面代码保存为/etc/init.d/php-fcgi,使用以下命令把spawn-fcgi加入开机启动过程:

> chkconfig --add php-fcgi  
> chkconfig --level 345 php-fcgi on  

平时停止或者重启spawn-fcgi:

> service spawn-fcgi stop  
> service spawn-fcgi start  
> service spawn-fcgi restart  


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容