1. はじめに
WSL の Ubuntu に apache2 パッケージを入れているのですが、いつの間にか動かなくなっていました。
これを直したときのメモです。
2. 環境
- Windows 10 Pro 22H2
- OS ビルド:19045.2311
- Ubuntu (WSL 2)
3. エラーの内容
apache2 パッケージの状態を見てみると、エラーが発生していることが分かります。
$ sudo systemctl status apache2
× apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2022-11-25 19:22:52 JST; 8min ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 422 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)
11月 25 19:22:52 PC-FOO systemd[1]: Starting The Apache HTTP Server...
11月 25 19:22:52 PC-FOO apachectl[459]: apache2: Syntax error on line 146 of /etc/apache2/apache2.c>
11月 25 19:22:52 PC-FOO apachectl[422]: Action 'start' failed.
11月 25 19:22:52 PC-FOO apachectl[422]: The Apache error log may have more information.
11月 25 19:22:52 PC-FOO systemd[1]: apache2.service: Control process exited, code=exited, status=1/>
11月 25 19:22:52 PC-FOO systemd[1]: apache2.service: Failed with result 'exit-code'.
11月 25 19:22:52 PC-FOO systemd[1]: Failed to start The Apache HTTP Server.
- 分かりやすくするため、エラーの部分に色をつけました。
4. 原因
どうやら、PHPのバージョンに問題があるようです。
apache2 には PHP 7.4 用の設定が入っているのに、このシステムに PHP 7.4 はインストールされていません。代わりに PHP 8.1 が入っています。
5. 修正作業
apache2 に、PHP 8.1 用の設定を追加します。
まず、apache2 の PHP 7.4用設定を無効にするために、以下のコマンドを実行します。
$ sudo a2dismod php7.4
apache2 に PHP 8.1 用の設定を追加するために、以下のコマンドを実行します。
$ sudo a2enmod php8.1
apache2 を再起動します。
$ sudo systemctl restart apache2
設定ファイルの文法チェックを行います。
$ sudo apachectl configtest
apache2 の状態を確認します。
$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-11-25 19:34:55 JST; 42s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 1932 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1936 (apache2)
Tasks: 6 (limit: 7039)
Memory: 16.9M
CGroup: /system.slice/apache2.service
├─1936 /usr/sbin/apache2 -k start
├─1939 /usr/sbin/apache2 -k start
├─1941 /usr/sbin/apache2 -k start
├─1942 /usr/sbin/apache2 -k start
├─1943 /usr/sbin/apache2 -k start
└─1944 /usr/sbin/apache2 -k start
11月 25 19:34:55 PC-FOO systemd[1]: Starting The Apache HTTP Server...
11月 25 19:34:55 PC-FOO systemd[1]: Started The Apache HTTP Server.
Active のところが「active (running)」になっているので、ちゃんと動いているようです。
一つ気になるのが、自動起動が有効 (enabled) になっている部分です。たまにしか使わないので、自動起動は無効にします。
$ sudo systemctl disable apache2
以上で対応作業は終わりです。