CentOS 7 Nginx 控制脚本
*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记得修改权限。