LoginSignup
24
22

More than 5 years have passed since last update.

Route53 で Let’s Encrypt 証明書取得&更新を行う最低限の手順

Posted at

できること

  • Let's Encrypt 証明書取得 (wildcard, SAN もOK)
  • 証明書の自動更新

前提

  • 対象ドメインのゾーンが Route53 管理下
  • 証明書を置くホストに、Route53 操作権限を与えられる

手順の概要

  • certbot インストール
  • certbot-dns-route53 プラグインのインストール
    • credentials を設定
  • certbot コマンドで初回の証明書取得
  • 定期更新を設定

具体例

以下 Ubuntu 18.04 での例

certbot インストール

https://certbot.eff.org/ の手順でインストールする。

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install certbot 

certbot-dns-route53 プラグインのインストール

README には setuptools を使う方法が記載されているが、pip install が簡単だと思う。

$ sudo apt install python3-pip libssl-dev libffi6
$ sudo pip3 install certbot-dns-route53

credentials を設定

必要な権限は route53:ListHostedZones, route53:GetChange と対象ゾーンに対する Route53:ChangeResourceRecordSets 。こちら => https://certbot-dns-route53.readthedocs.io/en/stable/#credentials を参考に IAM を設定する。
上記の権限を与えた credentials を以下のように配置。

$ cat ~/.aws/credentials
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

または、証明書を置くホストが EC2 ならば IAM Role を使ってもよい。

初回の証明書取得

$ sudo certbot certonly --dns-route53 \
    -d '*.mydomain.com' \
    -d 'www.altname.mydomain.com' \
    --email myname@myemail.com \
    -n \
    --agree-tos \

証明書取得やが行われ、/etc/letsencrypt 以下に証明書や設定ファイルが配置される。
サーバからは live に配置されている証明書を参照する

$ sudo ls /etc/letsencrypt/live/mydomain.com -l
total 4
-rw-r--r-- 1 root root 692 Jan 10 09:39 README
lrwxrwxrwx 1 root root  32 Jan 10 09:39 cert.pem -> ../../archive/mydomain.com/cert1.pem
lrwxrwxrwx 1 root root  33 Jan 10 09:39 chain.pem -> ../../archive/mydomain.com/chain1.pem
lrwxrwxrwx 1 root root  37 Jan 10 09:39 fullchain.pem -> ../../archive/mydomain.com/fullchain1.pem
lrwxrwxrwx 1 root root  35 Jan 10 09:39 privkey.pem -> ../../archive/mydomain.com/privkey1.pem

定期更新を設定

Ubuntu 18.04 の certbot パッケージでは、インストール時に systemd timer による定期実行も設定してくれる(ついでにcronも入る)。

$ systemctl list-unit-files  | grep certbot
certbot.service                                static
certbot.timer                                  enabled
$ sudo systemctl list-timers
NEXT                         LEFT          LAST                         PASSED       UNIT
...
Fri 2019-01-11 15:37:43 UTC  11h left      n/a                          n/a          certbot.timer
...
$ ls /lib/systemd/system/certbot*
/lib/systemd/system/certbot.service  /lib/systemd/system/certbot.timer

通常、証明書の更新後はサーバのリロードも行うと思うので、post-hook を追記する。

$ EDITOR=vim sudo -E systemctl edit certbot.service

編集内容はこんな↓

[Service]
ExecStart=
ExecStart=/usr/bin/certbot -q renew --post-hook "systemctl reload nginx"
~

編集したら $ sudo systemctl daemon-reload でリロード

動作確認のためには、certbot renew に一時的に --force-renewal をつけてみて、(timerのデフォルトは半日毎なので)翌日にでも /etc/letsencrypt/archive 以下が更新され、サーバのリロードログがでているかなどを見ればよい。確認したら忘れず戻す。

関連

24
22
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
24
22