自宅の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_*」に対応のため微修正。


コメントする