CentOS 7 Nginx 控制腳本

jopen 9年前發布 | 7K 次閱讀 Shell 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</pre>

注意幾個地方的配置,就是上在nginx中編譯時設置的那些目錄:

config: /etc/nginx/nginx.conf

</blockquote>

pidfile: /run/nginx/nginx.pid

</blockquote>

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:關</pre>

上面其實就是在/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</pre>

三、使用

$ 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</pre> 這樣的操作就簡單多了。

四、問題:

如果有下面的錯誤提示,去看看目錄是否在該文件,如果實在是存在,且腳本無錯,則考慮文件的格式是不是正確,網上有用工具轉的比如:doc2unix 。

env: /etc/init.d/nginx: 沒有那個文件或目錄

我的做法是直接復制下內容:

$ rm /etc/init.d/nginx

$ vi /etc/init.d/nginx</pre> 再把內容復制進去,保存,

$ chmod a+x /etc/init.d/nginx
記得修改權限。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!