nginx 开机自启动脚本

CSDNMrWang 2015-03-19

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

DESC="nginxdaemon"

NAME=nginx

DAEMON=/usr/local/nginx/sbin/$NAME

CONFIGFILE=/usr/local/nginx/conf/$NAME.conf

PIDFILE=/usr/local/nginx/$NAME.pid

SCRIPTNAME=/etc/init.d/$NAME

方法:

1将nginx保存为/etc/init.d/nginx

然后可以通过

/etc/init.d/nginxstart命令启动nginx

/etc/init.d/nginxstop命令停止nginx

/etc/init.d/nginxrestart命令重启nginx

2执行命令

chkconfig--addnginx

chkconfig--levelnginx2345on

3如果出现servicenginxdoesnotsupportchkconfig问题

请看http://blog.csdn.net/gebitan505/article/details/17606799

解决办法就是

加入

#chkconfig:-8515

#description:nginxisaWorldWideWebserver.Itisusedtoserve

4防火墙端口开启

修改文件/etc/sysconfig/iptables

-AINPUT-ptcp-mstate--stateNEW-mtcp--dport[端口]-jACCEPT

然后重启防火墙:serviceiptablesrestart

#! /bin/sh
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# date 2015-03-19 9:25

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/$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() {
 kill -INT `cat $PIDFILE` || echo -n "nginx not running"
}

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

相关推荐