建立一個啟動的 Script 來管理 Nginx Web Server

因為是採用手動編譯的方式來安裝 Nginx Web Server,所以必須要手動來執行或關閉 Nginx Web Server,有些麻煩,後來參考底下的網站,建立一個啟動的 Script 來管理 Nginx Web Server。
關閉
# kill -9 $(ps aux | grep nginx | grep -v grep | awk ‘{print $2}’)
or
# killall nginx
啟動
# /usr/sbin/nginx

參考網站:Websites with Nginx on Debian 7 (Wheezy) – Linode Guides & Tutorials
[@more@]下載 Script 檔
wget -O init-deb.sh http://www.linode.com/docs/assets/1538-init-deb.sh

編輯 Script 檔
# vim init-deb.sh
#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

set -e

case “$1” in
  start)
        echo -n “Starting $DESC: “
        start-stop-daemon –start –quiet –pidfile /var/run/$NAME.pid
                –exec $DAEMON — $DAEMON_OPTS
        echo “$NAME.”
        ;;
  stop)
        echo -n “Stopping $DESC: “
        start-stop-daemon –stop –quiet –pidfile /var/run/$NAME.pid
                –exec $DAEMON
        echo “$NAME.”
        ;;
  restart|force-reload)
        echo -n “Restarting $DESC: “
        start-stop-daemon –stop –quiet –pidfile
                /var/run/$NAME.pid –exec $DAEMON
        sleep 1
        start-stop-daemon –start –quiet –pidfile
                /var/run/$NAME.pid –exec $DAEMON — $DAEMON_OPTS
        echo “$NAME.”
        ;;
  reload)
          echo -n “Reloading $DESC configuration: “
          start-stop-daemon –stop –signal HUP –quiet –pidfile     /var/run/$NAME.pid
              –exec $DAEMON
          echo “$NAME.”
          ;;
      *)
            N=/etc/init.d/$NAME
            echo “Usage: $N {start|stop|restart|reload|force-reload}” >&2
            exit 1
            ;;
    esac

    exit 0

搬移檔案到 /etc/init.d 目錄
# mv init-deb.sh /etc/init.d/nginx

更改檔案權限
# chmod +x /etc/init.d/nginx

測試是否能正常執行
# /etc/init.d/nginx start
Starting nginx: nginx.

檢查是否有執行成功
# ps aux | grep nginx | grep -v grep
root     12795  0.0  0.0   4484   768 ?        Ss   23:56   0:00 nginx: master process /usr/sbin/nginx
www-data 12796  0.0  0.1   4636  1136 ?        S    23:56   0:00 nginx: worker process

測試關閉
# /etc/init.d/nginx stop
Stopping nginx: nginx.

設定開機時執行
# /usr/sbin/update-rc.d -f nginx defaults
update-rc.d: using dependency based boot sequencing