Ubuntu14.04配置nginx開機自啟動項

這里需要特別說明的是,Ubuntu系統(tǒng)下沒有RedHat系統(tǒng)下的chkconfig命令。
但Ubuntu有一個類似的命令: sysv-rc-conf

通過apt-get命令完成sysv-rc-conf軟件的安裝。

背景

Linux系統(tǒng)的運行級別有7個,分別對應的:

  • 0: 關機
  • 1: 單用戶(維護)
  • 2~5: 多用戶
  • 6: 重啟

可以通過runlevel命令來查看當前系統(tǒng)的運行等級:

wds@wds-VirtualBox:~$ runlevel
N 2

其中第一個表示上一次的運行等級,N表示沒有上一次運行等級的記錄;第二個表示當前運行等級,這里為2.

Linux中所有開機自啟動項目運行腳本都放在/etc/init.d/目錄下;同時在/etc/目錄下有rc?.d目錄,分別對應了7中不同的運行級別:

wds@wds-VirtualBox:/$ ls  /etc/ | grep ^rc
rc0.d
rc1.d
rc2.d
rc3.d
rc4.d
rc5.d
rc6.d
rc.local
rcS.d

這里rc2.d目錄就對應了我們系統(tǒng)當前的運行等級。

其中里面的一些文件其實都是/etc/init.d/目錄下文件的軟鏈接:

wds@wds-VirtualBox:/etc/rc2.d$ ls -ltr
total 4
-rw-r--r-- 1 root root 677  3月 13  2014 README
lrwxrwxrwx 1 root root  18 12月  8 19:49 S99rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root  18 12月  8 19:49 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx 1 root root  18 12月  8 19:49 S70pppd-dns -> ../init.d/pppd-dns
lrwxrwxrwx 1 root root  19 12月  8 19:49 S70dns-clean -> ../init.d/dns-clean
lrwxrwxrwx 1 root root  15 12月  8 19:49 S50saned -> ../init.d/saned
lrwxrwxrwx 1 root root  27 12月  8 19:49 S20speech-dispatcher -> ../init.d/speech-dispatcher
lrwxrwxrwx 1 root root  15 12月  8 19:49 S20rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root  20 12月  8 19:49 S20kerneloops -> ../init.d/kerneloops
lrwxrwxrwx 1 root root  21 12月  9 17:25 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx 1 root root  15 12月  9 17:45 S20nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root  17 12月  9 17:47 S20php-fpm -> ../init.d/php-fpm

整個開機自啟動項的流程如下:

  1. 開機后,系統(tǒng)獲得當前的運行等級(例如這里的等級為2);
  2. 運行/etc/rc?.d目錄下的所有可執(zhí)行文件(這里運行/etc/rc2.d/目錄下所有的軟鏈接。這些軟鏈接的源文件都保存在/etc/init.d/目錄下)。

因此我們只需要在/etc/init.d/完成啟動nginx進程的腳本,然后在/etc/rc2.d/做對應的軟鏈接即可。

配置nginx自啟動文件

創(chuàng)建/etc/init.d/nginx文件

#! /bin/sh
# Author: rui ding
# Modified: Geoffrey Grosenbach http://www.linuxidc.com
# Modified: Clement NEDELCU
# Reproduced with express authorization from its contributors
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME


# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0

d_start() {
        $DAEMON || echo -n " already running"
}

d_stop() {
        $DAEMON –s quit || echo -n " not running"
}

d_reload() {
        $DAEMON –s reload || echo -n " could not reload"
}

case "$1" in
    start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0

然后利用sysv-rc-conf命令將其在對應rc?.d目錄下建立一個軟鏈接:

root@wds-VirtualBox:~# sysv-rc-conf nginx on

該命令會在rc2.d ~ rc5.d目錄下都建立了一個nginx的軟鏈接。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容