無料のSSLサーバ証明書を使用して、ホームページの常時SSL化を図ります。前提条件として、apacheによるホームページが公開されていることです。apacheに関する設定はこちらを参考に準備ください。ブラウザから http://ホスト名.dassyi.com/ としてホームページ内容が見れることを確認しておきます。
自動でサーバ証明書を発行するcertbotのインストール
では、早速 certbotをインストールしていきます。以下のコマンドを実行してください。もしcertbotがないと言われたら、epel-releaseのリポジトリを追加してください。
# dnf install certbot
なお、Snap版のcertbotをインストールする方法もありますが、たくさんのパッケージを必要とするためディスクの容量に影響があるのと、セキュリティの観点から不要なパッケージ、アプリはインストールしないようにするため、epelからインストールしております。
snapとは、liinuxのディストリビューションを問わず利用できる「ユニバーサルパッケージ」で、独立性が高いのが特徴です。要はOSのライブラリのバージョン等の影響は受けませんが、その分必要な独自のパッケージを追加するので、ディスクの使用容量が大きくなる傾向があります。
無料のSSLサーバ証明書を発行する
今回は、証明書を取得または更新するために、「certonly」オプションを使用します。次に、「–webroot」オプションをつけます。こちらは、証明書発行時の認証のためにサーバーのwebrootフォルダーに認証ファイルを配置することを意味します。また、「-w」オプションでは、具体的な認証ファイルの配置先のディレクトリ(外部から閲覧できる領域)を指定します。
# certbot certonly --webroot -w /var/www/html -d ホスト名.dassyi.com
すると、メールアドレスの入力を促されますので、メールアドレスを入力します。メールアドレスは、緊急の通知、鍵を紛失したときの復旧、証明書の有効期限が近付いた場合の通知に使用されるようです。
次に、Let’s Encrypt の利用規約に同意します。(「Yes]を入力)
その次に、Let’s Encryptパートナーにメールアドレスを公開していいか聞かれるので、「No」を入力します。
この後、実際にサーバにアクセスされて確認しますが、私はFirewalldでガチガチにアクセス制限していたので、失敗しました。ログを確認すると、3つほど80番ポートにアクセスがあり、awsから2つと、letsencrypt.orgから1つアクセスがありました。このうち2つはブラックリストとして登録されておりました。。。(大丈夫か?)
とはいえ、アクセスできないと証明書が発行できないので、いったんfirewalldを停止して、上記コマンドを実行すると、以下の通り成功しました。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for aws.dassyi.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/aws.dassyi.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/aws.dassyi.com/privkey.pem
This certificate expires on 2023-02-15.
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
取得した証明書をapacheに設定しSSL対応を行う
証明書が発行されましたら、ssl.confにLet’sEncryptで発行される証明書とキーのパスを記載します。
SSLCertificateFile /etc/letsencrypt/live/aws.dassyi.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/aws.dassyi.com/privkey.pem
apacheのサービスを再起動して、ブラウザで httpsでアクセスしてみます。
エラーなく表示されていることを確認します。また証明書の中身を確認し、Let’s encryptになっている事と、有効期限が3か月である事を確認します。
証明書の自動更新を行う設定
ここでは発行したSSLサーバ証明書を3か月ごとに自動更新させるために、cronにて自動更新シェルを設定します。
まず、以下のような自動更新シェル(ssl_renew.sh)を作成し、/usr/local/sbin に配置します。
#!/bin/bash
CMD=/usr/bin/certbot
# シスログへ開始を記録
/usr/bin/logger "`date` SSL Certfile Update start"
# 更新前にfirewalldを停止
/usr/bin/systemctl stop firewalld
# certbotコマンドで証明書を更新。更新後apacheを再読み込み
${CMD} renew --post-hook "systemctl reload httpd"
# irewalldを起動
/usr/bin/systemctl start firewalld
# シスログに終了を記録
/usr/bin/logger "`date` SSL Certfile Update end"
このファイルのパーミッションを 700 にして、以下の通りrootユーザにてcrontabに仕掛けます。
30 21 10,20 * * /usr/local/sbin/ssl_renew.sh
こちらは、毎月10日と20日の21時30分に実行する意味になります。
取得した証明書の有効期限が、残り30日未満にならないと更新されないので、しばらく様子見です。
結果は、また報告します。
約2か月後
上記の設定を11月20日頃に実施して、約二か月経過した1月20日のcronで、自動更新されました。
管理者宛てにもメールが届き、更新されなかった場合は、
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/aws.dassyi.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Certificate not yet due for renewal - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - The following certificates are not due for renewal yet: /etc/letsencrypt/live/aws.dassyi.com/fullchain.pem expires on 2023-02-15 (skipped) No renewals were attempted. No hooks were run. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Saving debug log to /var/log/letsencrypt/letsencrypt.log
のようなメールでしたが、更新された際のメールは、
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/aws.dassyi.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Renewing an existing certificate for aws.dassyi.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations, all renewals succeeded: /etc/letsencrypt/live/aws.dassyi.com/fullchain.pem (success) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Saving debug log to /var/log/letsencrypt/letsencrypt.log
という内容に変わりました。もちろんブラウザから証明書の有効期限が更新されていることも確認済です。
—— 以下は、2025年11月に追記しました ——
DNSのTXTレコードを利用して証明書を発行する方法を試す
Let’s encrypt では証明書発行時にwebサーバが必要です。
(webサーバがなくても、簡易的なwebサーバを起動して取得可(standaloneモード))
セキュリティの観点から、TCP 80 443を開放したくなかったので、DNSのTXTレコードを利用して証明書を発行する方法を試してみました。
# certbot certonly --manual -d dassyi.com --preferred-challenges dns
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): info@dassyi.com ←メールアドレスを入力
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
hxxps://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y ←同意するので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: N ←メルマガの送付は不要なのでNを入力
Account registered.
Requesting a certificate for dassyi.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:
_acme-challenge.dassyi.com.
with the following value:
N3cxxxxxxxxxxxxxxxxxxxxxxxs5M
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.dassyi.com.
Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue ←これが表示されたら、DNSにTXTレコードを追加し、Enter。
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/dassyi.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/dassyi.com/privkey.pem
This certificate expires on 2026-02-10.
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/etc/letsencrypt/live/ドメイン名/配下に4つのファイルが生成されます。
cert.pem : SSL証明書
chain.pem : 中間証明書
fullchain.pem : SSL証明書と中間証明書を連結したファイル
privkey.pem : 秘密鍵
これをpostfixに設定すると問題なく利用できました。しかし、更新を実施すると、
# certbot renew --deploy-hook "sleep 10m;systemctl reload postfix"
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/dassyi.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Failed to renew certificate dassyi.com with error: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.')
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
All renewals failed. The following certificates could not be renewed:
/etc/letsencrypt/live/dassyi.com/fullchain.pem (failure)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 renew failure(s), 0 parse failure(s)
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.
と表示されてエラー。–manual-auth-hookのオプションを使用して、DNSのTXTレコードを自動で修正するプラグインを稼働させる必要があります。bindを使用しているので、有効なプラグインがないため、この方法は諦めることにしました。
Let’s encrypt証明書の失効方法
以下のコマンドで失効ができます。
# certbot revoke --cert-path /etc/letsencrypt/archive/dassyi.com/cert1.pem --reason keycompromise
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you like to delete the certificate(s) you just revoked, along with all
earlier and later versions of the certificate?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es (recommended)/(N)o: Y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certificate(s) are selected for deletion:
* dassyi.com
WARNING: Before continuing, ensure that the listed certificates are not being
used by any installed server software (e.g. Apache, nginx, mail servers).
Deleting a certificate that is still being used will cause the server software
to stop working. See https://certbot.org/deleting-certs for information on
deleting certificates safely.
Are you sure you want to delete the above certificate(s)?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Deleted all files relating to certificate dassyi.com.
Congratulations! You have successfully revoked the certificate that was located at /etc/letsencrypt/archive/dassyi.com/cert1.pem.
standaloneモードで発行
まずは、Firewallを停止してからstandaloneモードで証明書を発行します。発行が終了しましたら、Firewallを起動します。
# systemctl stop firewalld
#
# certbot certonly --standalone -d dassyi.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for dassyi.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/dassyi.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/dassyi.com/privkey.pem
This certificate expires on 2026-02-11.
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# systemctl start firewalld
更新前にfirewallを停止し、更新後にfirewallを起動、postfixを再読み込みする設定をcronに行います。
# crontab -l
0 9 * * * certbot renew --pre-hook "systemctl stop firewalld" --deploy-hook "sleep 1m;systemctl start firewalld;systemctl reload postfix" > /dev/null 2>&1
TCP 80 443を開放する必要がありますが、必要最低限の期間に留め、これで無事更新されることを確認しました。
今回はここまです。最後までお読みいただきありがとうございました。