【certbot】SSL証明書の作成と更新方法
目次
SSL証明書の作成と更新方法です。
頻度の少ない定期的な作業は忘れやすいので、メモしておこうと思います。
Let's Encryptが発行する無料SSL証明書
証明書には、Let’s Encryptが発行する無料のSSL証明書を利用させていただきます。
Let’s Encryptとは、米国の非営利団体が運営する組織です。
証明書作成には「certbot」というコマンドを使用します。certbotによって、90日間有効な証明書が提供されます。
実行環境
- 実行環境はLightsailのAmazonLinuxです
- 本件ではDNS認証(TXTレコード認証)を用いて証明書を発行します。
- DNS認証はRoute53で行います。
certbot インストール
sudo amazon-linux-extras install epel
sudo yum install certbot python-certbot-apache
証明書作成(DNS認証)
コマンド
sudo certbot --debug --agree-tos \
certonly --manual --preferred-challenges dns \
-d *.yourdomain.com -d yourdomain.com \
-m your-email-address@xxx.com
--debug:デバッグモード実行--agree-tos:諸々賛成します--manual:生成ファイルを手動で配置、設定ファイルも手動で修正--preferred-challenges dns:DNS認証する-d:ドメイン名を指定-m:メールアドレスを指定
実行後
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Requesting a certificate for *.yourdomain.com and yourdomain.com
Performing the following challenges:
dns-01 challenge for yourdomain.com
dns-01 challenge for yourdomain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.yourdomain.com with the following value:
RdiDxyz.....(略)
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue ⭐️まだ押さない!⭐️
Enterを押す前に、Route53でレコードを作成します。
- レコードタイプ:TXTレコード
- レコード名:_acme-challenge.yourdomain.com
- 値:RdiDxyz......(略)
ステータスが INSYNC になってからEnterします。
仮に、INSYNCになる前にEnterするとどうなるかというと
以下のように、注意書きと共にもう一回言われるので同じようにやるだけです。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.yourdomain.com with the following value:
hoRsxyz.....(略)
Before continuing, verify the record is deployed.
(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Route53で、先ほど作成した _acme-challenge.yourdomain.com レコードの値の末尾に改行して追記します。
- レコードタイプ:TXTレコード
- レコード名:_acme-challenge.yourdomain.com
- 値:hoRsxyz.....(略)
ステータスが INSYNC になってからEnterします。
「Congratulations!」と言われたら完了です。
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/yourdomain.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/yourdomain.com/privkey.pem
Your certificate will expire on 2023-05-08. 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"
- 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/<ドメイン名>/cert.pem
- 証明書+中間CA証明書
/etc/letsencrypt/live/<ドメイン名>/fullchain.pem
- 秘密鍵
/etc/letsencrypt/live/<ドメイン名>/privkey.pem
- 中間CA証明書
/etc/letsencrypt/live/<ドメイン名>/chain.pem
これらのファイルは、以下の組み合わせで使用します。
- 証明書、秘密鍵、中間CA証明書
- 証明書+中間CA証明書、秘密鍵
Apacheに証明書を設定する
Apacheのconfファイルを変更して、作成した証明書を設定します。
例えば、VirtualHostの場合は以下のようにします。
<VirtualHost *:443>
# ...他の設定
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
</VirtualHost>
- SSLCertificateFile:証明書+中間CA証明書または証明書のパスを指定
- SSLCertificateKeyFile:秘密鍵のパスを指定
- SSLCertificateChainFile:中間CA証明書のパスを指定
組み合わせに応じて設定項目を変えます。
上記の場合は②証明書+中間CA証明書、秘密鍵のパターンで設定しています。
証明書一覧
証明書と証明書の有効期限は、以下コマンドで確認できます。
sudo certbot certificates
証明書更新
cronとかで定期実行すると楽だが、ここでは手動更新します。
コマンド
sudo certbot \
certonly --manual --preferred-challenges dns \
-d *.yourdomain.com -d yourdomain.com \
-m your-email-address@xxx.com
実行後
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Cert is due for renewal, auto-renewing...
Renewing an existing certificate for *.yourdomain.com and yourdomain.com
Performing the following challenges:
dns-01 challenge for yourdomain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.yourdomain.com with the following value:
S3hH6......(略)
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Route53で_acme-challenge.yourdomain.comの値を改行して S3hH6......(略) を追記して、ステータスが INSYNC になってからEnterします。
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/yourdomain.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/yourdomain.com/privkey.pem
Your certificate will expire on 2023-08-12. 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"
- 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
sudo certbot certificates を実行して確認できます。
「Expiry Date」が更新され、「VALID」になってます。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: yourdomain.com
Serial Number: 3cf15......(略)
Key Type: ...
Domains: *.yourdomain.com yourdomain.com
Expiry Date: 2023-08-12 07:12:08+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/yourdomain.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
期限が切れてしまった場合の証明書更新方法
証明書一覧を確認したとき、「Expiry Date」が過去日付で「INVALID: EXPIRED」となっていたら、有効期限がきれてしまっています。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: yourdomain.com
Serial Number: 3eaa3.....(略)
Key Type: ...
Domains: *.yourdomain.com yourdomain.com
Expiry Date: 2023-05-08 04:34:29+00:00 (INVALID: EXPIRED)
Certificate Path: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/yourdomain.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
この場合、作り直すことになります。
古い証明書データを削除し、改めて証明書作成コマンドを実行します。
作り直しの前準備
rootユーザーに切り替え、期限切れの証明書をバックアップします。
# rootユーザーに切り替え
sudo su -
cd /etc/letsencrypt
# 必要に応じてWebサーバーの停止
service httpd stop
# 期限切れ証明書のバックアップ
rm -rf archive/yourdomain.com.bkup && mv archive/yourdomain.com archive/yourdomain.com.bkup
rm -rf live/yourdomain.com.bkup && mv live/yourdomain.com live/yourdomain.com.bkup
rm -rf renewal/yourdomain.com.conf.bkup && mv renewal/yourdomain.com.conf renewal/yourdomain.com.conf.bkup
証明書作成
証明書作成の際に実行したコマンドを改めて実行します。
sudo certbot --debug --agree-tos \
certonly --manual --preferred-challenges dns \
-d *.yourdomain.com -d yourdomain.com \
-m your-email-address@xxx.com
以降も証明書作成時と同じで、画面の指示に沿ってDNS認証(またはドメイン認証)をすすめます。
証明書の作成ができたら適宜 service httpd start でWebサーバーを起動して完了です。