httpd.initd 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/openrc-run
  2. # Copyright 1999-2016 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. extra_commands="configtest modules virtualhosts"
  5. extra_started_commands="configdump fullstatus graceful gracefulstop reload"
  6. description_configdump="Dumps the configuration of the runing apache server. Requires server-info to be enabled and www-client/lynx."
  7. description_configtest="Run syntax tests for configuration files."
  8. description_fullstatus="Gives the full status of the server. Requires lynx and server-status to be enabled."
  9. description_graceful="A graceful restart advises the children to exit after the current request and reloads the configuration."
  10. description_gracefulstop="A graceful stop advises the children to exit after the current request and stops the server."
  11. description_modules="Dump a list of loaded Static and Shared Modules."
  12. description_reload="Kills all children and reloads the configuration."
  13. description_virtualhosts="Show the settings as parsed from the config file (currently only shows the virtualhost settings)."
  14. description_stop="Kills all children and stops the server."
  15. # Apply default values for some conf.d variables.
  16. PIDFILE="${PIDFILE:-/run/httpd/httpd.pid}"
  17. TIMEOUT=${TIMEOUT:-15}
  18. SERVERROOT="${SERVERROOT:-/usr/lib/httpd}"
  19. CONFIGFILE="${CONFIGFILE:-/etc/httpd/conf/httpd.conf}"
  20. LYNX="${LYNX:-lynx -dump}"
  21. STATUSURL="${STATUSURL:-http://localhost/server-status}"
  22. RELOAD_TYPE="${RELOAD_TYPE:-graceful}"
  23. # Append the server root and configuration file parameters to the
  24. # user's APACHE2_OPTS.
  25. APACHE2_OPTS="${APACHE2_OPTS} -d ${SERVERROOT}"
  26. APACHE2_OPTS="${APACHE2_OPTS} -f ${CONFIGFILE}"
  27. # The path to the apache2 binary.
  28. APACHE2="/usr/bin/apachectl"
  29. depend() {
  30. need net
  31. use dns entropy logger mysql netmount postgresql
  32. after sshd
  33. }
  34. configtest() {
  35. ebegin "Checking ${SVCNAME} configuration"
  36. checkconfig
  37. eend $?
  38. }
  39. checkconfd() {
  40. if [ ! -d ${SERVERROOT} ]; then
  41. eerror "SERVERROOT does not exist: ${SERVERROOT}"
  42. return 1
  43. fi
  44. }
  45. checkconfig() {
  46. checkpath --directory /run/httpd
  47. checkconfd || return 1
  48. OUTPUT=$( ${APACHE2} ${APACHE2_OPTS} -t 2>&1 )
  49. ret=$?
  50. if [ $ret -ne 0 ]; then
  51. eerror "${SVCNAME} has detected an error in your setup:"
  52. printf "%s\n" "${OUTPUT}"
  53. fi
  54. return $ret
  55. }
  56. start() {
  57. checkconfig || return 1
  58. if [ -n "${STARTUPERRORLOG}" ] ; then
  59. # We must make sure that we only append to APACHE2_OPTS
  60. # in start() and not in stop() or anywhere else that may
  61. # be executed along with start(), see bug #566726.
  62. APACHE2_OPTS="${APACHE2_OPTS} -E ${STARTUPERRORLOG}"
  63. fi
  64. ebegin "Starting ${SVCNAME}"
  65. # Use start stop daemon to apply system limits #347301
  66. start-stop-daemon --start -- ${APACHE2} ${APACHE2_OPTS} -k start
  67. local i=0 retval=1
  68. while [ $i -lt ${TIMEOUT} ] ; do
  69. if [ -e "${PIDFILE}" ] ; then
  70. retval=0
  71. break
  72. fi
  73. sleep 1 && i=$(expr $i + 1)
  74. done
  75. eend ${retval}
  76. }
  77. stop() {
  78. if [ "${RC_CMD}" = "restart" ]; then
  79. checkconfig || return 1
  80. fi
  81. PID=$(cat "${PIDFILE}" 2>/dev/null)
  82. if [ -z "${PID}" ]; then
  83. einfo "${SVCNAME} not running (no pid file)"
  84. return 0
  85. fi
  86. ebegin "Stopping ${SVCNAME}"
  87. ${APACHE2} ${APACHE2_OPTS} -k stop
  88. local i=0 retval=0
  89. while ( test -f "${PIDFILE}" || pgrep -P ${PID} httpd >/dev/null ) \
  90. && [ $i -lt ${TIMEOUT} ]; do
  91. sleep 1 && i=$(expr $i + 1)
  92. done
  93. [ -e "${PIDFILE}" ] && retval=1
  94. eend ${retval}
  95. }
  96. reload() {
  97. checkconfig || return 1
  98. if [ "${RELOAD_TYPE}" = "restart" ]; then
  99. ebegin "Restarting ${SVCNAME}"
  100. ${APACHE2} ${APACHE2_OPTS} -k restart
  101. eend $?
  102. elif [ "${RELOAD_TYPE}" = "graceful" ]; then
  103. ebegin "Gracefully restarting ${SVCNAME}"
  104. ${APACHE2} ${APACHE2_OPTS} -k graceful
  105. eend $?
  106. else
  107. eerror "${RELOAD_TYPE} is not a valid RELOAD_TYPE. Please edit /etc/conf.d/${SVCNAME}"
  108. fi
  109. }
  110. graceful() {
  111. checkconfig || return 1
  112. ebegin "Gracefully restarting ${SVCNAME}"
  113. ${APACHE2} ${APACHE2_OPTS} -k graceful
  114. eend $?
  115. }
  116. gracefulstop() {
  117. checkconfig || return 1
  118. ebegin "Gracefully stopping ${SVCNAME}"
  119. ${APACHE2} ${APACHE2_OPTS} -k graceful-stop
  120. eend $?
  121. }
  122. modules() {
  123. checkconfig || return 1
  124. ${APACHE2} ${APACHE2_OPTS} -M 2>&1
  125. }
  126. fullstatus() {
  127. if ! command -v $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then
  128. eerror "lynx not found! you need to emerge www-client/lynx"
  129. else
  130. ${LYNX} ${STATUSURL}
  131. fi
  132. }
  133. virtualhosts() {
  134. checkconfig || return 1
  135. ${APACHE2} ${APACHE2_OPTS} -S
  136. }
  137. configdump() {
  138. INFOURL="${INFOURL:-http://localhost/server-info}"
  139. checkconfd || return 1
  140. if ! command -v $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then
  141. eerror "lynx not found! you need to emerge www-client/lynx"
  142. else
  143. echo "${APACHE2} started with '${APACHE2_OPTS}'"
  144. for i in config server list; do
  145. ${LYNX} "${INFOURL}/?${i}" | sed '/Apache Server Information/d;/^[[:space:]]\+[_]\+$/Q'
  146. done
  147. fi
  148. }
  149. # vim: ts=4 filetype=gentoo-init-d