LoginSignup
5
8

More than 3 years have passed since last update.

さくらのVPSにNginxをインストールしてhttpsで接続

Last updated at Posted at 2020-06-02

前提、環境

  • CentOS7
  • さくらのVPS
  • ドメインレジストラでネームサーバーをさくらにVPSに向けている
  • さくらのコンパネでドメインの登録をしている
  • Firewallの設定でhttpとhttpsを許可している
  • ssh接続してrootユーザーに切り替え済み

その他、基本的な設定は以下の記事の通りです。
さくらのVPSにSSH接続+最低限のセキュリティ対策

Nginxをインストール

yum install nginxだけでもインストールできますが、
バージョンが若干古い & デフォルトで生成される設定ファイルが少ない
ため、リポジトリを追加してからインストールします。

$ vim /etc/yum.repos.d/nginx.repo
/etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
$ yum info nginx #インストールできるnginxのバージョンが良い感じになっていることを確認
$ yum -y --enablerepo=nginx install nginx
$ nginx -v
$ systemctl start nginx #起動
$ systemctl enable nginx.service #自動起動設定

ブラウザでipアドレスにアクセスして、nginxの初期画面が表示されることを確認

公開ディレクトリの作成、設定

ファイルをデプロイするディレクトリを作成します。

$ mkdir -p /var/www/html/site1
$ cd /var/www/html/site1
$ vim index.html
/var/www/html/site1/index.html
hello from site1

nginxの設定ファイル/etc/nginx/conf.d/default.confをコピーして編集

$ cd /etc/nginx/conf.d
$ cp default.conf site1.com.conf
$ vim site1.com.conf
/etc/nginx/conf.d/site1.com.conf
server {
    listen       80;
    server_name  site1.com;
    location / {
        root   /var/www/html/site1;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
$ systemctl reload nginx

http://site1.com にアクセスして表示を確認。

マルチドメインで運用する場合、公開ディレクトリ、nginxの設定ファイルをドメインの数だけ作成します。

SSL証明書を導入

$ yum install -y certbot
$ systemctl stop nginx #証明書の導入前にサーバーを停止(standaloneモード用のサーバーを立てるため)
# 証明書をインストール。以下の2ファイルが手に入る
# /etc/letsencrypt/live/site1.com/fullchain.pem;
# /etc/letsencrypt/live/site1.com/privkey.pem;
$ certbot certonly --standalone -d site1.com # メールアドレスとか聞かれるので答える
$ systemctl start nginx
$ vim /etc/nginx/conf.d/site1.com.conf
/etc/nginx/conf.d/site1.com.conf
server {
    listen       80;
    server_name  site1.com;
    return 301 https://$host$request_uri;  # 追加。httpへのリクエストをhttpsにリダイレクト
    location / {
        root   /var/www/html/site1;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

# 以下を追加。https用の設定
server {
        listen  443 ssl;
        server_name     site1.com;
        ssl_certificate         /etc/letsencrypt/live/site1.com/fullchain.pem;
        ssl_certificate_key     /etc/letsencrypt/live/site1.com/privkey.pem;
    location / {
        root   /var/www/html/site1;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
$ systemctl reload nginx

http://site1.com にアクセスして、httpsにリダイレクトされれば成功。

ハマったところ

さくらのVPSにはパケットフィルタという独自のファイアーウォールのような機能があります。
管理画面からパケットフィルタの設定を変更して、80番や443番のポートを開放する必要があるので注意してください。

5
8
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
5
8