LoginSignup
3
1

More than 3 years have passed since last update.

さくらVPSにjprsのSSL証明書をインストール

Posted at

環境

  • CentOS(さくらVPS)
  • Docker
  • nginx
  • openssl

ゴール

https://でアクセス、http://アクセス時はhttps://でリダイレクト
スクリーンショット 2020-12-08 9.49.42.png

前提

  • 既にhttp://でアクセス可能とする。
  • dockerで環境構築済みとする。
  • opensslインストール済みとする(さくらVPSにインストールされていたバージョンを使用)。

SSL証明書の発行に必要なCSR(Certificate Signing Request)を作成

SSL証明書を申し込む際に必要な情報の1つ。作成方法はjprs公式のマニュアルを参照し作成する。
鍵ファイル作成時のパスフレーズは忘れないように!
スクリーンショット 2020-12-08 10.10.51.png

SSL証明書を購入する

使用するSSL証明書は低コストのjprsを選定。
さくらインターネットのコンパネにログインし、さくらインターネット経由で申し込む。
スクリーンショット 2020-12-08 10.14.46.png
スクリーンショット 2020-12-08 10.16.03.png

認証ファイルをダウンロードする

認証ファイルをサーバーに設置し認証局で承認されることでSSL証明書を取得することができるようになる。
まずは、SSL証明書を購入するとさくらインターネットの会員メニューから認証ファイルをダウンロードする。
名称未設定5.png
名称未設定4.png
名称未設定2.png

ダウンロードされた認証ファイルのファイル名は数字とローマ字が混在したテキストファイル。

認証ファイルをサーバーに設置する

nginxに記載されているドキュメントルート/.well-known/pki-validation配下に認証ファイルをアップロードし
ブラウザから認証ファイルの内容が参照できればOK。
スクリーンショット 2020-12-08 10.58.17.png

ドキュメントルートの確認はnginxのconfig(default.conf)を確認する。

nginx(default.conf)

server {
    listen 80;
    server_name example.com;   
    root /work/public; <- ドキュメントルート

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
:

SSL証明書をダウンロードする

認証局で認証ファイルが承認されるとSSL証明書が発行されダウンロード可能となる。
認証ファイルを設置してから20分程度で発行されダウンロード可能となる。
名称未設定7.png
名称未設定2.png

SSL証明書をインストールする

jprs公式のマニュアルを参照しインストールする。
スクリーンショット 2020-12-08 11.29.16.png

マニュアル中にある中間証明書のダウンロードは以下のとおり。
スクリーンショット 2020-12-08 11.28.25.png

マニュアル通りに「cat (サーバー証明書) (中間証明書) >/pathname/of/combined.crt」で中間証明書とサーバー証明証のファイルを合わせただけではnginxで起動エラーとなってしまったため、以下のように改行を挿入し編集が必要。

combined.crt(変更前)

-----BEGIN CERTIFICATE-----
サーバー証明書
-----END CERTIFICATE----------BEGIN CERTIFICATE-----
中間証明書
-----END CERTIFICATE-----
combined.crt(変更後)

-----BEGIN CERTIFICATE-----
サーバー証明書
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
中間証明書
-----END CERTIFICATE-----

鍵ファイル作成時のパスフレーズはテキストファイルに記載し設置する。

pass.txt
xxxxxxxxxxxxxx <- パスフレーズ

docker-compose.ymlに上記で作成したファイルの配置を記載する。

docker-compose.yml
version: "3.8"
services:
  web:
    image: nginx:1.19.2-alpine
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./src:/work
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./docker/nginx/ssl/example.com.key:/etc/pki/nginx/example.com.key <- 鍵ファイル
      - ./docker/nginx/ssl/combined.crt:/etc/pki/nginx/combined.crt <- サーバー証明書と中間証明書をあわせたもの
      - ./docker/nginx/ssl/pass.txt:/etc/pki/nginx/pass.txt <- 鍵ファイルのパスフレーズを記載したファイル
    working_dir: /work

nginxのconfigは以下の通り。

nginx(default.conf)
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri; <- http://アクセス時はhttps://にリダイレクト
}

server {
    listen 443 ssl; <- sslポート番号
    server_name example.com; <- 独自ドメイン
    root /work/public; <- ドキュメントルート

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    ssl_certificate /etc/pki/nginx/combined.crt; <- サーバー証明書と中間証明書をあわせたもの
    ssl_password_file /etc/pki/nginx/pass.txt; <- 鍵ファイル作成時のパスフレーズを記載したファイル
    ssl_certificate_key /etc/pki/nginx/example.com.key; <- 鍵ファイル
    ssl_session_timeout 5m;
}

nginx起動

mac
xxxMacBook:~ xxx$ ssh developer@xxx.xxx.xxx.xxx
developer@xxx.xxx.xxx.xxx's password: 
Last login: Mon Dec  7 23:50:58 2020 from ------

SAKURA Internet [Virtual Private Server SERVICE]

[developer@ ~]$ cd /var/www/html/example-app/
[developer@ example-app]$ docker-compose up --build
Creating network "example-app_default" with the default driver
Building app
Step 1/9 : FROM php:7.4-fpm-buster
 ---> 58d217bf6b84
Step 2/9 : SHELL ["/bin/bash", "-oeux", "pipefail", "-c"]
 ---> Using cache
 ---> b9de4244ddbf
Step 3/9 : ENV COMPOSER_ALLOW_SUPERUSER=1   COMPOSER_HOME=/composer
:

表示された!

スクリーンショット 2020-12-08 9.49.42.png

参考サイト

3
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
3
1