文書の過去の版を表示しています。
certbot(Let's Encrypt)
certbot
は、Let's Encrypt
でSSL証明書を発行するためのコマンド。
サイトをhttps
化するときに使う。
certbotがインストール済みか確認
yum list installed | grep certbot
- 実行結果
certbot.noarch 1.11.0-1.el7 @epel
上記のようなものが表示されればOK。
何も表示されない場合は、certbot
を自分でインストールする必要がある。
certbotをインストールする
yum install certbot -y
certbotでSSL証明書を発行する
certbotのACMEチャレンジ用にnginxの.confを設定
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
ファイルを開く。
- example.com.conf
location ~* /\.well-known { allow all; }
.conf
ファイルに上記を追加。
certbot
のACMEチャレンジ用に、/.well-known
で始まるパスにアクセスを許可している。
certbot certonly --dry-runコマンドを実行(試験)
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
オプションを付けたコマンドで動作確認をする。
- 実行結果
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 Simulating a certificate request for example.com Performing the following challenges: http-01 challenge for example.com Using the webroot path /home/user1/Dokuwiki/xxx for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - The dry run was successful.
上記のようなメッセージが表示されればOK。
certbot初回実行時
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.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. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: n
以下のように入力する。
certbotの対話モード | |
---|---|
Do you agree? | y |
to share your email address with the Electronic Frontier Foundation | n |
規約に同意する必要がある。
メールアドレスは登録しなくてOK。
certbot certonlyコマンドを実行(本番)
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 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"
上記のようなメッセージが表示されればSSL証明書発行に成功している。
certbot certonly実行後に生成されるファイル
/etc/letsencrypt/live
/etc/letsencrypt/live/
ディレクトリに、example.com
のようなディレクトリが作成される。
/etc/letsencrypt/live/example.com
ディレクトリには、以下4つのファイルが格納されている。
- cert.pem (証明書)
- chain.pem (中間証明書)
- fullchain.pem (
cert.pem
とchain.pem
を連結したもの) - privkey.pem (秘密鍵)
このうち、サイトのhttps
化に使うのは、fullchain.pem
とprivkey.pem
だけ。
また、これらのファイルはシンボリックリンクになっていて、実ファイルは
/etc/letsencrypt/archive
にある。
/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
/etc/letsencrypt/renewal
ディレクトリに、example.com.conf
というファイルが生成される。
.conf
の中には、fullchain.pem
やprivkey.pem
のパスなど設定情報が入っている。
certbotで発行した証明書一覧を確認する方法
certbot certificates
- 実行結果
Certificate Name: example.com Serial Number: xxxxxxxxxx Key Type: RSA Domains: 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
で発行済みのドメイン一覧が表示される。
証明書の有効期限
、fullchain.pem
やprivkey.pem
の保存先などが確認できる。
SSL証明書の発行後、nginxの.confを設定
nginxの.confにsslのポートと証明書のパスを設定
nginxの.confにhttpからhttpsにリダイレクトする設定
certbot certonlyで複数ドメインを指定する方法
certbot certonly --webroot -w /home/user1/xxx -d example.com -d www.example.com --email admin@example.com
複数個の-d ドメイン名
を指定することができる。
certbotの証明書にwwwありドメインを後から追加する方法
nginxの.confを設定
- example.com.conf
server_name example.com;
の部分を
- example.com.conf
server_name example.com www.example.com;
のように変更。
(server_name
にwwwありドメインを追加)
Let's Encrypt
からのACMEチャレンジが必要なので、この時点ではwwwあり
ドメインからwwwなし
ドメインにリダイレクトする設定をしてはいけない。
certbot certonlyコマンドを実行
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。
「wwwありドメイン」から「wwwなしドメイン」へリダイレクト
certbotでSSL証明書を更新する方法
certbot
のSSL証明書は90日で有効期限が切れる。
有効期限はcertbot certificates
コマンドで確認できる。
残り30日を切るとcertbot renew
コマンドで証明書を更新できるようになる。
certbot renew
コマンド1回で、certbot certificates
に表示される複数の証明書を一括更新できる。
certbot renew --dry-run
certbot renew --dry-run
- 実行結果
Congratulations, all simulated renewals succeeded:
certbot renew –dry-run
コマンドで、certbot renew
コマンドの予行演習ができる。
certbot certonly
やcertbot renew
コマンドは、1時間に5回程度までと回数制限がある。
–dry-run
オプションを付けると回数制限の対象外。
証明書は発行・更新されないが、エラーにならずにコマンド実行できるか確認できる。
ここでエラーになる場合は、
- コマンドの使い方が間違っていないか?
- ディレクトリのパーミッションは適切か?
- nginxの.confの設定が間違っていないか?
などを確認する。
certbot renew
certbot renew
–dry-run
オプション付きでコマンド実行し、問題なければオプションなしでcertbot renew
を実行。
cronでcertbot renewを定期実行する方法
cronサービスの有無を確認
systemctl status crond
crond
サービスが起動していることを確認する。
crontabに実験用のジョブを登録
crontab -e
crontab -e
によりvi
が起動する。
- crontab
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
の中身を確認。
- cron.log
Congratulations, all simulated renewals succeeded:
のような表示があれば、cron
を使ったcertbot renew
の実行に成功している。
crontabに本番用のジョブを登録
crontab -e
実験用のジョブを下記のように書き換え。
- crontab
2 4 * * 1 certbot renew > /tmp/cron.certbot.log
毎週月曜日
、4時2分
に
certbot renew
を実行。
–dry-run
オプションを削除した。
時刻は各自の状況に合わせて変更、サーバーの負荷が軽そうな時間帯を選ぶ。
週に1回実行する。
(証明書の更新は30日よりも短い周期ならばOKだが、処理に失敗する場合を考えて、短めの周期にした方がいい)
certbotで発行したSSL証明書を失効・削除する方法
certbot revokeでSSL証明書を失効・削除させる
certbot revoke
コマンドにより証明書を失効させる。
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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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
を選択。
- /etc/letsencrypt/archive
- /etc/letsencrypt/live
- /etc/letsencrypt/renewal
コマンド実行により、上記のディレクトリからexample.com
関連のファイルが削除される。
SSL証明書の手動削除
certbot revokeに失敗
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.
証明書の関連ファイルを手動で削除
- /etc/letsencrypt/archive
- /etc/letsencrypt/live
- /etc/letsencrypt/renewal
上記の各ディレクトリにある、example.comのようなディレクトリをrmコマンドで削除する。
ディレクトリの手動削除後、certbot certificates
コマンドを実行すると、一覧から証明書・ドメインの情報が消えている。
サーバー移転時にSSL証明書を移行する方法
新サーバーでcertbot certonly
コマンドを使いSSL証明書を発行したい。
しかし、DNSを切り替えるまでは、新サーバーでcertbot certonly
が使えない。
そこで、旧サーバーから新サーバーへSSL証明書の移行作業が必要になる。
旧サーバーでの作業
fullchain.pemとprivkey.pemの本体を確認
cd /etc/letsencrypt/live/example.com ls -la
- 実行結果
lrwxrwxrwx 1 root root 42 7月 1 18:28 cert.pem -> ../../archive/example.com/cert1.pem lrwxrwxrwx 1 root root 43 7月 1 18:28 chain.pem -> ../../archive/example.com/chain1.pem lrwxrwxrwx 1 root root 47 7月 1 18:28 fullchain.pem -> ../../archive/example.com/fullchain1.pem lrwxrwxrwx 1 root root 45 7月 1 18:28 privkey.pem -> ../../archive/example.com/privkey1.pem
fullchain.pem
の本体は/etc/letsencrypt/archive/example.com/fullchain1.pem
privkey.pem
の本体は/etc/letsencrypt/archive/example.com/privkey1.pem
ということが分かる。
fullchain1.pem
なのかfullchain2.pem
なのか、数字の部分は環境によって変わる。
(certbot renew
のたびに連番が増えていく)
fullchain.pemとprivkey.pemを新サーバーに転送
fullchain.pem
とprivkey.pem
の本体を新サーバーにファイル転送する。
新サーバーでの作業
fullchain.pemとprivkey.pemをtmpディレクトリに保存
cd /home/user1 mkdir tmp mv fullchain1.pem tmp mv privkey1.pem tmp
/home/user1/tmp
ディレクトリを作成。
旧サーバーで取得したfullchain.pem
とprivkey.pem
をtmp
ディレクトリに保管。
nginxの.confを設定
- example.com.conf
ssl_certificate /home/user1/tmp/fullchain1.pem; ssl_certificate_key /home/user1/tmp/privkey1.pem;
ssl_certificate
とssl_certificate_key
に旧サーバーからコピーしてきたfullchain.pem
とprivkey.pem
のパスを設定。
WordPress
なりDokuWiki
なりのデータを正常に移行して、nginx
の設定が適切に行われていれば、
ssl_certificate
とssl_certificate_key
に旧サーバーから持ってきた証明書を指定するだけで正常に稼働する。
certbot certificatesを実行
certbot certificates
新サーバーで管理中のSSL証明書の一覧を表示。
fullchain.pem
とprivkey.pem
を手動で移設しても、certbot certificates
の一覧にドメインは表示されない。
このままでは、certbot renew
をしても、証明書の自動更新の対象外。
certbot certonlyを実行
旧サーバーからコピーしてきたSSL証明書は一時的なもの。
DNSの設定を変更し、ドメインのAレコードが旧サーバーから新サーバーへ切り替わったら、新サーバーにて新たにSSL証明書を発行する。
certbot certonly --webroot -w /home/user1/Dokuwiki/xxx -d example.com --email admin@example.com --dry-run
certbot certonly --webroot -w /home/user1/Dokuwiki/xxx -d example.com --email admin@example.com
certbot certificatesを実行
certbot certificates
certbot
で管理中のSSL証明書の一覧を表示。
example.com
が管理対象に追加されたことを確認。
SSL証明書を確認
以下のディレクトリに関連ファイルが生成されていることを確認。
- /etc/letsencrypt/archive
- /etc/letsencrypt/live
- /etc/letsencrypt/renewal
nginxの.confを修正
- example.com.conf
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_certificate
とsl_certificate_key
の値を、
旧サーバーから移転したものから、
新サーバーのcertbot certonly
コマンドで発行したfullchain.pem
とprivkey.pem
に変更。
tmpの古いSSL証明書を削除
certbot certonly
で新しいSSL証明書の発行が完了後、
/home/user1/tmp
のfullchain.pem
とprivkey.pem
は削除する。
あとは、crontab
にcertbot renew
を登録するなど、通常のサーバー運営の要領で管理すればOK。
certbot renewに失敗するときに確認すること
webroot_pathは正しいか?
certbotコマンドで証明書を発行すると、
/etc/letsencrypt/renewal/example.com.conf
のようなファイルに設定が書き込まれる。
example.com.conf
を開くと
webroot_path = /home/user1/Dokuwiki/aaa, [[webroot_map]] example.com = /home/user1/Dokuwiki/aaa
のような箇所がある。
証明書発行後に、webrootのパスを変更した場合は、
/etc/letsencrypt/renewal/example.com.conf
を開き、
webroot_path = /home/user1/Dokuwiki/aaa,
example.com = /home/user1/Dokuwiki/aaa
の部分を、正しいパスに設定し直す必要がある。