VineLinux3.x/Apache2.0/インストールおよび設定 のバックアップ(No.14)


概要

WebサーバであるApache2をインストールします。APACHE(A PAtCHy server:つぎはぎだらけのサーバ)。

http://www.apache.jp/

Vine Plusにrpmが用意されていますが、そのパッケージだとなぜかphpが動かないので、ここではソースからインストールします。オプションを指定しないとpreforkでコンパイルされるみたいなので、workerオプションをつけて入れてみました。apache2は動作モードがいろいろあるみたいで、preforkはapache1と同じ非マルチスレッドでworkerはマルチスレッドらしい。

詳しくはこちら http://httpd.apache.org/docs/2.0/mpm.html

SSL対応にする場合、openssl-develをあらかじめインストールしておく。

# apt-get install openssl-devel
パッケージリストを読みこんでいます... 完了
依存情報ツリーを作成しています... 完了
以下のパッケージが新たにインストールされます:
  openssl-devel
アップグレード: 0 個, 新規インストール: 1 個, 削除: 0 個, 保留: 1 個
1649kB のアーカイブを取得する必要があります。
展開後に 3788kB のディスク容量が追加消費されます。
取得:1 http://updates.vinelinux.org 3.1/i386/updates openssl-devel 0.9.7d-0vl3.1 [1649kB]
1649kB を 1s 秒で取得しました (1276kB/s)
変更を適用しています...
Preparing...               ########################################### [100%]
   1:openssl-devel         ########################################### [100%]
完了

apache2のダウンロード

# wget http://www.apache.jp/dist/httpd/httpd-2.0.54.tar.gz
--13:25:20--  http://www.apache.jp/dist/httpd/httpd-2.0.54.tar.gz
           => `httpd-2.0.54.tar.gz'
www.apache.jp をDNSに問いあわせています... 210.188.224.37
www.apache.jp[210.188.224.37]:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 7,508,193 [application/x-gzip]
[==============================================>] 7,508,193    473.65K/s    ETA 00:00

13:25:36 (470.85 KB/s) - `httpd-2.0.54.tar.gz' を保存しました [7508193/7508193]

解凍

# tar zxf httpd-2.0.54.tar.gz 

インストール

# cd httpd-2.0.54
# ./configure --enable-so --enable-ssl=shared --enable-dav=shared --enable-dav-fs=shared --enable-rewrite=shared --with-ssl=/usr --with-mpm=worker

configureオプションは適宜行ってください。-h オプションで使えるオプションが表示されます。

--enable-so		DSOを有効にする
--enable-ssl=shared	sslをDSOとしてコンパイル
--enable-dav=shared	webdavをDSOとしてコンパイル
--with-ssl=/usr		vine3.1の場合/usrを指定
--with-mpm=worker	workerでコンパイル

DSO(Dynamic Shared Object)は、apache本体を軽くして必要があったときだけモジュールを読み込むようにします。

詳しくはこちら http://httpd.apache.org/docs/2.0/dso.html

# make
# make install

標準では、/usr/local/apache2 にインストールされます。アンインストールする場合はこのディレクトリを消すだけでいいらしい。

cgiスクリプトのためのシンボリックリンク

# ln -s /usr/bin/perl /usr/local/bin/perl

Document Root の作成

# mkdir -p /home/httpd/html

apacheを動作させるユーザの作成

# useradd -s /bin/false -d /dev/null apache2

設定ファイルの編集

# vi /usr/local/apache2/conf/httpd.conf
User apache2			←先ほど作成したユーザを記述
Group apache2

ServerAdmin [email protected]

ServerName example.com:80

HostnameLookups On				←ホスト名の逆引きを行う

DocumentRoot "/home/httpd/html"			←index.htmlを置くディレクトリ

<Directory "/home/httpd/html">

DirectoryIndex index.shtml index.html index.php	←〜/でアクセスした場合に表示するファイル

LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined	←414のアクセスを記録しない

SetEnvIf Remote_Addr 192.168.0. homelog nolog	←ローカルアドレスからのアクセスをhomelogに記録
SetEnvIf Remote_Addr 127.0.0.1 homelog nolog
SetEnvIf Request_URI "default.ida" wormlog nolog←ウイルスなどのアクセスをwormlogに記録
SetEnvIf Request_URI "NULL.IDA" wormlog nolog
SetEnvIf Request_URI "root.exe" wormlog nolog
SetEnvIf Request_URI "cmd.exe" wormlog nolog
SetEnvIf Request_URI "Admin.dll" wormlog nolog
SetEnvIf Request_URI "sumthin" wormlog nolog
CustomLog logs/home_log common env=homelog	←環境変数homelogをhome_logに記録
CustomLog logs/worm_log common env=wormlog	←環境変数wormlogをworm_logに記録
CustomLog logs/access_log combined env=!nolog	←環境変数nolog以外をaccess_logに記録

ScriptAlias /cgi-bin/ "/home/httpd/cgi-bin/"	←cgiスクリプトを置くディレクトリ

<Directory "/home/httpd/cgi-bin/">

AddHandler cgi-script .cgi			←cgiスクリプトを実行する

AddType text/html .shtml			←.shtmlでSSIを許可
AddOutputFilter INCLUDES .shtml			←.shtmlでSSIを許可

起動確認

# /usr/local/apache2/bin/apachectl start

とやれば起動します。

# /usr/local/apache2/bin/apachectl stop

とやれば終了します。

動作の確認

/home/httpd/html に適当にindex.htmlを置いて、クライアントよりhttp://[サーバーのIPアドレス]/でアクセスする。表示されればOK。

起動スクリプトの作成

/etc/rc.d/init.d/に起動スクリプトapache2を作ります。

tarボールには付属していないので、vineのrpmパッケージからパクってapache2へのパスを修正します。

# vi /etc/rc.d/ini.d/apache2
#!/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: apache2
# pidfile: /usr/local/apache2/logs/httpd.pid
# config: /usr/local/apache2/conf/httpd.conf

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

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

# 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/apache2/bin/apachectl
httpd=/usr/local/apache2/bin/httpd 
prog=httpd
RETVAL=0

# check for 1.3 configuration
check13 () {
	CONFFILE=/usr/local/apache2/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 /usr/share/doc/apache2-2.0.50/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
	if [ ! -f /usr/local/apache2/bin/httpd ] ; then
	  echo "test update alternatives"
	  /sbin/update-alternatives --auto apache2
	fi

        daemon $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /usr/local/apache2/logs/accept.lock
        return $RETVAL
}
stop() {
	echo -n $"Stopping $prog: "
	killproc $httpd
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f /usr/local/apache2/logs/accept.lock /usr/local/apache2/logs/httpd.pid
}
reload() {
	echo -n $"Reloading $prog: "
	check13 || exit 1
	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 /usr/local/apache2/logs/httpd.pid ] ; 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

保存したら、実行権限を与えます。

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

これで、

# /etc/rc.d/init.d/apache2 start

とやれば起動できます。

# /etc/rc.d/init.d/apache2 stop

とやれば停止できます。

自動起動するために、

# ntsysv

を実行して、apache2にチェックをいれ、ブート時に起動するようにします。

外部からアクセスできるように、ルータに穴を空ける

サーバマシンの80番ポートにアクセスできるように、ポートマッピング設定(ルータにより呼び名が異なる)を行う。

Tips

ログファイルのローテーション

このままだとログファイルが肥大化してディスクを圧迫するので定期的に分割、消去するようにします。logrotateを使ってログファイルを1週間毎に分割して、4週間以上経ったファイルは消去します。

apacha2という設定ファイルを作成します。

# vi /etc/logrotate.d/apache2
/usr/local/apache2/logs/*_log {
  postrotate
    /bin/kill -HUP `cat /usr/local/apache2/logs/httpd.pid 2> /dev/null` 2> /dev/null
  endscript
}

一行目でlogs/の*_logファイルをすべてローテーションさせる。
三行目の/bin/kill〜はログファイルの切り替えを行う。

確認

# /usr/sbin/logrotate /etc/logrotate.d/apache2

実行しても何も表示されないが、エラーが出なければ多分大丈夫。実際にローテーションされるのは1週間後(/etc/logrotate.confに設定した時間後)になる。

動作モードの確認

# /usr/local/apache2/bin/httpd -l

worker.c があればworkerで動いています。

コメント

コメントはありません。 Apache2/コメント?

お名前: