添加Nginx為系統(tǒng)服務(wù)(設(shè)置開機(jī)啟動(dòng))
在本節(jié)中,我們將創(chuàng)建一個(gè)腳本,將Nginx守護(hù)進(jìn)程轉(zhuǎn)換為實(shí)際的系統(tǒng)服務(wù)。 這有兩個(gè)作用:守護(hù)程序可以使用標(biāo)準(zhǔn)命令控制,更重要的是,它可以在系統(tǒng)啟動(dòng)時(shí)自動(dòng)啟動(dòng),并在系統(tǒng)關(guān)閉時(shí)停止。
System V scripts
大數(shù)基于Linux的操作系統(tǒng)使用System-V風(fēng)格的init守護(hù)進(jìn)程。 換句話說,他們的啟動(dòng)過程由一個(gè)稱為init的守護(hù)進(jìn)程管理。
這個(gè)守護(hù)進(jìn)程基于運(yùn)行級(jí)別的原理運(yùn)行,它代表計(jì)算機(jī)的狀態(tài)。 這里是一個(gè)表表示各種運(yùn)行級(jí)別:
運(yùn)行級(jí)別 | 狀態(tài) |
0 | 系統(tǒng)關(guān)閉 |
1 | 單用戶模式(救援模式) |
2 | 多用戶模式,不支持NFS |
3 | 完全多用戶模式 |
4 | 未使用 |
5 | 圖形界面模式 |
6 | 系統(tǒng)重新啟動(dòng) |
關(guān)于init腳本
init腳本(也稱為服務(wù)啟動(dòng)腳本或甚至sysv腳本)是一個(gè)符合某種標(biāo)準(zhǔn)的shell腳本。 該腳本通過諸如開始,停止和其他等命令來控制守護(hù)進(jìn)程應(yīng)用程序。首先,當(dāng)計(jì)算機(jī)啟動(dòng)時(shí),init守護(hù)程序?qū)⑹褂胹tart參數(shù)運(yùn)行腳本。 另一種可能性是通過從shell手動(dòng)執(zhí)行腳本:
[root@example.com ~]# service nginx start
或者如果您的系統(tǒng)沒有service命令:
[root@example.com ~]# /etc/init.d/nginx start
Nginx init腳本
/etc/init.d/nginx:
基于Debian的發(fā)行版本
#! /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=/opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid --exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/nginx.pid --exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
基于Red Hat的發(fā)行版本
#!/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
注意修改腳本中的路徑。
安裝Nginx init腳本
添加執(zhí)行權(quán)限:
[root@example.com ~]# chmod +x /etc/init.d/nginx
Debian-based發(fā)行版本:
[root@example.com ~]# update-rc.d -f nginx defaults
Red Hat–based發(fā)行版本:
[root@example.com ~]# chkconfig nginx on
會(huì)員登錄
賬號(hào)登錄還沒有賬號(hào)?立即注冊