Apache勉強

教材:日経Linux 2008/5連載 実践!Webサーバー構築 初めてのApacheモジュール

『まず、Apacheにどんなモジュールが組み込まれているか、見てみましょう』『・・・「httpd」コマンドを「-M」オプション付きで実行してみます』とあるので会社で使っているWebサーバーで実行してみる。



# cd /usr/local/apache2/bin/
# ./httpd -M
httpd: illegal option -- M
Usage: ./httpd [-D name] [-d directory] [-f file]
[-C "directive"] [-c "directive"]
[-k start|restart|graceful|stop]
[-v] [-V] [-h] [-l] [-L] [-t] [-S]
Options:
-D name : define a name for use in directives
-d directory : specify an alternate initial ServerRoot
-f file : specify an alternate ServerConfigFile
-C "directive" : process directive before reading config files
-c "directive" : process directive after reading config files
-e level : show startup errors of level (see LogLevel)
-E file : log startup errors to file
-v : show version number
-V : show compile settings
-h : list available command line options (this page)
-l : list compiled in modules
-L : list available configuration directives
-t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings)
-S : a synonym for -t -D DUMP_VHOSTS
-t : run syntax check for config files

いきなりつまずく。「-M」オプションが存在しない
パッケージとソースからインストールしたものでは違うのか?またはバージョンの違いか?
helpメッセージではオプションは「-l」のようなのでこっちのオプションで再度実行



# ./httpd -l
Compiled in modules:
core.c
mod_access.c
mod_auth.c
mod_auth_anon.c
mod_auth_dbm.c
mod_auth_digest.c
mod_ext_filter.c
mod_include.c
mod_log_config.c
mod_log_forensic.c
mod_env.c
mod_mime_magic.c
mod_cern_meta.c
mod_expires.c
mod_headers.c
mod_usertrack.c
mod_unique_id.c
mod_setenvif.c
mod_version.c
mod_ssl.c
prefork.c
http_core.c
mod_mime.c
mod_dav.c
mod_status.c
mod_autoindex.c
mod_asis.c
mod_info.c
mod_cgi.c
mod_dav_fs.c
mod_vhost_alias.c
mod_negotiation.c
mod_dir.c
mod_imap.c
mod_actions.c
mod_speling.c
mod_userdir.c
mod_alias.c
mod_rewrite.c
mod_so.c

記事では静的に組み込まれるモジュールか、動的(DSO)かの違いで『static』か『shared』が表示されるようだが表示されない
※後で分かったが「-l」オプションは静的モジュールだけを表示

<静的組み込み>
メリット・・・実行時の負荷が低い
デメリット・・・モジュールの追加・削除するたびにApacheの再インストールが必要

<動的組み込み(DSO)>
メリット・・・設定ファイルの修正とApacheの再起動で不要なモジュールを切り離せる
デメリット・・・実行時にモジュールを読み込むため負荷がかかる

139P ※2
MPM(マルチプロセッシングモジュール)の種類
prefork MPM・・・Linuxディストリの標準、あらかじめhttpd子プロセスをいくつか生成
worker MPM・・・マルチプロセスに加え、マルチスレッドに対応
event MPM・・・より効率的にマルチスレッド処理可能、スケーラビリティが高くなるように拡張

「mod_info」を導入
DSOでインストール

既に静的に組み込まれているが強引にDSOでインストール



# cd /usr/local/src/httpd-2.0.63/modules/generators/
# /usr/local/apache2/bin/apxs -i -a -c mod_info.c
 ・
chmod 755 /usr/local/apache2/modules/mod_info.so
[activating module `info' in /usr/local/apache2/conf/httpd.conf]


apxsコマンドオプション
「-i」・・・ビルド済みモジュールをモジュールディレクトリにコピー
「-a」・・・httpd.confにモジュールをロードする1行を追加
「-c」・・・ソースファイルを指定

httpd.confファイルを編集



LoadModule info_module modules/mod_info.so
#「-i」オプションで追加されたと思われる
#


SetHandler server-info
Order deny,allow
Deny from all
Allow from 127.0.0.1 192.168.1.2


サーバーを再起動



# /usr/local/apache2/bin/apachectl restart
Syntax error on line 233 of /usr/local/apache2/conf/httpd.conf:
module info_module is built-in and can't be loaded

やっぱり既に静的に組み込まれているのでエラー
ロードしている部分をコメントにして再起動

http://サーバー/server-info にアクセスしてみる


別のあいているサーバーで記事と同じバージョン(2.2系)をインストールしてみる
デフォルトでモジュールは静的に組み込まれる
DSOを利用するにはconfigure時に「--enable-module=all --enable-mods-shared=all」オプションを付加



# tar xvjf httpd-2.2.15.tar.bz2
# cd httpd-2.2.15
# ./configure --prefix=/usr/local/apache2 --enable-module=all --enable-mods-shared=all
# make
# make install

httpd.confを修正
ServerName IPアドレス:80

httpd.conf構文チェック



# /usr/local/apache2/bin/apachectl -t
Syntax OK

起動



# /usr/local/apache2/bin/apachectl -k start
停止


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

Apache2.2では処理中のリクエストの完結を待って再起動や停止をする「graceful-○○」が利用可能とのこと
graceful再起動



# /usr/local/apache2/bin/apachectl -k graceful

graceful停止



# /usr/local/apache2/bin/apachectl -k graceful-stop

先の環境ではダメだったhttpd -Mを試してみる



# /usr/local/apache2/bin/httpd -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
authn_file_module (shared)
authn_dbm_module (shared)
authn_anon_module (shared)
authn_dbd_module (shared)
authn_default_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
authz_dbm_module (shared)
authz_owner_module (shared)
authz_default_module (shared)
auth_basic_module (shared)
auth_digest_module (shared)
dbd_module (shared)
dumpio_module (shared)
reqtimeout_module (shared)
ext_filter_module (shared)
include_module (shared)
filter_module (shared)
substitute_module (shared)
deflate_module (shared)
log_config_module (shared)
log_forensic_module (shared)
logio_module (shared)
env_module (shared)
mime_magic_module (shared)
cern_meta_module (shared)
expires_module (shared)
headers_module (shared)
ident_module (shared)
usertrack_module (shared)
unique_id_module (shared)
setenvif_module (shared)
version_module (shared)
mime_module (shared)
dav_module (shared)
status_module (shared)
autoindex_module (shared)
asis_module (shared)
info_module (shared)
cgi_module (shared)
dav_fs_module (shared)
vhost_alias_module (shared)
negotiation_module (shared)
dir_module (shared)
imagemap_module (shared)
actions_module (shared)
speling_module (shared)
userdir_module (shared)
alias_module (shared)
rewrite_module (shared)
Syntax OK
できた。

/etc/init.d以下に置く起動スクリプトがソースの中にあり、それをコピーして使えるとのこと



# cp /usr/local/src/httpd-2.2.15/build/rpm/httpd.init /etc/init.d/httpd
# chkconfig --add httpd
# chkconfig --level 35 httpd on
# chkconfig --list httpd
httpd 0:off 1:off 2:off 3:on 4:off 5:on 6:off

スクリプトから起動してみる



# /etc/init.d/httpd start
httpd を起動中: grep: /etc/httpd/conf/httpd.conf: そのようなファイルやディレクトリはありません
/bin/bash: /usr/sbin/httpd: No such file or directory
[失敗]

パスが全く違う
httpdを修正



# diff /etc/init.d/httpd /usr/local/src/httpd-2.2.15/build/rpm/httpd.init
56c56< httpd=${HTTPD-/usr/local/apache2/bin/httpd}

    • -

> httpd=${HTTPD-/usr/sbin/httpd}
64c64< CONFFILE=/usr/local/apache2/conf/httpd.conf

    • -

> CONFFILE=/etc/httpd/conf/httpd.conf

起動はできるが停止できない

pidfileの場所も変更する



# diff /etc/init.d/httpd /usr/local/src/httpd-2.2.15/build/rpm/httpd.init
25c25< # pidfile: /usr/local/apache2/logs/httpd.pid

    • -

> # pidfile: /var/log/httpd/httpd.pid
52c52< # Set HTTPD=/usr/local/apache2/bin/httpd.worker in /etc/sysconfig/httpd to use a server

    • -

> # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
56c56< httpd=${HTTPD-/usr/local/apache2/bin/httpd}

    • -

> httpd=${HTTPD-/usr/sbin/httpd}
58c58< pidfile=${PIDFILE-/usr/local/apache2/logs/httpd.pid}

    • -

> pidfile=${PIDFILE-/var/log/httpd/httpd.pid}
64c64< CONFFILE=/usr/local/apache2/conf/httpd.conf

    • -

> CONFFILE=/etc/httpd/conf/httpd.conf

起動できた