Apache2 mod_rewrite と AllowOverride

仮想マシン上の WordPress でパーマリンクをデフォルトの (?p=番号) 形式以外にすると動かない件、考えてみるに、

  • Apache mod_rewrite

が入っていないのかもしれません。

仮想マシンは Ubuntu 10.04 desktop に tasksel で Apache、PHP、MySQL を入れたもので、WordPress の動作要件を調べていませんでした。

Apache の mod_rewrite が入っているかどうかの調べ方ですが、ターミナルで httpd -l としたら、そんなコマンドはありませんと言われてしまいました。

$ ps -e

でプロセスを調べたら、 apache2 という名前でした。

$ apache2 -l

Compiled in modules:
core.c
mod_log_config.c
mod_logio.c
prefork.c
http_core.c
mod_so.c

ということで、静的には mod_rewrite が入っていません。しかし、 mod_so があるので動的にロードしているかもしれません。

動的なモジュールも表示してくれる apache2 -M は受け付けてくれませんでした。どうやら、 apache2ctl というコマンドを使うようです。

$ apache2ctl -v  (バージョン)
$ apache2ctl -V (バージョン、環境)
$ apache2ctl -l  (静的モジュール)
$ apache2ctl -M  (静的・動的モジュール)

どれも実行可能でした。

$ apache2ctl -v
Server version: Apache/2.2.14 (Ubuntu)
Server built:   Sep 28 2010 12:52:38

Apache2 のバージョンは 2.2.14 ということです。

「動的共有オブジェクト (DSO) サポート」というそうですが、今動いているモジュールを調べると、

$ apache2ctl -M
apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
Loaded Modules:
core_module (static)
log_config_module (static)
logio_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
alias_module (shared)
auth_basic_module (shared)
authn_file_module (shared)
authz_default_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
mime_module (shared)
negotiation_module (shared)
php5_module (shared)
reqtimeout_module (shared)
setenvif_module (shared)
status_module (shared)
Syntax OK

残念ながら mod_rewrite は入っていないようです。

それで、モジュールをどうやって有効にするのか。

Google 「ubuntu apache2 mod_rewrite」

と検索したら、

$ sudo a2enmod rewrite

とするようです。man a2enmod で調べると、逆は a2dismod だそうです。実行すると、

$ sudo a2enmod rewrite
[sudo] password for … :
Enabling module rewrite.
Run ‘/etc/init.d/apache2 restart’ to activate new configuration!

ということで、apache2 を再起動です。

$ sudo /etc/init.d/apache2 restart
$ apache2ctl -M

rewrite_module (shared)

mod_rewrite を有効にして再起動に成功したらしいので、WordPress を動かしてみます。

まだパーマリンクがデフォルトの (?p=数字) 形式しか動きません。

よく見ると、404 Not Found のページが WordPress ではなく Apache/2.2.14 から報告されています。これはどうやら Apache の設定をいじらなければならないような雰囲気です。WordPress が動き始めたときも、ルートの index.php が有効にならずに index.html が優先されたままだったので、おかしいなとは思っていました。 これは、.htaccess が有効になっていません。

などで検索。

これらを参考に、Apache の設定ファイル 「/etc/apache2/sites-available/default」 を sudo gedit で開き、AllowOverride のところを None から All へ変更。

<Directory /var/www/>
	Options Indexes FollowSymLinks MultiViews
#	AllowOverride None
	AllowOverride All
	Order allow,deny
	allow from all
</Directory>

それから、apache2 を再起動

$ sudo /etc/init.d/apache2 restart

これでルートディレクトリの .htaccess が有効になり、WordPress が生成する動的なページ群が有効になりました。日付 + スラッグ のページ指定できちんと見えます。

(2010.11.13)

投稿者:

librarian

http://makisima.org/ の librarian です。マキシマ文庫=書斎 の主。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です