文書の過去の版を表示しています。
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
の部分を、正しいパスに設定し直す必要がある。