码迷,mamicode.com
首页 > 其他好文 > 详细

CentOS 7 Nginx 控制脚本

时间:2015-06-24 19:28:26      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

「个人记录,肯定有理解错误和理解不透的地方,小白总是有个成长的过程,希望大家多多指教。」

*书接上文:CentOS 7 安装Nginx-1.9.2

*Nginx 安装完后,开启,关闭的操作实在是长,因此……

一、新建脚本:

$ vi /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:      /etc/nginx/nginx.conf
# pidfile:     /run/nginx/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/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

lockfile=/var/lock/nginx.lock

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

注意几个地方的配置,就是上在nginx中编译时设置的那些目录:

# config: /etc/nginx/nginx.conf
# pidfile: /run/nginx/nginx.pid
nginx="/usr/sbin/nginx"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile=/var/lock/nginx.lock

二、添加到服务

$ chmod a+x /etc/init.d/nginx

$ chkconfig --add nginx

$ chkconfig --list nginx
nginx          	0:关	1:关	2:关	3:关	4:关	5:关	6:关

上面其实就是在/etc/rc.d/rc5.d/目录下创建了一个链接。如下:

$ cd /etc/rc.d/rc5.d/

$ ll |grep nginx
lrwxrwxrwx. 1 root root 15 6月  24 16:14 K15nginx -> ../init.d/nginx

三、使用

$ service nginx start  
$ service nginx stop  
$ service nginx restart  
$ service nginx reload  
  
$ /etc/init.d/nginx start  
$ /etc/init.d/nginx stop  
$ /etc/init.d/nginx restart  
$ /etc/init.d/nginx reload
这样的操作就简单多了。

四、问题:

如果有下面的错误提示,去看看目录是否在该文件,如果实在是存在,且脚本无错,则考虑文件的格式是不是正确,网上有用工具转的比如:doc2unix 。

env: /etc/init.d/nginx: 没有那个文件或目录

我的做法是直接复制下内容:

$ rm /etc/init.d/nginx

$ vi /etc/init.d/nginx
再把内容复制进去,保存,
$ chmod a+x /etc/init.d/nginx
记得修改权限。

CentOS 7 Nginx 控制脚本

标签:

原文地址:http://my.oschina.net/liucao/blog/470344

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!