サーバー更改に伴いSSL証明書を移行した
どうもこんばんは。
大層な題名にしていますが、個人で勉強用のAWSの話です。
訳あってAWSのインスタンスを切り替えるのですが、
現状で運用しているSSL証明書をそのまま使用したいと思いました。
1.DNSサーバーの設定値変更
用途が個人の勉強用なので、数時間エラーが出ていても問題ありません。
ですから、先にDNSサーバーの設定値を変更しました。
Route53を使用しているので、対象のドメインのAレコードを変更しました。
エラーが許容できないサイトなら最後にやる手順ですかね?
ここは本題とは離れるので軽く流します。
2./etc/letsencryptをディレクトリごと移行
SSL証明書はLet's Encryptを使用しています。
関連するファイルは全て/etc/letsencryptにあるのでzipにして新しいサーバーへ移動します。
#letsencryptフォルダをzip化
#yオプションでシンボリックリンクを維持したまま圧縮
$ zip -ry ssl.zip /etc/letsencrypt
一度ローカルに落としてからSCPで新側に持っていきました。
#新側のetc配下に解凍
$ unzip ssl.zip -d /etc
3.certbotクライアントのインストール
サーバー証明書の取得・更新をするためにcertbotクライアントをインストールします。
#OSによって変更してください。途中でyを指定。
$ sudo yum install epel-release
$ sudo yum install certbot
nginxを使用しているので設定ファイルに以下を追記します。
#/etc/nginx/conf.d/default.confに以下を追記
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/hogehoge.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/hogehoge.com/privkey.pem;
nginxを再起動してHTTPS通信できるかを確認します。
4.証明書更新テスト
Let's Encryptの証明書は3ヶ月の有効期限があり、定期的に更新する必要があります。
実際に更新できるかどうかをテストします。(--dry-runをつけると更新のテストをしてくれます。)
# certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/hogehoge.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for hogehoge.com
Waiting for verification...
Challenge failed for domain hogehoge.com
http-01 challenge for hogehoge.com
Cleaning up challenges
Attempting to renew cert (hogehoge.com) from /etc/letsencrypt/renewal/hogehoge.com.conf produced an unexpected error: Some challenges have failed.. Skipping.
All renewal attempts failed. The following certs could not be renewed:
/etc/letsencrypt/live/hogehoge.com/fullchain.pem (failure)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)
All renewal attempts failed. The following certs could not be renewed:
/etc/letsencrypt/live/hogehoge.com/fullchain.pem (failure)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 renew failure(s), 0 parse failure(s)
IMPORTANT NOTES:
- The following errors were reported by the server:
Domain: hogehoge.com
Type: unauthorized
Detail: Invalid response from
http://hogehoge.com/.well-known/acme-challenge/GAYsxV0NQPL3BEsaAWVVOO6ha7fxdhk2MffGibmks4
[xx.xxx.xx.xxx]: "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n
<meta charset=\"utf-8\" />\n <title>Action Controller: Exception
caught</title>\n <style"
To fix these errors, please make sure that your domain name was
entered correctly and the DNS A/AAAA record(s) for that domain
contain(s) the right IP address.
上記のようなエラーが出ました。
原因は2つありました。
原因① リバースプロキシ
エラーの中に"http://hogehoge.com/.well-known/acme-challenge/"へのアクセスがあるようなので、リバースプロキシをうまく設定する必要があると考えました。
#/etc/nginx/conf.d/default.confに以下を追記
location ^~ /.well-known/acme-challenge/ {
root /usr/share/nginx/html;
}
とりあえずドキュメントルートを指定。
原因② Apacheとの差分
以前はApacheを使用していました。
その時のドキュメントルートとはパスが異なるので、Let's Encryptの設定値も変更します。
$ vi /etc/letsencrypt/renewal/hogehoge.com.conf
[削除]
hogehoge.com = /var/www/html
[追記]
hogehoge.com = /usr/share/nginx/html
これで再度、証明書更新テストを実施。
# certbot renew --dry-run
末尾に以下のような文言が出ればOK
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/hogehoge.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
本来ならcronで定期的に証明書の更新をする流れですが、今回は省略。
参考
この記事が気に入ったらサポートをしてみませんか?