Let's encryptでAmazon Linux2でDNS-01で証明書を手動で作る


概要

  • AWS上でやんごとなき事情で限定公開しかできないインスタンスで証明書が欲しいよ
  • Route53も使えないけど、外部でDNS登録はできるからDNS-01方式だったらやれそうだよ

って方向けの内容です
ちょくちょくやるけど忘れちゃうのでメモ
(きっと探せば類似記事がいっぱいあるだろうなぁ)

やりかた

certbotをインストール

# 聞かれたらよしなに'y'か'-y'オプションつけてね
sudo amazon-linux-extras install epel
sudo yum install certbot

証明書発行

オプションで指定できるけど今回は手入力

sudo certbot certonly --manual --preferred-challenges dns

色々聞かれる

コマンド実行の中で証明書発行のためにアカウント作成周りのことが聞かれる

[ec2-user@ip-10-210-15-44 ~]$ sudo certbot certonly --manual --preferred-challenges dns
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): [email protected]

書いてある通り、緊急通知とかの連絡先のメールアドレスを入れる

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

利用許諾に同意してね

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Account registered.

管理している団体をサポートするために何かしらメール送ってもいいかい?(的な意訳)
YでもNでもどちらでもお好きな方を
ここまででアカウント登録

証明書発行周り

Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): xxx.xxx.com

証明書を発行したいFQDNが聞かれるので入力する
入力したらDNS登録が待っているので待機

DNS登録確認

Requesting a certificate for xxx.xxx.com
Performing the following challenges:
dns-01 challenge for xxx.xxx.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.xxx.xxx.com with the following value:

xxxxxxxxxxxx

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

上記のように表示されたらDNS登録
_acme-challenge.xxx.xxx.comと書かれたFQDNを、
TXTレコードでxxxxxxxxxxxxと書かれている所の値で指定して登録

別ウィンドウや別EC2等で正常に目的のレコードが登録されているかを確認
(社内とかより外側のDNSを叩きに行った方が正確なものが取れそうなので、EC2がおすすめ)

[ec2-user@ip-10-210-15-95 ~]$ dig _acme-challenge.xxx.xxx.com txt

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.amzn2.2 <<>> _acme-challenge.xxx.xxx.com txt
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65226
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;_acme-challenge.xxx.xxx.com. IN    TXT

;; ANSWER SECTION:
_acme-challenge.xxx.xxx.com. 60 IN  TXT "xxxxxxxxxxxx"

;; Query time: 9 msec
;; SERVER: 10.210.15.2#53(10.210.15.2)
;; WHEN: 金  3月 05 17:58:31 JST 2021
;; MSG SIZE  rcvd: 117

上記のように登録されていることが確認できたら、Enter

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/xxx.xxx.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/xxx.xxx.com/privkey.pem
   Your certificate will expire on 2021-06-03. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again. 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

これで/etc/letsencrypt/live/xxx.xxx.com/配下に証明書が発行されたので、
よしなに使いたいアプリに食わせて使いましょう

おまけ

Nginxならこんな感じで上記ファイルを指定して使う
(デフォルトConfigからいじる場合)

    server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name  _;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/letsencrypt/live/xxx.xxx.com/fullchain.pem";
        ssl_certificate_key "/etc/letsencrypt/live/xxx.xxx.com/privkey.pem";
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }