Vagrant で Apache が自動起動しない場合の対処法
はじめに
Vagrant + VirtualBox で Apache を動かすと、 DocumentRoot is not a directory, or is not readable で自動起動せずに強制終了してしまい、手動でのサービス再起動が必要になってしまいます。Apache を強制終了しないようにする方法を紹介します。
強制終了してしまう
仕事で Laravel を使った Web サイトを VirtualBox で構築(指定されたので...)していたところ、 vagrant up しても Apache が自動起動せずに強制終了してしまう現象に遭遇しました。
ログには下記のように残っています。
Main PID: 1021 (code=exited, status=1/FAILURE)
systemd[1]: Starting The Apache HTTP Server...
httpd[1021]: AH00526: Syntax error on line 5 of /etc/httpd/conf/httpd.conf:
httpd[1021]: DocumentRoot '/var/www/public' is not a directory, or is not readable
systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
kill[1455]: kill: cannot find process ""
systemd[1]: httpd.service: control process exited, code=exited status=1
systemd[1]: Failed to start The Apache HTTP Server.
systemd[1]: Unit httpd.service entered failed state.
systemd[1]: httpd.service failed.
Apache から、DocumentRoot が読めないというエラーになっています。
DocumentRoot '/var/www/public' is not a directory, or is not readable
しかし、この後にサービスの再起動をすると強制終了せずに動作します。
原因
原因は、Vagrant によるマウントが完了する前に Apache が起動してしまい、DocumentRoot のシンボリックリンクが読めないため発生しています。
対処方法
かなり対処療法ですが、VagrantFile によるマウントの記述の後に Apache を再起動することで対処します。
CentOS 7 系の場合はマウントの記述の後に下記を追加します。
config.vm.provision :shell, run: "always", :inline => <<-EOT
sudo systemctl restart httpd.service
EOT
その他は下記を追加します。
config.vm.provision :shell, run: "always", :inline => <<-EOT
sudo service httpd restart
EOT
以上で、マウント後に Apache が自動再起動されます。
ちなみに、vagrant up 時に Apache の再起動をした際のログが下記のように残ります。
==> default: Running provisioner: shell...
default: Running: inline script