ubuntu14下nginx自启动脚步编写

来源:互联网 发布:不用网络的游戏大全 编辑:程序博客网 时间:2024/06/05 00:56

手动安装安装完nginx,如果需要下次开机自动启动还需要写一个简单脚步,以下为具体步骤(默认root用户操作,非root用户 部分指令禁止操作)

在 /etc/init.d/目录下新建一个nginx脚本文件

执行指令 vi /etc/init.d/nginx  写好脚本后,:wq保存退出

给文件增加可执行权限 chmod +x nginx 

切换到  /etc/init.d/下

执行 ./nginx start 

执行 ps -ef|grep nginx 查看进程是否存在,显示以为内容说明启动成功

root      1502     1  0 14:06 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    1503  1502  0 14:06 ?        00:00:00 nginx: worker process    

重启系统后 nginx将自动启动


以下为nginx脚步内容 脚本头部的变量可能因系统的不同需要修改

#! /bin/sh



PATH=/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME


set -e
[ -x "$DAEMON" ] || exit 0


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


do_stop() {
 if [ -f "$PIDFILE" ]; then  
  kill -INT `cat $PIDFILE` || echo -n "nginx not running"
 fi 
}


do_reload() {
kill -HUP cat $PIDFILE || echo -n "nginx can't reload"
}


case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac


exit 0