Linux服务源码安装后开机自启动03-nginx

1、在/etc/init.d目录下新建nginx文件

sudo vim /etc/init.d/nginx

2、粘贴以下代码

#!/bin/bash
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: nginx
# Description: penavico nginx
### END INIT INFO
#
#nginx 安装实际安装地址
NGINX_PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
# nginx路径
DAEMON=/usr/sbin/$NAME
# 配置文件路径
CONFIGFILE=$NGINX_PATH/nginx.conf
# PID文件路径(在nginx.conf设置)
PIDFILE=$NGINX_PATH/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

rh_start() {
$DAEMON -c $CONFIGFILE || echo -n " already running"
}

rh_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}

rh_reload() {
# kill -HUP `cat $PIDFILE` || echo -n " can't reload"
$DAEMON -s reload || echo -n " can't reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
rh_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
rh_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
rh_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
rh_stop
sleep 1
rh_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0


3、更改文件目录权限

sudo chmod +x /etc/init.d/nginx

4、设置开机启动 

[ubuntu]sudo update-rc.d nginx defaults

5、设置完毕,关机重启。查看是否开机自启。

posted @ 2018-04-30 11:00  Jimmy Can  阅读(295)  评论(0编辑  收藏  举报