certbotは、Let's EncryptでSSL証明書を発行するためのコマンド
サイトをhttps化するときに使う
yum list installed | grep certbot
certbot.noarch 1.11.0-1.el7 @epel
上記のようなものが表示されればOK
何も表示されない場合は、certbotを自分でインストールする必要がある
yum install certbot -y
certbotコマンドでSSL証明書を発行する際に、Let's Encrypt(証明書の発行元)から
http://example.com/.well-known/acme-challenge/xxxxxxxxxx
のようなURLにアクセスが発生する
外部から/.well-known/acme-challenge/xxxxxxxxxxにアクセスできるようにnginxの.confを設定する
vi /etc/nginx/conf.d/example.com.conf
.confファイルを開く
location ~* /\.well-known {
allow all;
}
.confファイルに上記を追加
certbotのACMEチャレンジ用に、/.well-knownで始まるパスにアクセスを許可している
certbot certonly --webroot -w /home/user1/xxx -d example.com --email admin@example.com --dry-run
/home/user1/xxxはドキュメントルート
nginxのexample.com.confのrootに設定した値
example.comはサイトのドメイン
nginxのexample.com.confのserver_nameに設定した値
admin@example.comは管理者のメールアドレス
Let's Encryptからこのアドレス宛に「証明書の有効期限が迫っています」みたいなメールが定期的に届く
certbotコマンドは1時間に5回程度と回数制限がある
オプションの指定ミスや、nginxの設定ミスでコマンドの失敗を繰り返すうちに回数制限を超える可能性がある
–dry-runは動作試験用のオプション
回数制限の対象外なので、まずは–dry-runオプションを付けたコマンドで動作確認をする
IMPORTANT NOTES: - The dry run was successful.
上記のようなメッセージが表示されればOK
certbot certonlyコマンドの初回実行時は、対話モードが始まる
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator webroot, Installer None Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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
Do you agree?(規約に同意しますか?)と聞かれるので
yを入力する
–dry-runオプションを外して、先ほどと同じコマンドを実行
certbot certonly --webroot -w /home/user1/xxx -d example.com --email admin@example.com
コマンド実行中に
/home/user1/xxx/.well-known/acme-challenge/xxxxxxxxxx
のようなファイルが作成される
また、Let's Encryptから
http://example.com/.well-known/acme-challenge/xxxxxxxxxx
へアクセスが発生する
このアクセスに成功すれば、自分がドメイン所有者だと証明したことになる
コマンドの実行完了後、
/home/user1/xxx/.well-known/acme-challenge/xxxxxxxxxx
は自動的に削除される
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your certificate will expire on 2023-08-17. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew"
上記のようなメッセージが表示されればSSL証明書発行に成功している
certbot certonlyコマンドの初回実行時は、対話モードが始まる
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator webroot, Installer None Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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:
Do you agree?(規約に同意しますか?)と聞かれるので
yを入力する
Would you be willing (中略) to
share your email address with the Electronic Frontier Foundation
(Electronic Frontier Foundationとメールアドレスを共有してもいいですか?)と聞かれるので
nを入力する
メールアドレスの共有は任意なので、yesでもnoでもどちらでもいい
/etc/letsencrypt/live/ディレクトリに、example.comのようなディレクトリが作成される
/etc/letsencrypt/live/example.comディレクトリには、以下4つのファイルが格納されている
cert.pemとchain.pemを連結したもの)
このうち、サイトのhttps化に使うのは、fullchain.pemとprivkey.pemだけ
また、これらのファイルはシンボリックリンクになっていて、実ファイルは以下のパスにある
/etc/letsencrypt/archive
/etc/letsencrypt/archiveディレクトリに、example.comのようなディレクトリが作成される
/etc/letsencrypt/archive/example.com/ディレクトリには
fullchain.pemやprivkey.pemの本体が
fullchain1.pemやprivkey1.pemのようなファイル名で保管されている
certbot renewで証明書を更新すると
fullchain2.pemやprivkey2.pemのように連番で新しいファイルが作成され
archiveには古い証明書が履歴として蓄積される
/etc/letsencrypt/renewalディレクトリに、example.com.confというファイルが生成される
.confの中には、fullchain.pemやprivkey.pemのパスなど設定情報が入っている
certbot certificates
Certificate Name: example.com
Serial Number: xxxxxxxxxx
Key Type: RSA
Domains: example.com
Expiry Date: 2023-10-1 05:35:57+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem
certbotで発行済みのドメイン一覧が表示される
証明書の有効期限、fullchain.pemやprivkey.pemの保存先などが確認できる
certbot certonly --webroot -w /home/user1/xxx -d example.com -d www.example.com --email admin@example.com
-d ドメイン名オプションで複数個のドメインを指定できる
server_name example.com;
の部分を
server_name example.com www.example.com;
のように変更
(server_nameにwwwありドメインを追加)
Let's EncryptからのACMEチャレンジが必要なので、この時点ではwwwありドメインからwwwなしドメインにリダイレクトする設定をしてはいけない
certbot certonly --webroot -w /home/user1/xxx -d example.com -d www.example.com
-dオプションで、wwwなしとwwwありの2種類のドメインを指定する
You have an existing certificate that contains a portion of the domains you requested (ref: /etc/letsencrypt/renewal/example.com.conf) It contains these names: example.com You requested these names for the new certificate: example.com, www.example.com. Do you want to expand and replace this existing certificate with the new certificate? (E)xpand/(C)ancel:
上記のような対話メッセージが表示されるので、eを入力
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your certificate will expire on 2021-10-1. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew"
上記のようなメッセージが表示されればOK
certbot certificates
Certificate Name: example.com
Serial Number: xxxxxxxxxx
Key Type: RSA
Domains: example.com www.example.com
Expiry Date: 2021-10-1 05:35:57+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem
certbot certificatesコマンドの実行結果で、Domainsの欄にexample.com www.example.comのように2つのドメインが表示されていればOK
certbotのSSL証明書は90日で有効期限が切れる
有効期限はcertbot certificatesコマンドで確認できる
残り30日を切るとcertbot renewコマンドで証明書を更新できるようになる
certbot renewコマンド1回で、certbot certificatesに表示される複数の証明書を一括更新できる
certbot renew --dry-run
Congratulations, all simulated renewals succeeded:
certbot renew –dry-runコマンドで、certbot renewコマンドの予行演習ができる
certbot certonlyやcertbot renewコマンドは、1時間に5回程度までと回数制限がある
–dry-runオプションを付けると回数制限の対象外
証明書は発行・更新されないが、エラーにならずにコマンド実行できるか確認できる
ここでエラーになる場合は
などを確認する
certbot renew
–dry-runオプション付きでコマンド実行し、問題なければオプションなしでcertbot renewを実行
systemctl status crond
crondサービスが起動していることを確認する
crontab -e
crontab -eによりviが起動する
1 9 * * * certbot renew --dry-run > /tmp/cron.certbot.log
上記のような設定を書き込みcrontabを保存
毎日9時1分に
certbot renew –dry-runを実行、
/tmp/cron.certbot.logに実行結果を保存
時刻は現在時刻の5分後くらいに設定する
vi /tmp/cron.certbot.log
指定時刻を過ぎたら/tmp/cron.certbot.logの中身を確認
Congratulations, all simulated renewals succeeded:
のような表示があれば、cronを使ったcertbot renew –dry-runの実行に成功している
crontab -e
実験用のジョブを下記のように書き換え
2 4 * * 1 certbot renew > /tmp/cron.certbot.log
毎週月曜日、4時2分に
certbot renewを実行
–dry-runオプションを削除した
時刻は各自の状況に合わせて変更、サーバーの負荷が軽そうな時間帯を選ぶ
週に1回実行する
証明書の有効期限が残り30日未満になるとcertbot renewを実行したときに新しい証明書が発行されるので
証明書の更新は30日よりも短い周期にする
処理に失敗する場合を考えて、30日ジャストよりは、少し短めの周期にした方がいい
30日周期にして、万が一処理に失敗した場合は、次に実行されるのはさらに30日後なので、計60日間は証明書を更新しないことになる
それだと証明書の有効期限が切れる恐れがある
yum update --enablerepo=epel certbot
古いバージョンのcertbotでは証明書のデータを削除する際に一部のデータが自動的に削除できなかいことがある
作業前にcertbotを最新のバージョンにしておくことをすすめる
certbot revokeコマンドにより証明書を失効させる
certbot revoke --cert-path /etc/letsencrypt/live/example.com/fullchain.pem
Saving debug log to /var/log/letsencrypt/letsencrypt.log Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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: * example.com Are you sure you want to delete the above certificate(s)? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: y Deleted all files relating to certificate example.com. Congratulations! You have successfully revoked the certificate that was located at /etc/letsencrypt/live/example.com/fullchain.pem.
Would you like to delete the certificate(s) you just revoked, along with all
earlier and later versions of the certificate?
証明書を削除してもいいか聞かれるのでyを選択
Are you sure you want to delete the above certificate(s)?
本当に証明書を削除してもいいか聞かれるのでyを選択
コマンド実行により、上記の2つのディレクトリと
上記の1つのファイルが自動的に削除される
certbot revoke --cert-path /etc/letsencrypt/live/example.com/fullchain.pem
証明書の有効期限が切れてしまうと、certbot revokeコマンドで失効させようとしても下記のようなエラーとなる
Saving debug log to /var/log/letsencrypt/letsencrypt.log Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org An unexpected error occurred: The client lacks sufficient authorization :: Certificate is expired Please see the logfiles in /var/log/letsencrypt for more details.
上記の2つのディレクトリを rm -rf コマンドで削除する
上記のファイルも rm -f コマンドで削除する
上記3つの手動削除後、certbot certificatesコマンドを実行すると、一覧から証明書・ドメインの情報が消えている
certbotコマンドで証明書を発行すると、
/etc/letsencrypt/renewal/example.com.conf
のようなファイルに設定が書き込まれる
example.com.confを開くと
webroot_path = /home/user1/Dokuwiki/xxx, [[webroot_map]] example.com = /home/user1/Dokuwiki/xxx
のような箇所がある
証明書発行後に、webrootのパスを変更した場合は、
/etc/letsencrypt/renewal/example.com.conf
を開き、
webroot_path = /home/user1/Dokuwiki/xxx,example.com = /home/user1/Dokuwiki/xxxの部分を、正しいパスに設定し直す必要がある