環境とか状況
- CentOS7
-
nginx(v1.15.8)
- リバースプロキシで待受、アクセス振り分け
- port:443
- リバースプロキシで待受、アクセス振り分け
-
httpd(2.4.35)
-
wordpress
とかdjango
とか動かしている。アプリごとに違うポートで待受している- port:8443
- port:8444
-
原因
nginx
がhttpd
より先に起動すると、当然まだhttpd
側のポートが開放されていないので、nginx
が死ぬ!!
治す!!
自動起動の設定ファイルは以下の2つのディレクトリに置いてあります。
/usr/lib/systemd/system/
/etc/systemd/system/
注意: 追加、編集する際は/etc/systemd/system
を編集するようにしましょう。
-
/usr/lib/systemd/system/
はsystemctl
が作成したり書き換える場所なので、いじっちゃ駄目です。 -
/etc/systemd/system/
は、サーバー管理者がいじっても平気なファイルたちです。
両方のディレクトリに同じファイルが存在する場合、/etc/systemd/system/
内が優先されます。
nginx.serviceの作成
# nginxをyumでインストールした場合、雛形が置いてあるのでコピーしてくる
sudo cp /usr/lib/systemd/system/nginx.service /etc/systemd/system/
sudo vim /etc/systemd/system/nginx.service
ほとんどそのままなのですが、After=
というパラメータにhttpd.serviceを追加することでnginx
がhttpd
の起動後に立ち上がるようにしてくれます。ついでに、Requires=
パラメータを追加しています。httpd.service
が起動していないなら、nginx.service
の起動はしませんよって意味です。
nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target httpd.service
Wants=network-online.target
Requires=httpd.service
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
最後にsystemctl
にunit
ファイルを更新したよ~っていう通知を行います。
sudo systemctl daemon-reload
# サービスの再起動、確認
sudo systemctl restart nginx httpd
sudo systemctl status nginx httpd