通过sudo apt-get install nginx安装nginx
nginx 开机自启动脚本位置:/etc/init.d/nginx
禁止开机自启动:sudo update-rc.d -f nginx remove
运行结果:
Removing any system startup links for /etc/init.d/nginx …
/etc/rc0.d/K20nginx
/etc/rc1.d/K20nginx
/etc/rc2.d/S20nginx
/etc/rc3.d/S20nginx
/etc/rc4.d/S20nginx
/etc/rc5.d/S20nginx
/etc/rc6.d/K20nginx
添加开机自启动:sudo update-rc.d -f nginx defaults
尽管/etc/init.d目录中的脚本可以启动和停止各个服务,但在系统引导时,init并不是直接在/etc/init.d目录下找各个服务的启动脚本,而是在/etc/rc.d/目录下查找,该目录包含rc0.d、rc1.d等分别代表不同的init启动级别的子目录。
你可以用Runlevel命令查看当前你的系统是在那个运行级
rcx.d目录下指向启动脚本的符号链接是由K或S+数字+服务名 所组成,中间这个数字十分重要,系统启动时按照这个数字递增执行所有S开头的脚本,系统关闭时按照这个数字递减执行所有K开头的脚本。
如何自定义符号链接? 启动脚本: 因为我刚才的运行级是2级 所以在/etc/rc.d/rc2.d 目录下运行
#ln -s /etc/init.d/nginx S99nginx //重新定义nginx服务的脚本启动顺序
脚本内容:
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# 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=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
. /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
test_nginx_config() {
if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
return 0
else
$DAEMON -t $DAEMON_OPTS
return $?
fi
}
case “$1” in
start)
echo -n “Starting $DESC: ”
test_nginx_config
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n “$ULIMIT” ]; then
# Set the ulimits
ulimit $ULIMIT
fi
start-stop-daemon –start –quiet –pidfile /var/run/$NAME.pid
–exec $DAEMON — $DAEMON_OPTS || true
echo “$NAME.”
;;
stop)
echo -n “Stopping $DESC: ”
start-stop-daemon –stop –quiet –pidfile /var/run/$NAME.pid
–exec $DAEMON || true
echo “$NAME.”
;;
restart|force-reload)
echo -n “Restarting $DESC: ”
start-stop-daemon –stop –quiet –pidfile
/var/run/$NAME.pid –exec $DAEMON || true
sleep 1
test_nginx_config
start-stop-daemon –start –quiet –pidfile
/var/run/$NAME.pid –exec $DAEMON — $DAEMON_OPTS || true
echo “$NAME.”
;;
reload)
echo -n “Reloading $DESC configuration: ”
test_nginx_config
start-stop-daemon –stop –signal HUP –quiet –pidfile /var/run/$NAME.pid
–exec $DAEMON || true
echo “$NAME.”
;;
configtest|testconfig)
echo -n “Testing $DESC configuration: ”
if test_nginx_config; then
echo “$NAME.”
else
exit $?
fi
;;
status)
status_of_proc -p /var/run/$NAME.pid “$DAEMON” nginx && exit 0 || exit $?
;;
*)
echo “Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}” >&2
exit 1
;;
esac
exit 0