Ubuntu 23.04
Sponsored Link

SSL 証明書を取得する (Let's Encrypt)2023/04/24

 
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日です。
ただし、Certbot パッケージには、Systemd の Timer が付属しているため、初回の取得以降の更新作業は自動で実行されます。
[1] 証明書を取得するためのツール Certbot クライアントをインストールします。
root@dlp:~#
apt -y install certbot
[2] 証明書を取得します。
下例は Apache httpd や Nginx などの Web サーバーが稼働していることが前提となります。
作業を実施するサーバーで Web サーバーが稼働していない場合は [3] を参照ください。
また、インターネット側から、作業を実施するサーバー (証明書を取得したい FQDN のサーバー) の 80 ポート宛てにアクセス可能であることも前提です。
# [--webroot] 指定で稼働中 Web サーバーの公開ディレクトリ配下を認証用の一時領域に使用
# -w [ドキュメントルート] -d [証明書を取得したい FQDN]
# FQDN (Fully Qualified Domain Name) : ホスト名.ドメイン名を省略なしで表記
# ドキュメントルートはバーチャルホストで複数のホスト定義がある場合、該当するホスト定義のものを指定
# 証明書を取得したい FQDN が複数ある場合は、-d [証明書を取得したい FQDN] を複数指定
# 例 : srv.world/dlp.srv.world の 2つについて取得する場合
# ⇒ [-d srv.world -d dlp.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: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Certbot 開発の非営利団体 Electronic Frontier Foundation にもメールアドレスを登録するか否か
(Y)es/(N)o: A
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 2023-07-25.
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    ⇒ 公開鍵に対する秘密鍵
[3] 作業を実施するサーバーで Webサーバーが稼働していない場合でも、Certbot の簡易 Webサーバー機能を使用して証明書の取得が可能です。 いずれにしろ、インターネット側から当作業を実施するサーバーの 80 ポート宛てにアクセス可能であることが前提となります。
# [--standalone] 指定で Certbot の簡易 Webサーバー機能を使用
# -d [証明書を取得したい FQDN]
# FQDN (Fully Qualified Domain Name) : ホスト名.ドメイン名を省略なしで表記
# 証明書を取得したい FQDN が複数ある場合は -d [証明書を取得したい FQDN] を複数指定
# 例 : [srv.world/mail.srv.world] について取得する場合 ⇒ [-d srv.world -d mail.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 2023-07-25.
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[4] 取得済みの証明書をまとめて更新する場合は、[renew] サブコマンドを使用します。
有効期限が 30日未満の証明書が全て更新されます。
有効期限の残り日数に関わらず更新したい場合は [--force-renew] を合わせて指定します。
ただし、Certbot パッケージにはタイマーが付属しているため、通常は手動での更新実行は必要ありません。
# Certbot パッケージはタイマーが付属

root@dlp:~#
systemctl status certbot.timer

*  certbot.timer - Run certbot twice daily
     Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; preset: enable>
     Active: active (waiting) since Mon 2023-04-24 05:16:05 UTC; 47s ago
      Until: Mon 2023-04-24 05:16:05 UTC; 47s ago
    Trigger: Mon 2023-04-24 21:39:07 UTC; 16h left
   Triggers: * certbot.service

root@dlp:~#
systemctl list-timers certbot.timer --no-pager

NEXT                        LEFT     LAST PASSED UNIT          ACTIVATES
Mon 2023-04-24 21:39:07 UTC 16h left -    -      certbot.timer certbot.service

1 timers listed.
Pass --all to see loaded but inactive timers, too.

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

root@dlp:~#
systemctl cat certbot.timer

# /lib/systemd/system/certbot.timer
[Unit]
Description=Run certbot twice daily

[Timer]
OnCalendar=*-*-* 00,12:00:00
RandomizedDelaySec=43200
Persistent=true

[Install]
WantedBy=timers.target

root@dlp:~#
systemctl cat certbot.service

# /lib/systemd/system/certbot.service
[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://certbot.eff.org/docs
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true

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

root@dlp:~#
certbot renew

[5] 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:
関連コンテンツ