Apacheモジュールの基礎知識

出典
Software Design 2007年 09月号
テスト環境
CentOS5 テスト用最小インストール手順参照。
apacheバージョン
2.2系

Apacheモジュールの基本

  • Apache の機能は
    • コアで基本機能を提供。
    • モジュールで付加機能を提供。
  • モジュールが提供するのは、、、
    • ユーザ認証
    • アクセス制限
    • ユーザディレクトリ機能
    • HTTPSの実装
    • サーバのステータス情報
    • Proxy機能
    • ログ出力形式の指定機能
    • などなど
  • モジュールのメリット
    • サイトに併せて必要な機能を柔軟に組み込める。
    • 設定ファイルで機能の有効/無効を切り替えることが出来る。
    • デフォルトでも多くのモジュールが組み込まれている。⇒Apache2.2の標準モジュール一覧1

静的組み込みと動的組み込み(DSO)

  • モジュールを組み込む方法は以下。
    • 静的組み込み ・・・ モジュール内部に組み込む。
    • 動的組み込み(DSO)・・・ モジュール起動時に読み込む。
  • それぞれのメリット/デメリットは以下。
    • 静的組み込み
      • 起動時のコストが低い。
      • パフォーマンスが比較的高い。
      • 新しくモジュールを組み込む/不要なモジュールを切り離す場合は再ビルドが必要。
      • 使っていないモジュールがあってもその分メモリを消費する。
    • 動的組み込み
      • 新しくモジュールを組み込む場合はモジュールだけビルドして設定に追加すればよい。
      • 不要なモジュールを切り離す場合も設定から削除するだけで良い。
      • 起動時にモジュールを読み込むのでオーバーヘッドが大きい。

静的組み込みモジュールの確認

CentOS5の標準パッケージを使ってみる。

# yum install httpd

組み込まれたモジュール一覧を見るにはhttpd -l。

# httpd -l
Compiled in modules:
 core.c
 prefork.c
 http_core.c
 mod_so.c

動的組み込みモジュールの確認

httpd -l は、静的に組み込まれたものしか表示されない。動的に組み込まれたものも含めて全てのモジュール一覧を見るには-Mオプション。2

# httpd -M
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 auth_basic_module (shared)
 auth_digest_module (shared)
 authn_file_module (shared)
 authn_alias_module (shared)
 authn_anon_module (shared)
 authn_dbm_module (shared)
 authn_default_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 authz_owner_module (shared)
 authz_groupfile_module (shared)
 authz_dbm_module (shared)
 authz_default_module (shared)
 ldap_module (shared)
 authnz_ldap_module (shared)
 include_module (shared)
 log_config_module (shared)
 logio_module (shared)
 env_module (shared)
 ext_filter_module (shared)
 mime_magic_module (shared)
 expires_module (shared)
 deflate_module (shared)
 headers_module (shared)
 usertrack_module (shared)
 setenvif_module (shared)
 mime_module (shared)
 dav_module (shared)
 status_module (shared)
 autoindex_module (shared)
 info_module (shared)
 dav_fs_module (shared)
 vhost_alias_module (shared)
 negotiation_module (shared)
 dir_module (shared)
 actions_module (shared)
 speling_module (shared)
 userdir_module (shared)
 alias_module (shared)
 rewrite_module (shared)
 proxy_module (shared)
 proxy_balancer_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_connect_module (shared)
 cache_module (shared)
 suexec_module (shared)
 disk_cache_module (shared)
 file_cache_module (shared)
 mem_cache_module (shared)
 cgi_module (shared)
 proxy_ajp_module (shared)
Syntax OK

CentOS5の標準パッケージでは、多くのモジュールが動的(shared)で組み込まれているようになっているのがよくわかる。3なお、動的モジュールの場合、有効/無効は設定ファイルで行われるので設定ファイルのチェックも行われる様子(最終行)


モジュールの追加・削除

静的なモジュール追加・削除

apache-2.2.4をソースからビルド。ビルドする場合はgccをインストールしておくこと。

# yum install gcc
# cd /usr/local/src
# wget http://ftp.kddilabs.jp/infosystems/apache/httpd/httpd-2.2.4.tar.gz
# tar zxvf httpd-2.2.4.tar.gz
# cd httpd-2.2.4
# ./configure 
# make
# make install

組み込まれたモジュールを確認

# /usr/local/apache2/bin/httpd -l
Compiled in modules:
  core.c
  mod_authn_file.c
  mod_authn_default.c
  mod_authz_host.c
  mod_authz_groupfile.c
  mod_authz_user.c
  mod_authz_default.c
  mod_auth_basic.c
  mod_include.c
  mod_filter.c
  mod_log_config.c
  mod_env.c
  mod_setenvif.c
  prefork.c
  http_core.c
  mod_mime.c
  mod_status.c
  mod_autoindex.c
  mod_asis.c
  mod_cgi.c
  mod_negotiation.c
  mod_dir.c
  mod_actions.c
  mod_userdir.c
  mod_alias.c
  mod_so.c

標準でも結構組み込まれているのがわかる。
ただし、標準モジュールといっても組み込みも標準というわけではないもの(無効化されているもの)があるのでそれを使ってみる。標準では組み込まれないものは、

# ./configure --enable-機能名

を指定してビルドする。mod_deflateの場合は、

# yum install zlib-devel
# ./configure --enable-deflate
# make
# make install
#  /usr/local/apache2/bin/httpd -l
・・・
   mod_deflate.c
・・・

となる。4 逆に標準で組み込まれるものを外すには、

# ./configure --disable-機能名

動的なモジュール追加・削除

ソースからビルドが完了している状態から、動的にモジュールを組み込んでみる。

まず以下のモジュールが組み込まれていることを確認。5

# /usr/local/apache2/bin/httpd -l
Compiled in modules:
・・・
  mod_so.c
・・・

標準モジュールだけど無効化されているinfo_moduleをapxsコマンドで組み込む。ソースディレクトリに移動。

# cd /usr/local/src/httpd-2.2.4

info_moduleのソースは modules/generators/mod_info.c。これをapxsコマンドの-cオプションで指定してビルド。

# /usr/local/apache2/bin/apxs -i -a -c modules/generators/mod_info.c 

なお、オプションは以下。

-i
モジュールをapacheツリーの所定の位置にインストール
-a::

モジュールを有効にする為にhttpd.confに"LoadModule"ディレクティブを追加する

実行結果は以下。

/usr/local/apache2/build/libtool --silent --mode=compile gcc -prefer-pic
 -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -g -O2 -pthread
 -I/usr/local/apache2/include  -I/usr/local/apache2/include
 -I/usr/local/apache2/include   -c -o modules/generators/mod_info.lo
 modules/generators/mod_info.c && touch modules/generators/mod_info.slo
/usr/local/apache2/build/libtool --silent --mode=link gcc -o
 modules/generators/mod_info.la  -rpath /usr/local/apache2/modules -module
 -avoid-version    modules/generators/mod_info.lo
/usr/local/apache2/build/instdso.sh
 SH_LIBTOOL='/usr/local/apache2/build/libtool'
 modules/generators/mod_info.la /usr/local/apache2/modules
/usr/local/apache2/build/libtool --mode=install cp
 modules/generators/mod_info.la /usr/local/apache2/modules/
cp modules/generators/.libs/mod_info.so /usr/local/apache2/modules/mod_info.so
cp modules/generators/.libs/mod_info.lai /usr/local/apache2/modules/mod_info.la
cp modules/generators/.libs/mod_info.a /usr/local/apache2/modules/mod_info.a
ranlib /usr/local/apache2/modules/mod_info.a
chmod 644 /usr/local/apache2/modules/mod_info.a
PATH="$PATH:/sbin" ldconfig -n /usr/local/apache2/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/apache2/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/local/apache2/modules/mod_info.so
[activating module `info' in /usr/local/apache2/conf/httpd.conf]

モジュールが/usr/local/apache2/modulesにコピーされ、httpd.confが変更されたとのこと。確認してみると、

# ls -lt /usr/local/apache2/modules/
合計 52
-rwxr-xr-x 1 root root 40204  9月  8 12:11 mod_info.so
-rw-r--r-- 1 root root  8951  9月  7 18:33 httpd.exp
# grep -i info /usr/local/apache2/conf/httpd.conf
・・・
LoadModule info_module        modules/mod_info.so
・・・

なお、元のhttpd.confはhttpd.conf.bakとしてバックアップされる。
ではhttpd -M で確認。

# /usr/local/apache2/bin/httpd -l
・・・
info_module (shared)
・・・

apacheを起動してブラウザから確認する。httpd.confに以下を追加。

<IfModule mod_info.c>
  <Location /server-info>
    SetHandler server-info
  </Location>
</IfModule> 

apacheを起動。

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

http://サーバのIPアドレス/server-info/ にアクセス。こんな画面が出る。 mod_info


その他

2.2で気になった点は以下。

  • MPMにeventが追加
    ただしまだexperimentalっぽい。6
  • 認証モジュール見直し
    一般的な認証と同じように、Authentication(認証)、Auhorization(承認)の各機能毎にモジュールが分かれた。
    • mod_auth_○○ ・・・ 認証方法を提供するモジュール。
      ex) mod_auth_basic(BASIC認証モジュール)
    • mod_authn_○○ ・・・ 認証バックエンドをサポートするモジュール。
      ex) mod_authn_file(.htpasswdファイルによるパスワード認証モジュール)
    • mod_authz_○○ ・・・ 承認(アクセス制御)を提供するモジュール
    • mod_authnz_○○ ・・・認証と承認を両方提供するモジュール。
  • ドキュメントキャッシュ機能
  • ディスクキャッシュ、メモリキャッシュ
  • Proxy ・・・ mod_proxy_balancerで負荷分散
  • コンテンツフィルタ機能 ・・・ mod_filter


  1. 1. ただしこれら全てが標準で「動作するようになっているか」は別。
  2. 2. 2.2以降のみ?
  3. 3. パッケージシステムでは当然といえば当然
  4. 4. ちなみに、mod_deflate の場合、zlib-develも必要なので最初にyumでインストールしている。
  5. 5. 2.2系では標準で組み込まれる様子。記憶では2.0系では標準では組み込まれなかったような。。。
  6. 6. MPMはどれか一つを静的に組み込むしかできない。(configure --with-mpm=○○○)

添付ファイル