Armoris日記 Let’s Encrypt編

このブログは、3月までN高等学校に潜んでいた株式会社Armorisの社員が書いています。

あるもりすぶろぐの内容は個人の意見です。

Let’s Encrypt を使ってサイトを HTTPS 化する

昨今一部ブラウザの比較的新しいバージョンで、アクセスしたWebサイトが SSL/TLS 設定されていない場合、「このサイトは安全ではありません」というような警告が表示されるようになっています。
このような警告が出たからと言って、必ずしも当該サイトに危険性があると言うわけではないのですが、サイトの訪問者の中には不安に感じる人もいると思います。

そこで今回は Let’s Encrypt を使用して簡単に SSL/TLS の設定を行い、サイトを HTTPS 対応のサイトにする方法を紹介します。

Let’s Encrypt についての詳しい情報はこちらの wiki をご覧ください。
Let’s Encrypt 公式サイト はこちら。

使用した環境

今回使用した環境は以下になります。
Web サーバーは Nginx を使用しています。

サーバー環境
f:id:Armoris:20200820120945p:plain

Nginx 環境
f:id:Armoris:20200820120949p:plain

Certbot
f:id:Armoris:20200820122420p:plain

事前準備

まずは Cloudflare などでドメインによる名前解決ができるようにしておきます。
画像は Cloudflare での設定例です。
f:id:Armoris:20200820120941p:plain

サーバー側で HTTPS 通信用に 443 ポートを使用できるようにしておきます。
これは利用している環境に合わせて作業を行ってください。

Certbot をインストール

Let’s Encrypt で証明書を取得するために Certbot をインストールします。

$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt update
$ sudo apt -y install certbot

Nginx の設定

Certbot のインストールが完了したら Nginx の設定を変更します。
/etc/nginx/conf.d/example.conf

server {
  listen 443;
  listen [::]:443 ssl;
  server_name example.com;

  ssl                   on;
  ssl_certificate       /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key   /etc/letsencrypt/live/example.com/privkey.pem;

  ssl_session_timeout   5m;

  ssl_protocols              TLSv1.2;
  ssl_ciphers                HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers  on;

  root /var/www/html;
  index index.nginx-debian.html;
}

config の設定が完了したら Nginx を再起動します。

$ sudo service nginx restart

証明書をインストール

ここまでの手順が完了したらいよいよ Certbot を使用して証明書をインストールします。
webroot-pathドメイン名、メールアドレスは適所環境に合わせて変更してください。

$ sudo certbot certonly --webroot --webroot-path /var/www/html -d example.com -m user@user.local
    ~~~~~~~~~~~~~~~~~~~ 省略 ~~~~~~~~~~~~~~~~~~~
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2020-00-00. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

証明書のインストールが完了したら https://example.com で接続できるようになります。

自動更新の設定

Let’s Encrypt の証明書は有効期限が 90日なので期限が切れる前に更新しなければいけません。手動で更新することもできますが、更新し忘れることもあるため自動更新の設定をお勧めします。

自動更新の設定が完了すると以下のように success と表示されます。

$ sudo certbot renew --dry-run
    ~~~~~~~~~~~~~~~~~~~ 省略 ~~~~~~~~~~~~~~~~~~~
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/example.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

以上で一通りの設定が完了しました。

終わりに

冒頭でも書いた通り、最近のブラウザでは HTTPS化されていないサイトにアクセスしようとすると警告が出るようになりました。
今回紹介した方法で Let’s Encrypt による証明書の発行を行い、サイトの HTTPS化を簡単に行うことができます。

個人でサイトなどを作成した際はぜひ今回の方法を参考にサイトを HTTPS化してみてください。