突然RaspberryPiのNginxが起動しなくてかなり焦ったのでメモしておきます。
起動しない
再起動すると起動しなくなっていた。
自動起動がうまく行かなかったのかと、
sudo service nginx start
するも起動しません。
検証
エラーは以下のとおりです。
Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
このメッセージは
‘systemctl status nginx.service’ と ‘journalctl -xn’ で詳細を調べろ!
ってことらしいです。
$ sudo systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
Active: failed (Result: exit-code) since 水 2017-08-02 22:58:37 JST; 18s ago
Process: 2756 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 654 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 2759 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Main PID: 657 (code=exited, status=0/SUCCESS)
8月 02 22:58:37 raspberrypi nginx[2759]: nginx: [alert] could not open error log file: open(...ry)
8月 02 22:58:37 raspberrypi nginx[2759]: 2017/08/02 22:58:37 [emerg] 2759#0: open() "/var/lo...ry)
8月 02 22:58:37 raspberrypi nginx[2759]: nginx: configuration file /etc/nginx/nginx.conf tes...led
8月 02 22:58:37 raspberrypi systemd[1]: nginx.service: control process exited, code=exited s...s=1
8月 02 22:58:37 raspberrypi systemd[1]: Failed to start A high performance web server and a ...er.
8月 02 22:58:37 raspberrypi systemd[1]: Unit nginx.service entered failed state.
Hint: Some lines were ellipsized, use -l to show in full.
途中、省略されちゃってます。
Hint: Some lines were ellipsized, use -l to show in full.
幾つかの行が省略されちゃってるから、-lを使えばいいじゃん?
てなわけで、リベンジ。
$ sudo systemctl -l status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
Active: failed (Result: exit-code) since 水 2017-08-02 22:58:37 JST; 2min 10s ago
Process: 2756 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 654 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 2759 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Main PID: 657 (code=exited, status=0/SUCCESS)
8月 02 22:58:37 raspberrypi nginx[2759]: nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
8月 02 22:58:37 raspberrypi nginx[2759]: 2017/08/02 22:58:37 [emerg] 2759#0: open() "/var/log/nginx/access.log" failed (2: No such file or directory)
8月 02 22:58:37 raspberrypi nginx[2759]: nginx: configuration file /etc/nginx/nginx.conf test failed
8月 02 22:58:37 raspberrypi systemd[1]: nginx.service: control process exited, code=exited status=1
8月 02 22:58:37 raspberrypi systemd[1]: Failed to start A high performance web server and a reverse proxy server.
8月 02 22:58:37 raspberrypi systemd[1]: Unit nginx.service entered failed state.
前半はよくわからない。
どんなサービスが動かなかったかの結論だけ言ってるみたいで、原因らしいものがないみたい。
後半の「could not open error log file: open() “/var/log/nginx/error.log” failed」がきになりました。
nginx.confのログファイルの場所はここじゃなくて別の場所に変更しているのに、なぜここが開かないというのかな・・・?
確かに、/var/log/nginx/のディレクトリはないけれど・・・。
そこで、ふには落ちないけど、mkdir -p?/var/log/nginx をしてから sudo service nginx start して起動してみました。やはりだめ?
ああ、パーミッションかな?ととりあえず chmod 777 でユルユルにしてもう一度起動すると・・・!起動したーー!
結局/var/log/nginx/error.logという0ファイルができました。どうやらここは設定ファイルで指定しなくても必要なようですね・・・?バグだと思うけどね。
こうなった原因
原因はswapをRamdiskに変えたことでした。
microSDの突然死がありましたので、以下を見つけて設定したのです。
Raspberry Pi、RAMディスクで micro SDを長寿命化
rc.localに更に以下を追加しました。
mkdir -p /var/log/nginx
chown root.www-data /var/log/nginx
これで、再起動してもちゃんと起動するようになりました。
今回はなんとかなりましたけど、やっぱり再起動する前にはバックアップしないと危険ですね。