1)在/etc/init.d/目录下创建nginx文件
cd /etc/init.d/
vim nginx
2)编写nginx 脚本,内容如下:
#!/bin/bash
#
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/opt/nginx/sbin/nginx"
NGINX_CONF="/opt/nginx/conf/nginx.conf"
NGINX_PID="/opt/nginx/run/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
3)给nginx添加权限
chmod a+x /etc/init.d/nginx
4)将nginx加入到,开启自启动项
Centos系统下,开机自启配置:
chkconfig --add nginx
chkconfig nginx on
Ubuntu 系统下,开机自启设置:
update-rc.d nginx defaults