Thursday, January 17, 2013

Apache 2.4 系をソースからインストールします

Debian GNU/Linux 6.0 (Squeeze) で提供される Apache HTTP Server の安定版パッケージ apache2 は, 現在のところ 2.2 系です. 2.4 系を利用したい場合は, ソースからビルドしてコンパイルする必要があります. わたし自身は, ふだんはパッケージを利用していたのですが, ふと 2.4 系を試してみたくなりました. やってみると, それほど難しくもない気がしましたので, ここにメモを残しておきたいと思います.

APR と APR-Util

まず Download - The Apache HTTP Server Project から, 現時点で最新の安定版のソースをダウンロードします. httpd-2.4.3.tar.gz です. 適当な場所に展開したら, configure スクリプトを実行します.

~/httpd-2.4.3$ ./configure --prefix=/opt/httpd-2.4.3
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... no
configure: error: APR not found.  Please read the documentation.
~/httpd-2.4.3$ 

思ったよりも早くエラーとなりました. APR が必要だよ, とのことです. オンラインドキュメントの Compiling and Installing を読みますと, けっこう冒頭のところに, 次のような前提条件が書かれていました.

APR and APR-Util
Make sure you have APR and APR-Util already installed on your system. If you don't, or prefer to not use the system-provided versions, download the latest versions of both APR and APR-Util from Apache APR, unpack them into ./srclib/apr and ./srclib/apr-util (be sure the domain names do not have version numbers; for example, the APR distribution must be under ./srclib/apr/) and use ./configure's --with-included-apr option. On some platforms, you may have to install the corresponding -dev packages to allow httpd to build against your installed copy of APR and APR-Util.

2.4 系では, APR と APR-Util は別途用意しなければならなくなったのですね. とはいえ, 別途ビルドしてインストールしておかなければならない, というほどでもないようです. というのも, APR と APR-Util のソースさえ用意しておけば, Apache HTTP Server のビルドの際に, ついでに一緒にビルドしてもらうことが可能だからです.

Download - The Apache Portable Runtime Project から, 現時点で最新の安定版のソース apr-1.4.6.tar.gzapr-util-1.5.1.tar.gz をダウンロードし, 適当な場所に展開します. そのうえで, それらを特定のディレクトリにコピーします.

~$ cp -Rp ~/apr-1.4.6 ~/httpd-2.4.3/srclib/apr
~$ cp -Rp ~/apr-util-1.5.1 ~/httpd-2.4.3/srclib/apr-util

コピーする際に, それぞれのディレクトリ名を aprapr-util に変更しておくことが, ここでは必要です.

なお, 上に引用した説明には, --with-included-apr オプションが必要であるように書かれていますが, 事前に APR も APR-Util もインストールされていない環境では, 自動的に srclib ディレクトリを見に行ってくれるか何かしてくれて, ひょっとしたら不要なのではないかなという気がしています. 少なくとも, わたしの手元の環境では, そのオプションは不要でした.

ためしに, --with-included-apr オプションを使ったときの標準出力は,

checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
configuring package in srclib/apr now
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
Configuring APR library

/* 後略 */

となっていましたが, --with-included-apr オプションを使わなかったときの標準出力は,

checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... reconfig
configuring package in srclib/apr now
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
Configuring APR library

/* 後略 */

となっていました. 以後の出力結果を見比べても, 両者の違いは以下の一文があるかないかだけです.

checking for APR... reconfig

しかも, この部分は, どう見ても, 自動的に APR を探しに行きまっせ, という宣言であるようにしか思えないのです. なので, あくまで想像の域を出ていませんが, すでに古い APR や古い APR-Util がインストールされているような環境でないかぎり, --with-included-apr オプションはなくても構わないのではないか, というふうに思うのです.

PCRE

再び configure スクリプトを実行してみます.

~/httpd-2.4.3$ ./configure --prefix=/opt/httpd-2.4.3

/* 中略 */

checking for pcre-config... false
configure: error: pcre-config for libpcre not found. PCRE is required and 
available from http://pcre.org/
~/httpd-2.4.3$ 

今度は, PCRE (Perl Compatible Regular Expressions) が必要だよ, とのことです.

Debian Squeeze の安定版パッケージでは, libpcre3libpcre3-dev もバージョン 8.02 です. それに対して, PCRE 自体の現時点での最新版はバージョン 8.32 です. 若干離れているような気がしなくもないですが, そもそもその違いがどれだけ意味のあることなのか, よくわかっていなかったりします. ランタイムライブラリのようなものはパッケージに任せてしまってもいいかなと思いまして, 今回は次のように済ませることにしました.

~/httpd-2.4.3$ sudo apt-get install libpcre3 libpcre3-dev

(ちなみに, 蛇足になりますが, もし PCRE をソースからビルドしてインストールするのであれば, C++ コンパイラ (g++ とか) が必要です. また, インストール後は, (たぶん) ldconfig を実行することを忘れないようにしなければなりません.)

モジュール

Apache HTTP Server で利用するモジュールや, 特に MPM (Multi-Processing Module) などは, ビルドの段階でそれを明示しておかないと, あとで利用したいときに再びビルドとインストールをおこなわなければならなくなる... という印象を持っていました. が, わたしの勘違いだったのか, 遠い昔の話だったのか, 少なくとも 2.4 系ではそうでもなく, DSO (Dynamic Shared Object) として取り扱えるようにしておきさえすれば, いつでも有効にしたり無効にしたりすることができるようになっているようです. まあ, いつでも, というのは言いすぎですが, 起動時の設定ファイルで LoadModule ディレクティブで指定しておけば, けっこう柔軟に何とかなりそうであります.

その場合, configure スクリプトには, --enable-mods-shared--enable-mpms-shared といったオプションをつけておくのがよいと思います. これに関連したことは, 昨日の記事 Apache 2.4 系をソースからインストールする際の DSO に関して でも触れましたので, 本日は割愛します.

三度目の正直

Apache HTTP Server のオンラインドキュメント Compiling and Installing には, Perl 5 のインストールもしておいたほうがいいよ, と書かれています. それは, apxsdbmmanage といったユーティリティツールが Perl で書かれたスクリプトになっているので, そのためなのだと思います. が, 後でもできることなので, ここではスキップすることにします.

~/httpd-2.4.3$ ./configure --prefix=/opt/httpd-2.4.3
~/httpd-2.4.3$ make
~/httpd-2.4.3$ sudo make install

三度目の正直で, 今度は特にエラーもなく, 無事に /opt/httpd-2.4.3 配下に Apache HTTP Server がインストールされました.

起動

インストール直後の Apache HTTP Server の設定ファイル conf/httpd.conf には, 次のような記述があります.

<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User daemon
Group daemon

</IfModule>

初期状態では, daemon ユーザーで稼動させることになります. しかし, このユーザーは, もしかしたらほかの何かのためにも使用するかもしれませんし, できれば Apache HTTP Server にはそれ専用のユーザー / グループを割り当てたい気もします. そこで, httpd という名前のユーザー / グループを作ることにしてみます.

~/httpd-2.4.3$ sudo groupadd -g 501 httpd
~/httpd-2.4.3$ sudo useradd -M -u 501 -g httpd -s /bin/false httpd

そして, conf/httpd.conf の該当箇所は, 次のように変更します.

User httpd
Group httpd

いよいよ起動します.

~/httpd-2.4.3$ sudo /opt/httpd-2.4.3/bin/apachectl -k start

ブラウザからアクセスしてみると, たしかに "It works!" と表示されます.

まとめ

以上です. おしまい

No comments:

Post a Comment