CentOS中创建管理nginx的shell脚本

如果想在CentOS中安装最新版本的nginx,那用yum的方式安装就不行了,目前yum中的版本是0.6,而最新nginx版本是0.8.36。所以我们只能通过编译安装,但编译安装后问题就来了,不能像yum安装的那样通过/etc/init.d/nginx start(或stop或restart)。唯一的办法就是自己创建个脚本来实现这些功能:

1.停止nginx,推荐用这个命令


sudo kill `cat /usr/local/nginx/logs/nginx.pid`

2.创建/etc/init.d/nginx文件


sudo vim /etc/init.d/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
# 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="/usr/local/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/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

3.增加可执行权限


sudo chmod +x /etc/init.d/nginx 

4.设置自启动


sudo /sbin/chkconfig nginx on 

5.使用举例

完成上面这些设置后,我们就可以像yum安装后那样,使用如下的命令


sudo /etc/init.d/nginx start  
...  
sudo /etc/init.d/nginx stop  
...  
sudo /etc/init.d/nginx restart  
...  
sudo /etc/init.d/nginx reload  
...  
sudo /etc/init.d/nginx status  
...  
sudo /etc/init.d/nginx configtest 


( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0001355064{main}( ).../index.php:0
20.0001355352require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.161810176112require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.162910247256include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.239810803984get_template_part( ).../single.php:5
60.239810804504locate_template( ).../general-template.php:167
70.239810804600load_template( ).../template.php:643
80.239810804984require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.245010790112cwppos_show_review( ).../content-single.php:29

发表评论

电子邮件地址不会被公开。 必填项已用*标注