wxyfj 发表于 2018-11-25 12:19:08

第五章 apache启动脚本

  版本V1.0
时间2012-10-04
版权GPL
作者itnihao 邮箱 itnihao@qq.com
博客 http://itnihao.blog.51cto.com
如需重新发行,请注明以上信息,谢谢合作


前言
本文档基于对apache的学习整理而成的笔记。本文档详细的记录了apache各种应用,以及一些个人的理解,如果偏差,请和我联系,以在下一个版本中进行更正。其中大部分文档均来自网络,感谢网络上各位朋友的分享,才有此文档的出现。其中本人对参考的部分网络文档进行适当的修改,以达到更好的参考效果。也希望各位积极的分享文档,为开源事业做出自己力所能及的贡献。
                                             itnihao2012年10月04日于成都
第五章 apache启动脚本
5.1 Apache的启动脚本
    用rpm包安装的apache启动脚本如下,那么用源码安装的apache,我们可以对此脚本进行稍微的修改即可。
# cat etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd      Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.It is used to serve \
#            HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
      . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# check for 1.3 configuration
check13 () {
      CONFFILE=/etc/httpd/conf/httpd.conf
      GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"
      GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"
      GONE="${GONE}AccessConfig|ResourceConfig)"
      if LANG=C grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then
                echo
                echo 1>&2 " Apache 1.3 configuration directives found"
                echo 1>&2 " please read /usr/share/doc/httpd-2.2.3/migration.html"
                failure "Apache 1.3 config directives test"
                echo
                exit 1
      fi
}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.So we just do it the way init scripts
# are expected to behave here.
start() {
      echo -n $"Starting $prog: "
      check13 || exit 1
      LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
      RETVAL=$?
      echo
      [ $RETVAL = 0 ] && touch ${lockfile}
      return $RETVAL
}

# When stopping httpd a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
      echo -n $"Stopping $prog: "
      killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
      RETVAL=$?
      echo
      [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
      RETVAL=$?
      echo $"not reloading due to configuration syntax error"
      failure $"not reloading $httpd due to configuration syntax error"
    else
      killproc -p ${pidfile} $httpd -HUP
      RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
start)
      start
      ;;
stop)
      stop
      ;;
status)
      status -p ${pidfile} $httpd
      RETVAL=$?
      ;;
restart)
      stop
      start
      ;;
condrestart)
      if [ -f ${pidfile} ] ; then
                stop
                start
      fi
      ;;
reload)
      reload
      ;;
graceful|help|configtest|fullstatus)
      $apachectl $@
      RETVAL=$?
      ;;
*)
      echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
      exit 1
esac

exit $RETVAL

Rpm包执行安装前的作,在源码安装apache之前,需要以下的操作
#rpm -q --scripts httpd
preinstall scriptlet (using /bin/sh):
# Add the "apache" user
/usr/sbin/useradd -c "Apache" -u 48 \
      -s /sbin/nologin -r -d /var/www apache 2> /dev/null || :
postinstall scriptlet (using /bin/sh):
# Register the httpd service
/sbin/chkconfig --add httpd
preuninstall scriptlet (using /bin/sh):
if [ $1 = 0 ]; then
      /sbin/service httpd stop > /dev/null 2>&1
      /sbin/chkconfig --del httpd
Fi
5.2用lograte分割apache日志
# cat etc/logrotate.d/httpd   
/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    postrotate
      /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}



页: [1]
查看完整版本: 第五章 apache启动脚本