この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:4
ページ更新者:T
更新日時:2026-06-11 07:10:02

タイトル: 起動・停止コマンドとステータスの確認 (CentOS編)
SEOタイトル: Nginx 起動・停止・ステータス CentOS 編完全ガイド

この記事の要点
  • 起動: sudo systemctl start nginx / 停止: stop / 再起動: restart
  • 無停止リロード: sudo systemctl reload nginx(設定変更だけ反映)
  • 状態確認: systemctl status nginx / is-active / is-enabled
  • 自動起動: sudo systemctl enable nginx(OS 起動時に自動で立ち上がる)
  • 設定構文チェック: sudo nginx -t(reload 前に必須)
  • ログ: /var/log/nginx/access.log / error.log / journalctl -u nginx

主要コマンド早見表

目的コマンド
起動sudo systemctl start nginx
停止sudo systemctl stop nginx
再起動(プロセス再生成)sudo systemctl restart nginx
無停止リロード(設定再読込)sudo systemctl reload nginx
状態確認systemctl status nginx
稼働中か(true/false)systemctl is-active nginx
自動起動有効化sudo systemctl enable nginx
自動起動無効化sudo systemctl disable nginx
自動起動状態確認systemctl is-enabled nginx
設定構文チェックsudo nginx -t
ログ表示(リアルタイム)sudo journalctl -u nginx -f

起動・停止・再起動

# 起動
sudo systemctl start nginx

# 停止
sudo systemctl stop nginx

# プロセス完全再起動(短時間切断あり)
sudo systemctl restart nginx

# 設定だけ再読込(無停止、推奨)
sudo systemctl reload nginx

# プロセスが居なければ起動、居れば再起動
sudo systemctl restart nginx

reload と restart の違い

本番運用では原則 reloadを使います。restart は接続が切れます。

操作動作切断使い所
reloadマスタープロセスが新ワーカー起動 → 古いワーカーは処理完了後に終了無し設定変更後
restartマスター含め全プロセス停止 → 起動あり(数百 ms)nginx 自体のアップグレード後

必ず構文チェックしてから reload

設定ミスがあると reload は失敗し、最悪はマスターが起動しなくなることもあります。nginx -t で構文チェック後に reload が鉄則。

# 設定ファイル構文チェック
sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

# ダンプ(実際に解釈される設定全体を表示)
sudo nginx -T

# 確認 OK なら reload
sudo systemctl reload nginx

# ワンライナー(チェック失敗なら reload しない)
sudo nginx -t && sudo systemctl reload nginx

自動起動(enable / disable)

# OS 起動時に自動で nginx を立ち上げる
sudo systemctl enable nginx

# 状態確認
systemctl is-enabled nginx
# enabled / disabled / static / masked

# 自動起動無効化
sudo systemctl disable nginx

# enable と start を同時に
sudo systemctl enable --now nginx

# disable と stop を同時に
sudo systemctl disable --now nginx

状態確認の見方

$ systemctl status nginx
* nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2026-06-10 09:00:00 JST; 2h ago    ← 稼働状態
  Process: 1234 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 1235 (nginx)
    Tasks: 5 (limit: 23456)
   Memory: 12.3M
   CGroup: /system.slice/nginx.service
           ├─1235 nginx: master process /usr/sbin/nginx
           ├─1236 nginx: worker process
           ├─1237 nginx: worker process
           ├─1238 nginx: worker process
           └─1239 nginx: worker process

Jun 10 09:00:00 host systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jun 10 09:00:00 host systemd[1]: Started The nginx HTTP and reverse proxy server.

# 主な Active 状態
# active (running)  → 稼働中
# active (exited)   → 起動成功して終了(oneshot 型)
# inactive (dead)   → 停止
# failed            → 異常終了(要調査)

スクリプトで使う系コマンド

# シェルスクリプトの分岐で使いやすい
if systemctl is-active --quiet nginx; then
    echo "running"
else
    echo "stopped"
    sudo systemctl start nginx
fi

# 戻り値で判定
systemctl is-active nginx > /dev/null && echo OK

# 全 unit から nginx 系を絞り込み
systemctl list-units --type=service | grep nginx

ログの確認

# アクセスログ
sudo tail -f /var/log/nginx/access.log

# エラーログ
sudo tail -f /var/log/nginx/error.log

# systemd ジャーナル(起動失敗時はこちらが詳しい)
sudo journalctl -u nginx -f             # リアルタイム
sudo journalctl -u nginx --since today
sudo journalctl -u nginx -n 100         # 最新 100 行
sudo journalctl -u nginx -p err         # error 以上のみ

# ログのサイズ確認とローテーション
ls -lh /var/log/nginx/
cat /etc/logrotate.d/nginx

ポート / プロセスの確認

# 待ち受けポート
sudo ss -tlnp | grep nginx
# LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1235,fd=6))

# 旧 netstat(要 net-tools)
sudo netstat -tlnp | grep nginx

# プロセス
ps aux | grep nginx
# nginx: master process /usr/sbin/nginx
# nginx: worker process

ファイアウォール (firewalld) と SELinux

CentOS / RHEL は firewalld と SELinux がデフォルト有効です。nginx を立てたのに外からアクセスできない場合は要確認。

# firewalld で 80/443 を開放
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

# SELinux で nginx が独自ポート (例 8080) を listen する場合
sudo semanage port -a -t http_port_t -p tcp 8080

# SELinux で /var/www 以外を DocumentRoot にする場合
sudo chcon -Rt httpd_sys_content_t /opt/myapp/public/
sudo setsebool -P httpd_can_network_connect on   # upstream へ接続許可

# 一時的に SELinux を無効化して切り分け(推奨しない)
sudo setenforce 0

起動失敗時のトラブルシューティング

# 1. status で原因確認
sudo systemctl status nginx -l

# 2. journal の最新エラー
sudo journalctl -u nginx -n 50 --no-pager

# 3. 設定ミス確認
sudo nginx -t

# 4. ポート競合確認(80/443 が既に使われていないか)
sudo ss -tlnp | grep -E ':80|:443'
# Apache が動いていたら停止
sudo systemctl stop httpd
sudo systemctl disable httpd

# 5. ファイル権限確認
ls -la /var/log/nginx/
ls -la /etc/nginx/nginx.conf

古い CentOS 6 系(systemd 以前)

CentOS 6 以前は init.d / chkconfig を使います。CentOS 7 以降は systemd 統一。

# CentOS 6 (init.d)
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
sudo service nginx reload
sudo service nginx status

# 自動起動
sudo chkconfig nginx on
sudo chkconfig nginx off
chkconfig --list nginx

FAQ

Q: nginx -s reload と systemctl reload nginx の違い
A: ほぼ同じ(マスターに HUP シグナルを送る)。systemctl 経由のほうが状態管理されるので推奨。

Q: reload しても変更が反映されない
A: nginx -T で実際に読み込まれている設定をダンプ。include 関係や server_name のミスを確認。キャッシュ系ディレクティブは reload で反映されないものもあり、その場合は restart 必要。

Q: 起動時に "Failed to read PID"
A: /run/nginx.pid の権限・存在を確認。設定の pid ディレクティブと systemd unit ファイルの PIDFile= 一致を確認。