centos系統中nginx啟動指令碼和chkconfig管理

來源:互聯網
上載者:User

在安裝完nginx後,重新啟動需要“kill -HUP nginx進程編號”來進行重新載入,顯然十分不方便。如果能像apache一樣,直接通過指令碼進行管理就方便多了。

nginx官方早就想好了,也提供了這個指令碼,地址:http://wiki.nginx.org/RedHatNginxInitScript。這裡將管理指令碼收錄在這裡:

 代碼如下 複製代碼

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# 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
# config:  /etc/sysconfig/nginx
# pidfile: /var/run/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"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
   useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
   if [ `echo $opt | grep '.*-temp-path'` ]; then
   value=`echo $opt | cut -d "=" -f 2`
   if [ ! -d "$value" ]; then
   # echo "creating" $value
   mkdir -p $value && chown -R $user $value
   fi
   fi
   done
}
 
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
sleep 1
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


將以上指令碼儲存到/etc/init.d/nginx檔案,並修改兩個地方:

nginx=”/usr/sbin/nginx” 修改成nginx執行程式的路徑。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成設定檔的路徑。
儲存後,就可以通過該指令碼對nginx服務進行管理了:

 代碼如下 複製代碼
$ /etc/init.d/nginx start
$ /etc/init.d/nginx stop
$ /etc/init.d/nginx reload

...
使用chkconfig進行管理

上面的方法完成了用指令碼管理nginx服務的功能,但是還是不太方便,比如要設定nginx開機啟動等。這時可以使用chkconfig來設定。

先將nginx服務加入chkconfig管理列表:

 代碼如下 複製代碼
chkconfig --add /etc/init.d/nginx

加完這個之後,就可以使用service對nginx進行啟動,重啟等操作了。

 代碼如下 複製代碼
$ service nginx start
$ service nginx stop
$ service nginx reload

...
設定終端模式開機啟動:

 代碼如下 複製代碼

$ chkconfig --level 3 nginx on

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.