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

タイトル: VPSへの導入例
SEOタイトル: CentOS VPS への nginx 導入例(firewalld 設定 / systemd / SELinux / 動作確認手順)

この記事の要点
  • CentOS / Rocky Linux ベースの VPS に nginx を導入する基本手順
  • firewalld で http / https の通信を許可(--add-service=http --permanent
  • systemctl で nginx の起動と自動起動を設定
  • SELinux が有効な場合の対応(document root 配置時の文脈設定)
  • 導入後は curl http://localhost や外部ブラウザでの動作確認、journalctl -u nginx でログ確認

前提条件

  • CentOS Stream / Rocky Linux / AlmaLinux などの RHEL 系 OS が稼働している VPS
  • root または sudo 権限のあるユーザー
  • 外部から 80 / 443 番ポートが解放されている(VPS 管理画面のファイアウォール設定も確認)
  • nginx のインストール済み(まだなら こちら 参照)

導入の全体フロー

  1. nginx インストール
  2. firewalld で http / https を許可
  3. systemctl で起動 / 自動起動
  4. SELinux 設定(必要なら)
  5. document root と設定ファイル配置
  6. 動作確認

1. firewalld の設定

インストール(未導入なら)

sudo yum install -y firewalld
# RHEL 8+ / Rocky Linux なら dnf も可
sudo dnf install -y firewalld

起動と自動起動の設定

sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo systemctl status firewalld

http / https 通信を許可

sudo firewall-cmd --add-service=http  --zone=public --permanent
sudo firewall-cmd --add-service=https --zone=public --permanent
sudo systemctl restart firewalld

# 確認
sudo firewall-cmd --list-all --zone=public

--permanent を付けないと再起動で消えるので必須。設定変更後は firewall-cmd --reload または systemctl restart firewalld で反映します。

2. nginx の起動 / 自動起動

sudo systemctl start nginx
sudo systemctl enable nginx

# 状態確認
sudo systemctl status nginx

3. SELinux 対応

RHEL 系では SELinux がデフォルト Enforcing で動作しているため、デフォルトのドキュメントルート(/usr/share/nginx/html)以外を使う場合は文脈設定が必要です。

状態確認

getenforce
# Enforcing / Permissive / Disabled のいずれか

独自ディレクトリを document root にする場合

# 例: /var/www/mysite を使う
sudo mkdir -p /var/www/mysite
echo "Hello from nginx" | sudo tee /var/www/mysite/index.html

# SELinux 文脈の付与(httpd_sys_content_t)
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/mysite(/.*)?"
sudo restorecon -Rv /var/www/mysite

semanage がない場合は sudo yum install policycoreutils-python-utils で導入。

非標準ポートでリッスンする場合

# 例: 8080 ポートでもリッスン
sudo semanage port -a -t http_port_t -p tcp 8080

4. 動作確認

サーバ内から

curl -I http://localhost
# HTTP/1.1 200 OK
# Server: nginx/1.20.1

外部ブラウザから

http://<VPS の IP アドレス>/ をブラウザで開き、nginx のテストページ(または独自の index.html)が表示されればOK。

ログの確認

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

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

# systemd ログ
sudo journalctl -u nginx -n 50

5. 設定ファイルの場所

パス用途
/etc/nginx/nginx.confメイン設定ファイル
/etc/nginx/conf.d/*.confサイト別設定(include される)
/usr/share/nginx/html/デフォルト document root
/var/log/nginx/access.log / error.log

6. シンプルなバーチャルホストの例

/etc/nginx/conf.d/mysite.conf を作成。

server {
    listen 80;
    server_name mysite.example.com;

    root /var/www/mysite;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    access_log /var/log/nginx/mysite_access.log;
    error_log  /var/log/nginx/mysite_error.log;
}

設定変更時は必ず構文チェック → リロード:

sudo nginx -t                # 構文チェック
sudo systemctl reload nginx  # 設定反映

7. HTTPS 対応(Let's Encrypt)

Certbot で無料の SSL 証明書を取得・自動更新できます。

sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-nginx

sudo certbot --nginx -d mysite.example.com

# 自動更新の確認
sudo certbot renew --dry-run

よくあるトラブル

症状原因 / 対処
外部からつながらないfirewalld または VPS 側のファイアウォールで 80/443 が閉じている
403 ForbiddenSELinux 文脈不一致 → restorecon
502 Bad Gatewayバックエンド(PHP-FPM / Node 等)が落ちている
nginx -t で syntax error設定ファイルの構文エラー。エラー行をチェック
start で failjournalctl -u nginx で詳細確認、ポート競合(80 を別プロセスが使用中など)

セキュリティの基本

  • サーバ情報の隠蔽: server_tokens off; でバージョン情報を非表示
  • HTTPS の強制: 80 → 443 へのリダイレクト
  • HSTS: add_header Strict-Transport-Security "max-age=31536000";
  • 不要モジュールの無効化
  • 定期アップデート: dnf update -y

関連

  • nginx — 高性能 Web サーバ / リバースプロキシ
  • firewalld — RHEL 系の標準ファイアウォール管理
  • systemctl — サービス管理
  • SELinux — 強制アクセス制御
  • Let's Encrypt — 無料 SSL 証明書