Nginx-init-CentOS 2.5 KB

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