[关闭]
@songlaf 2016-04-17T16:00:24.000000Z 字数 5386 阅读 554

centos/rhel下实现nginx自启动脚本实例

shell


centos/rhel下实现nginx自启动脚本实例

投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-07-08 我要评论
这篇文章主要介绍了centos/rhel下实现nginx自启动脚本,需要的朋友可以参考下

通常在centos、rhel的OS下,我们大多是通过chkconfig来管理服务,比如开机自动启动服务之类。
这里给大家介绍一个很好用的nginx启动脚本。以及简单的使用,如下:
1. 执行如下命令:
?
1
vim /etc/init.d/nginxd
2.输入如下内容:
注意:该文件中的如下几个变量,都需要根据你实际的目录来改写。

  1. nginx="/usr/local/nginx/sbin/nginx" ##nginx的可执行文件路径
  2. lockfile="/var/nginx/nginx.lock" ##需要先创建/var/nginx目录
  3. NGINX_CONF_FILE="/etc/nginx/nginx.conf" ##需要先创建该配置文件
  4. ```shell
  5. <div class="md-section-divider"></div>
  6. #!/bin/sh
  7. <div class="md-section-divider"></div>
  8. #
  9. <div class="md-section-divider"></div>
  10. # nginx - this script starts and stops the nginx daemon
  11. <div class="md-section-divider"></div>
  12. #
  13. <div class="md-section-divider"></div>
  14. # chkconfig: - 85 15
  15. <div class="md-section-divider"></div>
  16. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  17. <div class="md-section-divider"></div>
  18. # proxy and IMAP/POP3 proxy server
  19. <div class="md-section-divider"></div>
  20. # processname: nginx
  21. <div class="md-section-divider"></div>
  22. # Source function library.
  23. . /etc/rc.d/init.d/functions
  24. <div class="md-section-divider"></div>
  25. # Source networking configuration.
  26. . /etc/sysconfig/network
  27. <div class="md-section-divider"></div>
  28. # Check that networking is up.
  29. [ "$NETWORKING" = "no" ] && exit 0
  30. nginx="/usr/local/nginx/sbin/nginx"
  31. prog=$(basename $nginx)
  32. sysconfig="/etc/sysconfig/$prog"
  33. lockfile="/var/nginx/nginx.lock"
  34. pidfile="/var/nginx/${prog}.pid"
  35. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
  36. [ -f $sysconfig ] && . $sysconfig
  37. start() {
  38. [ -x $nginx ] || exit 5
  39. [ -f $NGINX_CONF_FILE ] || exit 6
  40. echo -n $"Starting $prog: "
  41. daemon $nginx -c $NGINX_CONF_FILE
  42. retval=$?
  43. echo
  44. [ $retval -eq 0 ] && touch $lockfile
  45. return $retval
  46. }
  47. stop() {
  48. echo -n $"Stopping $prog: "
  49. killproc -p $pidfile $prog
  50. retval=$?
  51. echo
  52. [ $retval -eq 0 ] && rm -f $lockfile
  53. return $retval
  54. }
  55. restart() {
  56. configtest_q || return 6
  57. stop
  58. start
  59. }
  60. reload() {
  61. configtest_q || return 6
  62. echo -n $"Reloading $prog: "
  63. killproc -p $pidfile $prog -HUP
  64. echo
  65. }
  66. configtest() {
  67. $nginx -t -c $NGINX_CONF_FILE
  68. }
  69. configtest_q() {
  70. $nginx -t -q -c $NGINX_CONF_FILE
  71. }
  72. rh_status() {
  73. status $prog
  74. }
  75. rh_status_q() {
  76. rh_status >/dev/null 2>&1
  77. }
  78. <div class="md-section-divider"></div>
  79. # Upgrade the binary with no downtime.
  80. upgrade() {
  81. local oldbin_pidfile="${pidfile}.oldbin"
  82. configtest_q || return 6
  83. echo -n $"Upgrading $prog: "
  84. killproc -p $pidfile $prog -USR2
  85. retval=$?
  86. sleep 1
  87. if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
  88. killproc -p $oldbin_pidfile $prog -QUIT
  89. success $"$prog online upgrade"
  90. echo
  91. return 0
  92. else
  93. failure $"$prog online upgrade"
  94. echo
  95. return 1
  96. fi
  97. }
  98. <div class="md-section-divider"></div>
  99. # Tell nginx to reopen logs
  100. reopen_logs() {
  101. configtest_q || return 6
  102. echo -n $"Reopening $prog logs: "
  103. killproc -p $pidfile $prog -USR1
  104. retval=$?
  105. echo
  106. return $retval
  107. }
  108. case "$1" in
  109. start)
  110. rh_status_q && exit 0
  111. $1
  112. ;;
  113. stop)
  114. rh_status_q || exit 0
  115. $1
  116. ;;
  117. restart|configtest|reopen_logs)
  118. $1
  119. ;;
  120. force-reload|upgrade)
  121. rh_status_q || exit 7
  122. upgrade
  123. ;;
  124. reload)
  125. rh_status_q || exit 7
  126. $1
  127. ;;
  128. status|status_q)
  129. rh_$1
  130. ;;
  131. condrestart|try-restart)
  132. rh_status_q || exit 7
  133. restart
  134. ;;
  135. *)
  136. echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
  137. exit 2
  138. esac

nginx="/usr/local/nginx/sbin/nginx" ##nginx的可执行文件路径
lockfile="/var/nginx/nginx.lock" ##需要先创建/var/nginx目录
NGINX_CONF_FILE="/etc/nginx/nginx.conf" ##需要先创建该配置文件

!/bin/sh

nginx - this script starts and stops the nginx daemon

chkconfig: - 85 15

description: Nginx is an HTTP(S) server, HTTP(S) reverse \

proxy and IMAP/POP3 proxy server

processname: nginx

Source function library.

. /etc/rc.d/init.d/functions

Source networking configuration.

. /etc/sysconfig/network

Check that networking is up.

[ "NETWORKING" = "no" ] && exit 0 
nginx="/usr/local/nginx/sbin/nginx"
prog=
(basename prog"
lockfile="/var/nginx/nginx.lock"
pidfile="/var/nginx/sysconfig ] && . sysconfig 
start() { 
  [ -x
nginx ] || exit 5 [ -f "Starting nginx -c ? echo [ retval -eq 0 ] && touchlockfile return retval 

stop() { 
  echo -n
"Stopping pidfile ? echo [ retval -eq 0 ] && rm -flockfile return retval 

restart() { 
  configtest_q || return 6 
  stop 
  start 

reload() { 
  configtest_q || return 6 
  echo -n
"Reloading pidfile prog -HUP 
  echo

configtest() {
nginx -t -c NGINX_CONF_FILE 

configtest_q() {
nginx -t -q -c NGINX_CONF_FILE 

rh_status() { 
  status
prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}

Upgrade the binary with no downtime.

upgrade() {
local oldbin_pidfile=""Upgrading pidfile ?
sleep 1
if [[ -f {oldbin_pidfile} && -f{pidfile} ]]; then
killproc -p prog -QUIT
success prog online upgrade"
echo
return 0
else
failure prog online upgrade"
echo
return 1
fi
}

Tell nginx to reopen logs

reopen_logs() {
configtest_q || return 6
echo -n prog logs: "
killproc -p prog -USR1
retval=retval
}
case "1" in
  start) 
    rh_status_q && exit 0
1
;;
stop)
rh_status_q || exit 0
1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7

    ;; 
  status|status_q) 
    rh_
1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo 0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
exit 2
esac
保存完毕之后,可以执行以下命令来启动nginx
?
1
2
chmod 777 /etc/init.d/nginxd
/etc/init.d/nginxd start
3. 配置开机启动
执行以下命令:
?
1
chkconfig nginxd on
可以以下命令查看是否成功
?
1
chkconfig --list | grep nginxd

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注