当前位置:Gxlcms > PHP教程 > centos下nginx启动脚本和chkconfig管理

centos下nginx启动脚本和chkconfig管理

时间:2021-07-01 10:21:17 帮助过:24人阅读

在安装完nginx后,重新启动需要“kill -HUP nginx进程编号”来进行重新加载,显然十分不方便。如果能像apache一样,直接通过脚本进行管理就方便多了。

nginx官方早就想好了,也提供了这个脚本,地址:http://wiki.nginx.org/RedHatNginxInitScript。这里将管理脚本收录在这里:

  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. # Source function library.
  13. . /etc/rc.d/init.d/functions
  14. # Source networking configuration.
  15. . /etc/sysconfig/network
  16. # Check that networking is up.
  17. [ "$NETWORKING"= "no"] && exit0
  18. nginx="/usr/sbin/nginx"
  19. prog=$(basename$nginx)
  20. NGINX_C/etc/nginx/nginx.conf"
  21. [ -f /etc/sysconfig/nginx] && . /etc/sysconfig/nginx
  22. lockfile=/var/lock/subsys/nginx
  23. make_dirs() {
  24. # make required directories
  25. user=`$nginx -V 2>&1 | grep"configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  26. if[ -z "`grep $user /etc/passwd`"]; then
  27. useradd-M -s /bin/nologin$user
  28. fi
  29. opti -V 2>&1 | grep'configure arguments:'`
  30. foropt in$options; do
  31. if[ `echo$opt | grep'.*-temp-path'` ]; then
  32. value=`echo$opt | cut-d "="-f 2`
  33. if[ ! -d "$value"]; then
  34. # echo "creating" $value
  35. mkdir-p $value && chown-R $user $value
  36. fi
  37. fi
  38. done
  39. }
  40. start() {
  41. [ -x $nginx ] || exit5
  42. [ -f $NGINX_CONF_FILE ] || exit6
  43. make_dirs
  44. echo-n $"Starting $prog: "
  45. daemon $nginx -c $NGINX_CONF_FILE
  46. retval=$?
  47. echo
  48. [ $retval -eq0 ] && touch$lockfile
  49. return$retval
  50. }
  51. stop() {
  52. echo-n $"Stopping $prog: "
  53. killproc $prog -QUIT
  54. retval=$?
  55. echo
  56. [ $retval -eq0 ] && rm-f $lockfile
  57. return$retval
  58. }
  59. restart() {
  60. configtest || return$?
  61. stop
  62. sleep1
  63. start
  64. }
  65. reload() {
  66. configtest || return$?
  67. echo-n $"Reloading $prog: "
  68. killproc $nginx -HUP
  69. RETVAL=$?
  70. echo
  71. }
  72. force_reload() {
  73. restart
  74. }
  75. configtest() {
  76. $nginx -t -c $NGINX_CONF_FILE
  77. }
  78. rh_status() {
  79. status $prog
  80. }
  81. rh_status_q() {
  82. rh_status >/dev/null2>&1
  83. }
  84. case"$1" in
  85. start)
  86. rh_status_q && exit0
  87. $1
  88. ;;
  89. stop)
  90. rh_status_q || exit0
  91. $1
  92. ;;
  93. restart|configtest)
  94. $1
  95. ;;
  96. reload)
  97. rh_status_q || exit7
  98. $1
  99. ;;
  100. force-reload)
  101. force_reload
  102. ;;
  103. status)
  104. rh_status
  105. ;;
  106. condrestart|try-restart)
  107. rh_status_q || exit0
  108. ;;
  109. *)
  110. echo$"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  111. exit2
  112. esac

将以上脚本保存到/etc/init.d/nginx文件,并修改两个地方:

  • nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
  • NGINX_C/etc/nginx/nginx.conf” 修改成配置文件的路径。
  • 保存后,就可以通过该脚本对nginx服务进行管理了:

    $ /etc/init.d/nginx start $ /etc/init.d/nginx stop $ /etc/init.d/nginx reload ...使用chkconfig进行管理

    上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便,比如要设置nginx开机启动等。这时可以使用chkconfig来设置。

    先将nginx服务加入chkconfig管理列表:

    chkconfig --add /etc/init.d/nginx加完这个之后,就可以使用service对nginx进行启动,重启等操作了。

    $ service nginx start $ service nginx stop $ service nginx reload ...设置终端模式开机启动:

    $ chkconfig --level 3 nginx on

    From: http://www.01happy.com/centos-nginx-shell-chkconfig/

    以上就介绍了centos下nginx启动脚本和chkconfig管理,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    人气教程排行