Nginx-init-RHEL 2.4 KB

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