汎用apache起動スクリプトをでっちあげた

| | コメント(0) | トラックバック(0)

自宅のVMware Server上のCentOS 4.5で、いろいろhttpdを立ち上げて開発環境を区別していて、いちいち開発環境を立ち上げるたびに起動スクリプトを書くのが億劫になってきて、汎用スクリプトをでっちあげた。

basenameで起動パスを識別するようにしたので、シンボリックリンクで別名を付けてやるだけで任意のhttpdを立ち上げてくれるという仕組み。

こうしておけば、apacheを使ったWebアプリのプロジェクトを複数抱えてても面倒臭くないよ!

apache以下一連の環境をconfigureする手間はやっぱりかかるけどw

あととりあえずhttpd.workerは捨ててます(PHP(笑))

使い方:(※注:以下、fooは任意の文字列)

まずシンボリックリンクを張って

ln -s /etc/rc.d/init.d/httpd_template /etc/rc.d/init.d/httpd_foo

あとは普通に呼び出すだけ

/etc/rc.d/init.d/httpd_foo start
/etc/rc.d/init.d/httpd_foo stop
/etc/rc.d/init.d/httpd_foo restart
などなど

このスクリプトを使用する際の前提条件:

  • configure時に--prefix=/usr/local/apache_foo でビルドされていること
  • 起動スクリプトは /etc/sysconfig/httpd_foo を読み込みます
  • httpd.conf で PidFile が /var/run/httpd_foo.pid に設定されていること
  • ロックファイルは /var/lock/subsys/httpd_foo を見に行きます
  • シンボリックリンクを張ること(killallするときに便利だから):/usr/local/apache_foo/bin/httpd -> /usr/local/apache_foo/bin/httpd_foo

おすすめの構成はこんな感じ:

  • /usr/local/apache_base (port 80, mod_rewrite + mod_proxy にて各プロジェクト用にポート転送)
    • /usr/local/apache_foo (port 10080)
    • /usr/local/apache_bar (port 10081)
    • and so on.

スクリプトはhttpd-2.2.8に付属していたbuild/rpm/httpd.initをベースにしています。

/etc/rc.d/init.d/httpd_template

#!/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.
#
#
# Startup script for the Apache Web Server
#
# chkconfig: 2345 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#	       HTML files and CGI.

base=`/bin/basename $0`
base=${base##S85}
base=${base##K15}
label=${base##httpd_}
[ $label == "template" ] && exit 1

sysconfig=/etc/sysconfig/$base

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

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

[ -f $sysconfig ] && . $sysconfig

# 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.
apachebasedir=/usr/local/apache2_$label
apachectl=$apachebasedir/bin/apachectl
httpd=${HTTPD-$apachebasedir/bin/$base}
prog=$base
lockfile=/var/lock/subsys/$prog
pidfile=/var/run/${prog}.pid
RETVAL=0

# check for 1.3 configuration
check13 () {
    CONFFILE=$apachebasedir/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
    daemon --check $prog $httpd $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch $lockfile
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f $lockfile $pidfile
}

restart() {
    stop
    start
}

condrestart() {
    [ -f $pidfile ] && restart
}

reload() {
    echo -n $"Reloading $prog: "
    check13 || exit 1
    killproc $prog -HUP
    RETVAL=$?
    echo
}

# See how we were called.
case "$1" in
    start)       start         ;;
    stop)        stop          ;;
    status)      status $httpd ;;
    restart)     restart       ;;
    condrestart) condrestart   ;;
    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

 2008/3/25:起動ファイル「S85httpd_*」「K15httpd_*」に対応のため微修正。

トラックバック(0)

このブログ記事を参照しているブログ一覧: 汎用apache起動スクリプトをでっちあげた

このブログ記事に対するトラックバックURL: http://www.kreis-net.jp/blog/mt-tb.cgi/82

コメントする

2009年11月

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          

ウェブページ

Profile

name: Michiya Honda
nick: PIA
birth: 21-Nov-1975
e-mail: pia at this domain
SNS: mixi, nowa
起業・独立サポート「katana」

このブログ記事について

このページは、PIAが2008年3月 8日 04:00に書いたブログ記事です。

ひとつ前のブログ記事は「Wayback Machine」です。

次のブログ記事は「謹賀新年」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

Powered by Movable Type 4.1