CentOS Stream 9
Sponsored Link

SSL 証明書を取得する (Let's Encrypt)2022/01/18

 
Let's Encrypt から 発行料 無料の SSL/TLS サーバー証明書を取得します。
Let's Encrypt は Linux Foundation の協業プロジェクトで、Web 全体の安全性を改善することをミッションに掲げています。
Let's Encrypt の詳細は公式サイトを参照ください。
⇒ https://letsencrypt.org/
Let's Encrypt では 一般的な ドメイン認証 (DV) の証明書を無料で発行しています。
無料ですが Let's Encrypt の中間証明書は、大手認証局 (CA) のルート証明書によってクロス署名されているため、多くの主要 Web ブラウザーで信頼済みとして扱われます。
なお、一回の作業で得られる証明書の有効期限は 90日です。よって、90日以内に更新作業を再度実施する必要があります。
ただし、下例のように Snapd からインストールした Certbot パッケージであれば、Systemd の Timer が付属しているため、更新作業は自動で実行されます。
[1]
[2] 証明書を取得するためのツール Certbot クライアントをインストールします。
[root@dlp ~]#
snap install certbot --classic

certbot 1.24.0 from Certbot Project (certbot-eff✓) installed
[root@dlp ~]#
ln -s /snap/bin/certbot /usr/bin/certbot

[3] 証明書を取得します。
下例は Apache httpd や Nginx などの Web サーバーが稼働していることが前提となります。
作業を実施するサーバーで Web サーバーが稼働していない場合は [4] を参照ください。
また、インターネット側から、作業を実施するサーバー (証明書を取得したい FQDN のサーバー) の 80 ポート宛てにアクセス可能であることも前提です。
# [--webroot] 指定で稼働中 Web サーバーの公開ディレクトリ配下を認証用の一時領域に使用
# -w [ドキュメントルート] -d [証明書を取得したい FQDN]
# FQDN (Fully Qualified Domain Name) : ホスト名.ドメイン名を省略なしで表記
# ドキュメントルートはバーチャルホストで複数のホスト定義がある場合、該当するホスト定義のものを指定
# 証明書を取得したい FQDN が複数ある場合は、-d [証明書を取得したい FQDN] を複数指定
# 例 : [srv.world/www.srv.world] の二つについて取得する場合
# ⇒ [-d srv.world -d www.srv.world]
[root@dlp ~]#
certbot certonly --webroot -w /var/www/html -d dns.srv.world

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
# 初回のみメールアドレスの登録と利用条件への同意が必要
# 受信可能なメールアドレスを指定
 (Enter 'c' to cancel): root@mail.srv.world

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 非営利団体 Electronic Frontier Foundation にもメールアドレスを登録するか否か
(Y)es/(N)o: Y
Account registered.
Requesting a certificate for dns.srv.world

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/dns.srv.world/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/dns.srv.world/privkey.pem
This certificate expires on 2022-02-03.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

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

# [Successfully received certificate] と表示さえれば成功
# メッセージ中に記載の通り [/etc/letsencrypt/live/(FQDN)/] 配下に証明書が取得されている

# cert.pem       ⇒ SSLサーバー証明書 (公開鍵含む)
# chain.pem      ⇒ 中間証明書
# fullchain.pem  ⇒ cert.pem と chain.pem が結合されたファイル
# privkey.pem    ⇒ 公開鍵に対する秘密鍵
[4] 作業を実施するサーバーで Webサーバーが稼働していない場合でも、Certbot の簡易 Webサーバー機能を使用して証明書の取得が可能です。 いずれにしろ、インターネット側から当作業を実施するサーバーの 80 ポート宛てにアクセス可能であることが前提となります。
# [--standalone] 指定で Certbot の簡易 Webサーバー機能を使用
# -d [証明書を取得したい FQDN]
# FQDN (Fully Qualified Domain Name) : ホスト名.ドメイン名を省略なしで表記
# 証明書を取得したい FQDN が複数ある場合は [-d (証明書を取得したい FQDN)] を複数指定
# 例 : [srv.world/www.srv.world] について取得する場合 ⇒ [-d srv.world -d www.srv.world]
[root@dlp ~]#
certbot certonly --standalone -d rx-9.srv.world

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for rx-9.srv.world

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/rx-9.srv.world/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/rx-9.srv.world/privkey.pem
This certificate expires on 2022-02-03.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[5] 取得済みの証明書を、手動でまとめて更新する場合は、[renew] サブコマンドを使用します。
有効期限が 30日未満の証明書が全て更新されます。
有効期限の残り日数に関わらず更新したい場合は [--force-renew] を合わせて指定します。
ただし、当例のように Snapd からインストールした [certbot] クライアントであれば、更新の手動実行は必要ありません。更新は自動で実行されます。
# Snapd の Certbot パッケージはタイマーが付属

[root@dlp ~]#
systemctl status snap.certbot.renew.timer

*  snap.certbot.renew.timer - Timer renew for snap application certbot.renew
     Loaded: loaded (/etc/systemd/system/snap.certbot.renew.timer; enabled; ven>
     Active: active (waiting) since Mon 2022-03-14 13:24:13 JST; 14min ago
      Until: Mon 2022-03-14 13:24:13 JST; 14min ago
    Trigger: Mon 2022-03-14 13:48:00 JST; 8min left
   Triggers: * snap.certbot.renew.service

# デフォルトでは以下のように毎日 2回 [renew] 実行

[root@dlp ~]#
systemctl cat snap.certbot.renew.timer

# /etc/systemd/system/snap.certbot.renew.timer
[Unit]
# Auto-generated, DO NOT EDIT
Description=Timer renew for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-1842.mount
After=var-lib-snapd-snap-certbot-1842.mount
X-Snappy=yes

[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 03:58
OnCalendar=*-*-* 13:48

[Install]
WantedBy=timers.target

# 手動で更新する場合は以下

[root@dlp ~]#
certbot renew

[6] Windows 向けに PKCS12 (PFX) 形式に変換するには以下のように実行します。
[root@dlp ~]#
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out dlp_for_iis.pfx

Enter Export Password:     # 任意のエクスポートパスワードを設定
Verifying - Enter Export Password:
関連コンテンツ