Apache 2.2系 スタートアップスクリプトまとめ!

Apache 2.2系のスタートアップスクリプトのまとめです!
文中にあるけど、2.4はlockfileの部分をなくせばいいだけなような気もするけど、使ってないからわからんので、2.4は他をあたってくださいw


<推奨される起動方法について>
http://httpd.apache.org/docs/2.2/invoking.html
http://httpd.apache.org/docs/2.4/invoking.html
2.2/2.4ともに、apachectl 制御スクリプトを使用するのが推奨らしいです。
ただ、サーバー起動時は勝手にこのスクリプトはなめませんので、自動起動したい場合には、
init.dにスタートアップスクリプトを用意し、「chkconfig httpd on」で登録しておきましょう。
httpdの上げ下げは、スタートアップスクリプトも直接httpdを叩くより、推奨どおりapachectl制御スクリプトへのリンクで作るとよいでしょう。


<PidFileについて>
http://httpd.apache.org/docs/2.2/mod/mpm_common.html#pidfile
http://httpd.apache.org/docs/2.4/mod/mpm_common.html#pidfile
PidFile(httpd.pid)における2.2/2.4のデフォルト保存先は、logs/httpd.pidとなっていますが、
http.confで指定すれば変更できます。デフォルトは使いづらいのでconfで変更しておきましょう。


<lockfileについて>
http://httpd.apache.org/docs/2.2/mod/mpm_common.html#lockfile
https://httpd.apache.org/docs/2.4/mod/core.html#mutex
lockfileは、2.2はlogs/httpd.pidが指定されてますが、pidfileと違って自動保存や削除はされません(2.4はこのディレクティブ自体が削除されました)。
2.2で自動作成・削除する場合には、スタートアップスクリプトで指定しましょう。


<apache付属のスタートアップスクリプト サンプルについて>
コンパイルしたときにサンプルとして作られるものがあります(httpd-2.2.xx/build/rpm/httpd.init)。
これには以下のようなものが含まれてます。
・chkconfigのスタートアップ順番の情報など
・/etc/rc.d/init.d/functionsの読み込み(daemonルーチンの読み込みなど)
・起動時の言語指定(LANG=C)
・起動時のlockfile作成
・停止時のlockfile削除
一番下に参考として張っておきました~


<2.2用 スタートアップスクリプト 例>
実際のスタートアップスクリプトの例です(ところどころサンプルパクった自作なので使用上の注意をよくよんでお使いください(笑))。

変数を見てもらえれば想定している環境はほとんどわかってもらえると思いますが、httpd.confに2行だけ追加してもらうことが推奨です。
ポイントはrestart時に確実にstopしたのを確認してから、startしているようにしている点などです。

/usr/local/apache2/conf/httpd.conf
PidFile /var/run/httpd.pid
LockFile /var/lock/subsys/httpd


/etc/init.d/httpd
#!/bin/sh
#
# chkconfig: - 85 15
# description: Apache 2.2.xx
# Source function library.
. /etc/rc.d/init.d/functions
prog=httpd
httpd=/usr/local/apache2/bin/${prog}
CONFFILE=/usr/local/apache2/conf/${prog}.conf
pidfile=/var/run/${prog}.pid
lockfile=/var/lock/subsys/${prog}
apachectl=/usr/local/apache2/bin/apachectl
start() {
        echo -n $"Starting $prog: "
daemon $apachectl -f ${CONFFILE}
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
stop()  {
        echo -n $"Stopping $prog: "
daemon $apachectl -k stop
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile}
}
restart() {
        if [ -f ${pidfile} ]; then
           stop
           ROOP=0
           while test $ROOP -lt 600
              do
                 if [ ! -f ${pidfile} ]; then
                    break
                 else
                    sleep 1
                    ROOP=`expr $ROOP + 1`
                 fi
              done
  start
        else
           echo "$prog not running."
        fi
}
reload() {
        echo -n $"Reloading $prog: "
        if test -f ${pidfile}; then
        daemon $apachectl -k restart
        RETVAL=$?
        echo
        else
        echo "$prog not running."
        fi
}
graceful() {
        echo -n $"Graceful $prog: "
        if test -f ${pidfile}; then
        daemon $apachectl -k graceful
        RETVAL=$?
        echo
        else
        echo "${prog} not running."
        fi
}
configtest() {
        echo "Check to ${CONFFILE}"
        $apachectl -t -f ${CONFFILE}
}
version() {
        $apachectl -V
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  graceful)
        graceful
        ;;
  version)
        version
        ;;
  configtest)
        configtest
        ;;
  status|help|v)
$apachectl $@
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|reload|status|graceful|help|configtest|version}"
        exit 1
esac


<参考:apache 2.2.24をビルドしたときについてくるサンプル完コピ>

httpd-2.2.xx/build/rpm/httpd.init
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# httpd        Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#             server implementing the current HTTP standards.
# processname: httpd
# pidfile: /var/log/httpd/httpd.pid
# config: /etc/sysconfig/httpd
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server
#  implementing the current HTTP standards.
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# What were we called? Multiple instances of the same daemon can be
# created by creating suitably named symlinks to this startup script
prog=$(basename $0 | sed -e 's/^[SK][0-9][0-9]//')
if [ -f /etc/sysconfig/${prog} ]; then
        . /etc/sysconfig/${prog}
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.
httpd=${HTTPD-/usr/sbin/httpd}
pidfile=${PIDFILE-/var/log/httpd/${prog}.pid}
lockfile=${LOCKFILE-/var/lock/subsys/${prog}}
RETVAL=0
# 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 grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then
                echo
                echo 1>&2 " Apache 1.3 configuration directives found"
                echo 1>&2 " please read @docdir@/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
}
stop() {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d 10 $httpd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
        echo -n $"Reloading $prog: "
        check13 || exit 1
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        echo
}
# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        if ! test -f ${pidfile}; then
            echo $prog is stopped
            RETVAL=3
        else
            status -p ${pidfile} $httpd
            RETVAL=$?
        fi
        ;;
  restart)
        stop
        start
        ;;
  condrestart)
        if test -f ${pidfile} && status -p ${pidfile} $httpd >&/dev/null; then
                stop
                start
        fi
        ;;
  reload)
        reload
        ;;
  configtest)
        LANG=$HTTPD_LANG $httpd $OPTIONS -t
        RETVAL=$?
        ;;
  graceful)
        echo -n $"Gracefully restarting $prog: "
        LANG=$HTTPD_LANG $httpd $OPTIONS -k $@
        RETVAL=$?
        echo
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|graceful|help|configtest}"
        exit 1
esac
exit $RETVAL

以上!