Tips記事
» 2002年05月29日 00時00分 UPDATE

ソ−スコンパイルのApacheも/etc/rc.d/init.d/下のスクリプトで制御させたい

[木田佳克,ITmedia]

 Apacheをソースコンパイルさせると,標準設定で「/usr/local/apache」(Apache2は,/usr/local/apache2/)ディレクトリ下にインストールされる。起動スクリプトは,「/usr/local/apache/bin/apachectl」だ。

 RPMパッケージであれば,「/etc/rc.d/init.d/」ディレクトリ下に制御スクリプトが自動生成されるが,Apacheを問わずソースコンパイルを行うとほかのRPMとは異なる起動方法を強いられてしまう。

 しかし,独自の起動スクリプトへのパスを覚えたり,自動起動させるために/etc/rc.d/rc.localへ記述させるのはスマートではない。ほかのRPMパッケージと同じく,/etc/rc.d/init.d/下に次のスクリプトを用意しよう。

# cat /etc/rc.d/init.d/httpd
#!/bin/bash
#
# Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide 
Web server. It is used to serve \ # HTML files and CGI. # processname: httpd # pidfile: /var/run/httpd.pid # config: /etc/httpd/conf/access.conf # config: /etc/httpd/conf/httpd.conf # config: /etc/httpd/conf/srm.conf # 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="" # Path to the apachectl script, server
binary, and short-form for messages. apachectl=/usr/local/apche/bin/apachectl httpd=/usr/local/apache/bin/httpd prog=httpd RETVAL=0 # Find the installed modules and convert
their names into arguments httpd # can use. moduleargs() { moduledir=/usr/lib/apache moduleargs=` /usr/bin/find ${moduledir} -type f
-perm -0100 -name "*.so" | env -i tr '[:lower:]'
'[:upper:]' | awk '{\ gsub(/.*\//,"");\ gsub(/^MOD_/,"");\ gsub(/^LIB/,"");\ gsub(/\.SO$/,"");\ print "-DHAVE_" $0}'` echo ${moduleargs} } # 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: " daemon $httpd `moduleargs` $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock
/subsys/httpd return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/
subsys/httpd /var/run/httpd.pid } reload() { echo -n $"Reloading $prog: " killproc $httpd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f /var/run/httpd.pid ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|
restart|condrestart|reload|status|
fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL

 上記のスクリプトは,RPMで自動生成されたものを利用する方法だ。

 ほかにもスマートな方法があり,次のようにシンボリックリンクを作成し,ソースコンパイルで生成されたスクリプト(apachectl)を利用するのもよいだろう。

# ln -s /usr/local/apache/bin/apachectl 
/etc/rc.d/init.d/http

 ただし,この方法では/etc/rc.d/init.d/httpファイルを元として「/etc/rc.d/rc2.d/」〜「/etc/rc.d/rc6.d/」ディレクトリ下にシンボリックリンクを作成できなくなってしまう(理由が分からない人は,Linux How-To「起動スクリプトを知って基礎を理解しよう」を参考のこと)。

 これを回避するためには,/usr/local/apache/bin/apachectlをリネームし,/etc/rc.d/init.d/httpとしてコピーしてしまうのがよいかもしれない。

 最後に,作成したスクリプトファイルに実効権限を設定することを忘れなく。

# chmod 755 /etc/rc.d/init.d/http

Copyright © ITmedia, Inc. All Rights Reserved.

注目のテーマ