在添加nginx服务之后,大家会希望开机伴随启动nginx,避免手动路径输入启动;
nginx官方提供了启动脚本:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
nginx 安装可以参考【Web】Nginx下载与安装
配置步骤
1、添加nginx服务,进入/etc/init.d/目录,新添加nginx脚本文件,内容就是官方起启动脚本(/etc/init.d/nginx),如下:
1 #!/bin/sh 2 # 3 # nginx - this script starts and stops the nginx daemon 4 # 5 # chkconfig: - 85 15 6 # description: NGINX is an HTTP(S) server, HTTP(S) reverse \ 7 # proxy and IMAP/POP3 proxy server 8 # processname: nginx 9 # config: /etc/nginx/nginx.conf 10 # config: /etc/sysconfig/nginx 11 # pidfile: /var/run/nginx.pid 12 13 # Source function library. 14 . /etc/rc.d/init.d/functions 15 16 # Source networking configuration. 17 . /etc/sysconfig/network 18 19 # Check that networking is up. 20 [ "$NETWORKING" = "no" ] && exit 0 21 22 nginx="/usr/sbin/nginx" 23 prog=$(basename $nginx) 24 25 NGINX_CONF_FILE="/etc/nginx/nginx.conf" 26 27 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 28 29 lockfile=/var/lock/subsys/nginx 30 31 make_dirs() { 32 # make required directories 33 user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 34 if [ -n "$user" ]; then 35 if [ -z "`grep $user /etc/passwd`" ]; then 36 useradd -M -s /bin/nologin $user 37 fi 38 options=`$nginx -V 2>&1 | grep 'configure arguments:'` 39 for opt in $options; do 40 if [ `echo $opt | grep '.*-temp-path'` ]; then 41 value=`echo $opt | cut -d "=" -f 2` 42 if [ ! -d "$value" ]; then 43 # echo "creating" $value 44 mkdir -p $value && chown -R $user $value 45 fi 46 fi 47 done 48 fi 49 } 50 51 start() { 52 [ -x $nginx ] || exit 5 53 [ -f $NGINX_CONF_FILE ] || exit 6 54 make_dirs 55 echo -n $"Starting $prog: " 56 daemon $nginx -c $NGINX_CONF_FILE 57 retval=$? 58 echo 59 [ $retval -eq 0 ] && touch $lockfile 60 return $retval 61 } 62 63 stop() { 64 echo -n $"Stopping $prog: " 65 killproc $prog -QUIT 66 retval=$? 67 echo 68 [ $retval -eq 0 ] && rm -f $lockfile 69 return $retval 70 } 71 72 restart() { 73 configtest || return $? 74 stop 75 sleep 1 76 start 77 } 78 79 reload() { 80 configtest || return $? 81 echo -n $"Reloading $prog: " 82 killproc $nginx -HUP 83 RETVAL=$? 84 echo 85 } 86 87 force_reload() { 88 restart 89 } 90 91 configtest() { 92 $nginx -t -c $NGINX_CONF_FILE 93 } 94 95 rh_status() { 96 status $prog 97 } 98 99 rh_status_q() { 100 rh_status >/dev/null 2>&1 101 } 102 103 case "$1" in 104 start) 105 rh_status_q && exit 0 106 $1 107 ;; 108 stop) 109 rh_status_q || exit 0 110 $1 111 ;; 112 restart|configtest) 113 $1 114 ;; 115 reload) 116 rh_status_q || exit 7 117 $1 118 ;; 119 force-reload) 120 force_reload 121 ;; 122 status) 123 rh_status 124 ;; 125 condrestart|try-restart) 126 rh_status_q || exit 0 127 ;; 128 *) 129 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 130 exit 2 131 esac
2、修改脚本内容
a、在脚本中11行,新增pid文件位置,可以在nginx运行时查看,命令:find / -name nginx.pid
1 pidfile="/data/soft/nginx/logs/nginx.pid"
b、在脚本中22行(nginx="/usr/sbin/nginx")修改nginx脚本文件位置
1 nginx="/data/soft/nginx/sbin/nginx"
c、在脚本中25行(NGINX_CONF_FILE="/etc/nginx/nginx.conf")修改nginx配置文件位置
1 NGINX_CONF_FILE="/data/soft/nginx/conf/nginx.conf"
3、给脚本增加可执行权限,命令:chmod a+x /etc/init.d/nginx
到这一步服务已经添加好了
a、服务启动:service nginx start
b、服务停止:service nginx stop
c、服务重新加载:service nginx reload
4、添加开机启动项
a、添加命令:chkconfig --add /etc/init.d/nginx
b、查看启动项,命令:chkconfig --list
nginx启动项状态:nginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off
c、需要设置nginx启动命令:chkconfig nginx on
nginx启动项状态:nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
d、关闭命令为:chkconfig nginx off
e、删除命令为:chkconfig --del nginx
5、执行命令reboot,重启服务器即可验证nginx开机启动