1.在/etc/init.d/下面建一个名叫nginx的文件
#! /bin/sh
#chkconfig: 2345 80 90
#description:auto_run
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
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"
}
d_check(){
$DAEMON -t|| echo -n " could not check config"
}
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."
;;
check)
echo -n "Checking $DESC: checking..."
d_check
echo "checked."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage:$SCRIPTNAME {start|stop|restart|reload}"
exit 3
;;
esac
exit 0
2.授权脚本可执行chmod +x /etc/init.d/nginx
3.命令启动service nginx start
或/etc.init.d/nginx start
4.自启动
Ubuntu:update-rc.d -f nginx defaults
CentOS:chkconfig --add nginx
chkconfig --list nginx