【 XBATU . COM 】    【 MOZICHINA . COM 】    【 2858999 . COM 】


 

ubuntu14.04设置nginx开机自启动

webmaster@李松涛 提交于 周一, 01/18/2016 - 22:40

ubuntu14.04 设置 nginx开机自启动

update-rc.d命令,是用来自动的升级System V类型初始化脚本,它用来设置哪些东西是系统在开机的时候自动运行的,哪些是关机时自动停止的。

首先 我们需要在 /etc/init.d/目录下创建一个nginx的脚本文件, 输入以下内容:

#! /bin/sh
# Author: rui ding
# Modified: Geoffrey Grosenbach http://www.linuxidc.com
# Modified: Clement NEDELCU
# Modified: lst, http://www.xbatu.com
# Reproduced with express authorization from its contributors
# Every script you write should include set -e at the top. This tells bash that it should exit
# the script if any statement returns a non-true return value. The benefit of using -e is that it
# prevents errors snowballing into serious issues when they could have been caught earlier. Again,
# for readability you may want to use set -o errexit.
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
然后输入 以下命令
chmod a+x nginx
update-rc.d –f nginx defaults
搞定!重启试试看。
如果要取消开机启动可以这样
update-rc.d -f nginx remove
ubuntu14.04 取消 apache2开机自启动

取消apache2的开机自启动可以运行下面的命令:
update-rc.d -f apache2 remove


【 XBATU . COM 】    【 MOZICHINA . COM 】    【 2858999 . COM 】