#!/bin/bash # # Startup script for the Apache Web Server # # chkconfig: 345 92 8 # description: Apache is a World Wide Web server. It is used to serve \ # HTML files and CGI. # processname: httpd # pidfile: /var/run/httpd.pid ### BEGIN INIT INFO # Provides: httpd # Required-Start: $network # Required-Stop: $network # Should-Start: mysqld postgresql radiusd ldap # Should-Stop: mysqld postgresql radiusd ldap # Default-Start: 3 4 5 # Short-Description: The Apache Web Server. # Description: Apache is a World Wide Web server. It is used to serve # HTML files and CGI. ### END INIT INFO # nuke the environment for i in `env | sed -e '/^[[:blank:]]/d' | cut -d= -f1 | grep "[A-Z]"`; do unset $i; done # Source function library. . /etc/rc.d/init.d/functions # Source the /etc/sysconfig/httpd file. if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi # Start httpd in the C locale by default. export LC_ALL=${HTTPD_LANG-"C"} export LC_CTYPE=${HTTPD_LANG-"C"} export LANG=${HTTPD_LANG-"C"} # Use a safe temp directory export TMP=${HTTPD_TMP:-"/tmp"} export TMPDIR=${HTTPD_TMPDIR:-"/tmp"} # Start with defaults httpdpid=${HTTPDPID:-"/var/run/httpd.pid"} options=${OPTIONS-""} httpd=${HTTPD:-"/usr/sbin/httpd"} statusurl=${STATUSURL:-"http://localhost/server-status"} processname="`basename $httpd`" ## Number of times to try to start Apache in case of error #times_start="1 2 3" times_start="1" sleep_time="1 2 3 4" # Change the major functions into functions. moduleargs() { moduleargs= for module in $1/*.so ; do if [ -x ${module} ] ; then module=`echo ${module} | awk '{ gsub(".*/",""); gsub("^mod_",""); gsub("^lib",""); gsub(".so$",""); print toupper($0)}'` moduleargs="${moduleargs} -DHAVE_$module" fi done echo ${moduleargs} } # Get the extra modules so only the main server gets them extramoduleargs() { extramoduleargs= for extramodule in $1/*.so ; do if [ -x ${extramodule} ] ; then extramodule=`echo ${extramodule} | awk '{ gsub(".*/",""); gsub("^mod_",""); gsub("^lib",""); gsub(".so$",""); print toupper($0)}'` extramoduleargs="${extramoduleargs} -DHAVE_$extramodule" fi done echo ${extramoduleargs} } # NOTE: detect/detectlib must be call before any use of $libdir ! detectlib() { local is_64bit=no case `/usr/bin/file /bin/bash` in *"ELF 64"*) is_64bit=yes ;; esac libdir="/usr/lib" case `uname -m`:$is_64bit in powerpc64:yes | s390x:yes | sparc64:yes | x86_64:yes) libdir="/usr/lib64";; esac } detect() { detectlib httpdconf=${HTTPDCONF:-"-f /etc/httpd/conf/httpd.conf"} extramodules="-DAPACHE2 `extramoduleargs /etc/httpd/extramodules`" modules=`moduleargs /etc/httpd/modules` debug=${DEBUG:-"-DNO_DETACH -DONE_PROCESS -DDEBUG"} } ## Hack for ApacheJServ that takes forever to shut down and restart snooze() { for count in 1 2 3 4 5 6 7 8 9 ; do echo "."; sleep 1; done echo } conftest() { detect badconf=0 if [ -x $httpd ]; then action "Checking configuration sanity for apache: " \ $httpd -t $httpdconf $extramodules $modules $options badconf=$? fi return $badconf } conftest_vhosts() { detect badconf=0 if [ -x $httpd ]; then action "Checking configuration sanity for apache: " \ $httpd -t $httpdconf $extramodules $modules $options -D DUMP_VHOSTS badconf=$? fi return $badconf } sanitizer() { # OE: 20040515 delete corrupt files ssl_scache_file="/var/cache/httpd/mod_ssl/ssl_scache" if [ -f "$ssl_scache_file" ]; then echo "Unclean shutdown detected, $ssl_scache_file exists and is probably corrupted, deleting..." rm -f "$ssl_scache_file" echo fi } semdetect() { # OE: 20040609 detect dead semaphores apache_semid=`ipcs -s|grep apache|grep '^0x00000000'|awk '{ print $2 }'` if ! [ -z "$apache_semid" ]; then number_of_dead_semid=`echo $apache_semid|wc -w` echo "Unclean shutdown detected/something made apache segfault leaving $number_of_dead_semid possible dead semaphores (arrays)." echo "A possible solution could be to tune the kernel using sysctl, check these values:" echo "kernel.shmall, kernel.shmmax, kernel.shmmni, kernel.sem and kernel.msgmni" echo "You may have to start apache with the \"semcleanrestart\" switch." fi } semkill() { # OE: 20040609 kill dead semaphores # OE: 20040610 ipcclean from the postgresql-server package does a much nicer job, # should we use that one instead? apache_semid=`ipcs -s|grep apache|grep '^0x00000000'|awk '{ print $2 }'` if ! [ -z "$apache_semid" ]; then echo -n "Killing semid: " for dead_semid in $apache_semid; do ipcrm -s $dead_semid; echo -n "$dead_semid " done echo fi } pidfilenuker() { if [ -z "`/sbin/pidof $processname`" ] && [ -e $httpdpid ]; then echo -n "removing stale pid file." rm -f $httpdpid echo fi } # check for 2.2 configuration check22() { HTTPD_ROOT=`$httpd -V | grep HTTPD_ROOT | cut -d\" -f2` CONFFILES=`find $HTTPD_ROOT/modules.d $HTTPD_ROOT/conf.d $HTTPD_ROOT/conf/webapps.d $HTTPD_ROOT/conf/vhosts.d -name '*.conf'` HTTPD_CONFFILE=`echo $httpdconf | sed -e 's/-f//'` GONE="(mod_access.c|mod_auth.c|mod_auth_ldap.c|mod_imap.c)" if LANG=C grep -HEin "[[:space:]]*($GONE)" $CONFFILES $HTTPD_CONFFILE ; then echo echo 1>&2 " Obsolete apache 2.0 configuration directives were found as shown above designated by filename:line number:directive." echo 1>&2 " Please read the /usr/share/doc/apache-base-*/new_features*.txt, /usr/share/doc/apache-conf-*/README.urpmi file and" echo 1>&2 " the online apache 2.2 documentation at http://httpd.apache.org/docs/2.2/" echo 1>&2 " The following example statement does not work with apache 2.2.0:" echo 1>&2 "" echo 1>&2 " <Directory /my/secure/files>" echo 1>&2 " Options -All -Multiviews" echo 1>&2 " AllowOverride None" echo 1>&2 " <IfModule mod_access.c> <- REMOVE THIS LINE" echo 1>&2 " Order deny,allow" echo 1>&2 " Deny from all" echo 1>&2 " </IfModule> <- REMOVE THIS LINE" echo 1>&2 " </Directory>" echo 1>&2 "" failure "Apache 2.0 config directives test" echo exit 1 fi } RETVAL=0 # See how we were called. case "$1" in start) if [ -n "`/sbin/pidof $processname`" ] && [ -e $httpdpid ]; then echo -n "apache is already running." echo fi pidfilenuker for tries in $times_start; do if [ -x $httpd -a ! -e $httpdpid ]; then if [ $tries -eq 1 ]; then echo -n "Starting httpd: "; fi check22 || exit 1 detect; sanitizer; semdetect $httpd $httpdconf $extramodules $modules $options && success || failure RETVAL=$? echo if [ $RETVAL -eq 0 ]; then for wait in $sleep_time; do if [ ! -e $httpdpid ]; then sleep 1; fi done fi [ $RETVAL -eq 0 ] && touch /var/lock/subsys/httpd fi done ;; stop) pidfilenuker if [ -e $httpdpid ]; then echo -n "Shutting down httpd: " killproc $processname if [ -e $libdir/apache-extramodules/mod_jserv.so ]; then snooze while [ -e $httpdpid ]; do echo "Trying harder" snooze killproc $processname done fi fi RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/httpd ;; debug) echo -n "Starting httpd (debug mode and in the foreground): " detect $httpd $httpdconf $debug \ -c "Pidfile /tmp/httpd-debug.pid" \ -c "LogLevel debug" \ -c "ErrorLog /dev/stdout" \ -c "CustomLog /dev/stdout common" \ $extramodules $modules $options RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/httpd /tmp/httpd-debug.pid ;; extendedstatus) detect if [ -e $httpdpid ]; then lynx -dump $statusurl | awk ' /process$/ { print; exit } { print } ' echo echo "#######################################" echo "#######################################" fi if [ ! -e $httpdpid ]; then echo "Apache is *not* running." echo RETVAL=3 fi ;; status) detect status $processname echo "Use $0 extendedstatus for more information." echo ;; restart) $0 stop $0 start RETVAL=$? ;; update|condrestart) if [ -e $httpdpid ]; then $0 stop conftest || exit $? $0 start RETVAL=$? fi ;; reload|graceful|condreload) # in doubt, check both possible library dirs if [ -e $libdir/apache-extramodules/mod_jserv.so ]; then $0 stop $0 start else detect if [ -e $httpdpid ]; then echo -n "Reloading httpd: " killproc $processname -USR1 echo fi fi ;; closelogs) detect if [ -e $httpdpid ]; then echo -n "Sending USR1 to httpd: " killproc $processname -USR1 echo fi ;; configtest) conftest exit $? ;; configtest_vhosts) conftest_vhosts exit $? ;; show_defines) # OE 20041013 make it spit out all those -D variables to make it # easier to use something like: # httpd -f /etc/httpd/conf/httpd.conf `/etc/rc.d/init.d/httpd define` -t detect echo "$extramodules $modules $options" exit $? ;; semcleanrestart) if ! [ -e $httpdpid ]; then semkill; detect; stop; detect; start fi ;; *) echo "Usage: $0 {start|stop|restart|reload|graceful|condreload|closelogs|update|condrestart|status|extendedstatus|configtest|configtest_vhosts|semcleanrestart|debug|show_defines}" exit 1 esac exit $RETVAL