Let's Encrypt + certbot(無料 SSL 証明書)
『Let's Encrypt + certbot』は、無料で SSL/TLS 証明書を発行・自動更新できる仕組みです。Let's Encrypt は非営利の認証局(CA)であり、certbot はその公式クライアントツールです。certbot --nginx や certbot --apache を実行するだけで証明書の取得から Web サーバーの設定変更まで自動で行われます。証明書の有効期限は 90 日間ですが、systemd タイマーまたは cron による自動更新を設定することで、期限切れを意識せず運用できます。
構文
# -----------------------------------------------
# certbot のインストール(snap 経由・推奨)
# -----------------------------------------------
# snap デーモンをインストールします(Ubuntu 20.04以降は標準搭載)
# sudo apt install snapd
# 既存の certbot パッケージを削除してから snap 版をインストールします
# sudo apt remove certbot
# snap で最新版 certbot をインストールします
# sudo snap install --classic certbot
# certbot コマンドをシステム全体で使えるようにシンボリックリンクを作成します
# sudo ln -s /snap/bin/certbot /usr/bin/certbot
# -----------------------------------------------
# Nginx 向け証明書の取得と自動設定
# -----------------------------------------------
# certbot --nginx -d {ドメイン名}
# → Nginx の設定ファイルを自動検出し、証明書を取得します
# → HTTPS リダイレクト等の設定も自動で行われます
# 例: sudo certbot --nginx -d example.com -d www.example.com
# -----------------------------------------------
# Apache 向け証明書の取得と自動設定
# -----------------------------------------------
# certbot --apache -d {ドメイン名}
# → Apache の設定ファイルを自動検出し、証明書を取得します
# 例: sudo certbot --apache -d example.com
# -----------------------------------------------
# 証明書のみ取得(Web サーバー設定は手動)
# -----------------------------------------------
# certbot certonly --webroot -w {ドキュメントルート} -d {ドメイン名}
# → Web サーバーの設定は変更せず、証明書ファイルのみ取得します
# → --nginx / --apache に対応していないカスタム構成で使用します
# 例: sudo certbot certonly --webroot -w /var/www/html -d example.com
# certbot certonly --standalone -d {ドメイン名}
# → certbot が一時的にポート 80 を占有して証明書を取得します
# → Web サーバーを停止してから実行する必要があります
# 例: sudo certbot certonly --standalone -d example.com
# -----------------------------------------------
# 証明書の更新
# -----------------------------------------------
# certbot renew
# → 有効期限が 30 日以内に迫った証明書をすべて更新します
# → 通常は systemd タイマーまたは cron が自動実行します
# certbot renew --dry-run
# → 実際には更新せず、更新処理が正常に動作するかテストします
# → 自動更新の設定後に必ず実行してください
# -----------------------------------------------
# 証明書の管理
# -----------------------------------------------
# certbot certificates
# → 取得済みの証明書の一覧と有効期限を表示します
# certbot delete --cert-name {証明書名}
# → 不要になった証明書を削除します
構文一覧
| コマンド | 説明 |
|---|---|
certbot --nginx -d {ドメイン} | Nginx 向けに証明書を取得し、設定ファイルを自動更新します。複数ドメインは -d を繰り返して指定します。 |
certbot --apache -d {ドメイン} | Apache 向けに証明書を取得し、設定ファイルを自動更新します。 |
certbot certonly --webroot -w {ルート} -d {ドメイン} | Web サーバーの設定は変更せず証明書ファイルのみ取得します。手動で設定を管理したい場合に使用します。 |
certbot certonly --standalone -d {ドメイン} | certbot が一時的に HTTP サーバーを起動して認証します。Web サーバーをあらかじめ停止する必要があります。 |
certbot renew | 有効期限が 30 日以内に迫ったすべての証明書を更新します。通常は自動実行されるため手動実行は不要です。 |
certbot renew --dry-run | 実際に更新せずに更新処理をシミュレーションします。自動更新の設定確認に使用します。 |
certbot certificates | 取得済みの証明書名・ドメイン・有効期限・ファイルパスを一覧表示します。 |
certbot delete --cert-name {証明書名} | 不要になった証明書を削除します。証明書名は certbot certificates で確認できます。 |
certbot --nginx --expand -d {既存ドメイン} -d {追加ドメイン} | 既存の証明書にドメインを追加します。SAN(Subject Alternative Name)を拡張します。 |
certbot revoke --cert-path {証明書パス} | 証明書を失効させます。秘密鍵が漏洩した際などに使用します。 |
使用例
Nginx 向けの証明書取得
# ----------------------------------------------- # snap で certbot をインストールして Nginx の証明書を取得します # ----------------------------------------------- # 既存の apt 版 certbot があれば削除します(競合を防ぐため) sudo apt remove certbot # snap で最新の certbot をインストールします sudo snap install --classic certbot # /usr/bin/certbot としてどこからでも実行できるようにします sudo ln -s /snap/bin/certbot /usr/bin/certbot # 岡部倫太郎のサイト okabe-lab.example.com の証明書を取得します # Nginx の設定ファイルを自動検出して HTTPS 設定も行います sudo certbot --nginx -d okabe-lab.example.com # certbot のバージョンを確認します certbot --version
実行するコマンドは次の通りです。
$ sudo certbot --nginx -d okabe-lab.example.com Saving debug log to /var/log/letsencrypt/letsencrypt.log Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): okabe@example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf Do you agree? (Y)es/(N)o: Y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Account registered. Requesting a certificate for okabe-lab.example.com Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/okabe-lab.example.com/fullchain.pem Key is saved at: /etc/letsencrypt/live/okabe-lab.example.com/privkey.pem This certificate expires on 2026-06-23. These files will be updated when the certificate renews. Deploying certificate to VirtualHost /etc/nginx/sites-enabled/okabe-lab Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/okabe-lab - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled HTTPS on https://okabe-lab.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
自動更新の設定確認と dry-run テスト
# ----------------------------------------------- # 自動更新の設定確認と動作テストを行います # ----------------------------------------------- # snap インストール版は自動的に systemd タイマーが設定されます # タイマーが有効かどうかを確認します sudo systemctl status snap.certbot.renew.timer # cron で管理している場合は crontab を確認します # (/etc/cron.d/certbot にエントリが存在することが多いです) sudo cat /etc/cron.d/certbot # 証明書の一覧と有効期限を確認します sudo certbot certificates # 実際に更新せずに更新処理が成功するかテストします # 自動更新を設定したら必ずこのコマンドで確認してください sudo certbot renew --dry-run
実行するコマンドは次の通りです。
$ sudo certbot certificates
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: okabe-lab.example.com
Serial Number: 03e7a1b2c4d5e6f7a8b9c0d1e2f3a4b5c6d7
Key Type: ECDSA
Domains: okabe-lab.example.com
Expiry Date: 2026-06-23 01:23:45+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/okabe-lab.example.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/okabe-lab.example.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/okabe-lab.example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for okabe-lab.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/okabe-lab.example.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Apache 向けの証明書取得
# ----------------------------------------------- # Apache 向けに証明書を取得します # ----------------------------------------------- # 牧瀬紅莉栖のサイト makise-lab.example.com の証明書を Apache 向けに取得します # certbot が Apache の VirtualHost 設定を自動検出して HTTPS を有効化します sudo certbot --apache -d makise-lab.example.com # 証明書取得後に Apache が正常に動作しているか確認します sudo systemctl status apache2
実行するコマンドは次の通りです。
$ sudo certbot --apache -d makise-lab.example.com Saving debug log to /var/log/letsencrypt/letsencrypt.log Requesting a certificate for makise-lab.example.com Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/makise-lab.example.com/fullchain.pem Key is saved at: /etc/letsencrypt/live/makise-lab.example.com/privkey.pem This certificate expires on 2026-06-23. Deploying certificate to VirtualHost /etc/apache2/sites-enabled/makise-lab.conf Enabling available site: /etc/apache2/sites-enabled/makise-lab-le-ssl.conf Redirecting vhost in /etc/apache2/sites-enabled/makise-lab.conf to ssl vhost in /etc/apache2/sites-enabled/makise-lab-le-ssl.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled HTTPS on https://makise-lab.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
certonly で証明書のみ取得して手動設定する
# -----------------------------------------------
# 証明書ファイルのみ取得して Nginx の設定に手動で組み込みます
# -----------------------------------------------
# 椎名まゆりのサイト shiina-mayuri.example.com の証明書をファイルのみ取得します
# --webroot は既存の Web サーバーを使って認証します(停止不要)
sudo certbot certonly --webroot \
-w /var/www/shiina-mayuri \
-d shiina-mayuri.example.com
# 取得した証明書のパスを確認します
sudo certbot certificates
# /etc/nginx/sites-available/shiina-mayuri.conf に以下を手動で追記します
#
# server {
# listen 443 ssl;
# server_name shiina-mayuri.example.com;
#
# ssl_certificate /etc/letsencrypt/live/shiina-mayuri.example.com/fullchain.pem;
# ssl_certificate_key /etc/letsencrypt/live/shiina-mayuri.example.com/privkey.pem;
#
# root /var/www/shiina-mayuri;
# index index.html;
# }
# 設定を反映します
sudo nginx -t && sudo systemctl reload nginx
実行するコマンドは次の通りです。
$ sudo certbot certonly --webroot -w /var/www/shiina-mayuri -d shiina-mayuri.example.com Saving debug log to /var/log/letsencrypt/letsencrypt.log Requesting a certificate for shiina-mayuri.example.com Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/shiina-mayuri.example.com/fullchain.pem Key is saved at: /etc/letsencrypt/live/shiina-mayuri.example.com/privkey.pem This certificate expires on 2026-06-23. These files will be updated when the certificate renews. $ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
概要
『Let's Encrypt + certbot』は、従来は有料であった SSL/TLS 証明書を無料で取得・管理できる手段として、現在では Web サーバーの HTTPS 化で広く使われています。certbot は Let's Encrypt の ACME プロトコルを実装したクライアントツールであり、証明書の取得からWebサーバーへの設定組み込みまでをほぼ自動化します。証明書の有効期限は 90 日間に設定されており、意図的に短く保つことでセキュリティリスクを低減しています。snap でインストールした場合は snap.certbot.renew.timer という systemd タイマーが自動的に登録され、1日2回の更新チェックが行われます。運用開始後は必ず certbot renew --dry-run でシミュレーションを実施し、自動更新が正しく機能することを確認してください。取得した証明書ファイルは /etc/letsencrypt/live/{ドメイン名}/ に格納されており、fullchain.pem(証明書チェーン)と privkey.pem(秘密鍵)の 2 ファイルをWeb サーバーの設定で参照します。Nginx の設定については Nginx の基本設定 を、Apache の設定については Apache の基本設定 を参照してください。
記事の間違いや著作権の侵害等ございましたらお手数ですがこちらまでご連絡頂ければ幸いです。