LoginSignup
1
1

More than 5 years have passed since last update.

systemctlコマンドでnginxとhttpdを自動起動にするとnginxがコケる

Posted at

環境とか状況

  • CentOS7
  • nginx(v1.15.8)
    • リバースプロキシで待受、アクセス振り分け
      • port:443
  • httpd(2.4.35)
    • wordpressとかdjangoとか動かしている。アプリごとに違うポートで待受している
      • port:8443
      • port:8444

原因

nginxhttpdより先に起動すると、当然まだ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を追加することでnginxhttpd起動後に立ち上がるようにしてくれます。ついでに、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

最後にsystemctlunitファイルを更新したよ~っていう通知を行います。

sudo systemctl daemon-reload

# サービスの再起動、確認
sudo systemctl restart nginx httpd
sudo systemctl status nginx httpd

終わり

参考にした記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1