LoginSignup
1
0

とりあえずcertbotでSSL証明書を発行する

Last updated at Posted at 2023-08-29

今まで検証用サーバなどで利用するSSL証明書をSSLなうを利用させてもらって発行していましたが、いつのまにかアクセスできなくなっていました。
=>いったん「https://sslnow.cf/」に移行されたようです。

なので、ブラウザからではないですが同じ感じでLet'sEncryptのSSL証明書を発行できるものを探してみたところcertbotがCLIツールですが手軽に使えたので備忘録として残しておきます。

必要なもの

  • docker(任意)
  • certbot
  • ドメイン
  • DNSサーバ

今回はcertbot のdockerイメージがあったためそれを利用してみました。各OSのインストール方法はGet Certbot — Installtionのリンクから参照してください。
またACMEチャレンジはDNS-01(txtレコードを追加)で行います。

certbotコンテナの起動

とりあえずバックグラウンドでコンテナを起動させ、コンテナにアクセスしコマンドをたたくようにします。
ホストマシンの適当なディレクトリをコンテナにマウントしてそこでSSL証明書ファイルのやり取りをします。

# コンテナ起動
docker run -d -i --rm --name certbot -v <ホストの任意のディレクトリ>:/opt/certbot/share_cert --entrypoint "/bin/sh" certbot/certbot

# 起動後コンテナにアクセスできればOK
docker exec -it certbot2 sh

SSL証明書の発行

証明書の発行は以下のような流れとなります。

  1. certbotコマンドを実行
  2. 利用規約などへ同意(初回のみ)
  3. ACMEチャレンジのtxtレコードに設定する値が表示され、ターミナルがEnterキー押下待ち状態となる
  4. DNSへtxtレコードを設定
  5. DNSのtxtレコードが反映後、Enterキーを押下すると証明書が発行
# 1. certbotコマンドを実行
# -d(domain)オプションと-mオプション(メールアドレス)を設定
certbot certonly --manual --preferred-challenges dns -d <発行したいFQDN> -m <YOUR-MAIL-ADDRESS>
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y # 2.利用規約などへの同意

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 2.利用規約などへの同意
# 特にいらないのでNo、Noにしても有効期限のメールは来る??未確認
(Y)es/(N)o: N 
Account registered.
Requesting a certificate for <発行しようとしているFQDN>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.<発行しようとしているFQDN>.

with the following value:

xxxxxxxxxxxxxxxxxxxx  # 3. ACMEチャレンジのtxtレコードに設定する値が表示

Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.watatest.vantiqjp.com.
Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 3. ターミナルがEnterキー押下待ち状態となる、txtレコード反映後Enterを押下
Press Enter to Continue  

# 4. DNSのtxtレコードが反映後、Enterキーを押下すると証明書が発行
# 押下後、検証が成功すると以下のようにファイルが作成される
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/<発行しようとしているFQDN>/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/<発行しようとしているFQDN>/privkey.pem
This certificate expires on 2023-11-23.
These files will be updated when the certificate renews.

NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.

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

生成した証明書ファイルをホストマシン側ディレクトリに移動

# 生成された証明書ファイルの確認
$ ls -l /etc/letsencrypt/live/<発行したFQDN>/
total 4
-rw-r--r--    1 root     root           692 Aug 25 08:05 README
lrwxrwxrwx    1 root     root            45 Aug 25 08:05 cert.pem -> ../../archive/watatest.vantiqjp.com/cert1.pem
lrwxrwxrwx    1 root     root            46 Aug 25 08:05 chain.pem -> ../../archive/watatest.vantiqjp.com/chain1.pem
lrwxrwxrwx    1 root     root            50 Aug 25 08:05 fullchain.pem -> ../../archive/watatest.vantiqjp.com/fullchain1.pem
lrwxrwxrwx    1 root     root            48 Aug 25 08:05 privkey.pem -> ../../archive/watatest.vantiqjp.com/privkey1.pem

# コンテナにマウントしているホスト側ディレクトリへ上記ファイルを移動
$ cp /etc/letsencrypt/live/<発行したFQDN>/* /opt/certbot/share_cert/

ホスト側に戻り、上記証明書ファイルがあることを確認して完了です。
念のため証明書の中身を確認する場合は以下のコマンドなどで確認してください。

$ openssl x509 -text -noout -in fullchain.pem

後は発行したファイルをWebサーバに送るなどして利用できます。
また、ACMEチャレンジ用にDNSへ登録したtxtレコードは不要なので削除します。(残しておいても問題ありません。)

補足)ワイルドカード証明書について

Getting certificates (and choosing plugins)にはcertbotのDNSプラグインがLet'sEncryptのワイルドカード証明書に対応している、との記載がありましたがmanualプラグインでも-dオプションに*.example.comのように指定すると発行できました。
自動更新に対応しているのがDNSプラグインということなのかもしれないです。

1
0
2

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