久しぶりにブログに何か書こうと思ってサイトを開いたら、保護されていない通信であるとChromeさんに心配されました。

調べてみたところ、Let’s EncryptのSSL証明書の期限が切れてしまっているとのこと。

アクセス数がガクッと落ちていてクソワロいました。元から大してないですけど。
ということで、SSL証明書の更新をしていきます。
当サイトの環境はAWSのEC2インスタンス上でWordpressサーバーを稼働させていますが、バックエンドの環境が何であろうが、更新の作業(コマンド等)はそれほど差異はないかと思います。
ASW EC2インスタンスにSSH接続
AWSのインスタンス一覧を開き、”接続”ボタンを押した時に表示される通りにすればつながります。(”インスタンスの作成”ボタンの隣)
# EC2インスタンスにSSH接続
$ ssh -i [/PATH/of/your/pemfile] ubuntu@[your public DNS]
SSL証明書の更新
まずはSSL証明書の期限を確認しましょう。
# PATHが通っているのであれば
$ sudo certbot certificates
# 私のようにPATHを通していなければ
$ sudo /usr/bin/certbot-auto certificates
#実行結果
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: www.so-hack.com
Domains: www.so-hack.com
Expiry Date: 2019-08-28 06:37:39+00:00 (INVALID: EXPIRED)
Certificate Path: /etc/letsencrypt/live/www.so-hack.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/www.so-hack.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ということで、8月28で切れてしまっています。
次に更新をしていくのですが、更新のコマンドにオプション`–dry-run`をつけることでリハーサルを行うことができます。そこでエラーが出ないことを確認したら、本番の更新作業を行います。
# 更新のリハーサル
$ sudo /usr/bin/certbot-auto renew --dry-run
...
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/www.so-hack.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
...
`Congratulations, all renewals succeeded. The following certs have been renewed:`と出ていれば良いです。
# 更新
$ sudo /usr/bin/certbot-auto renew
同じように`Congratu~`と出れば更新が完了です。
# 更新の確認
$ sudo /usr/bin/certbot-auto certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: www.so-hack.com
Domains: www.so-hack.com
Expiry Date: 2019-12-19 08:22:27+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/www.so-hack.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/www.so-hack.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3か月延命されました。やったね。
Apacheサーバーに反映
あとは、Wordpressを動かしているApacheサーバーを再起動して反映させてあげれば完了と言いたいところなのですが、新しく発行した証明書をbitnamiのapache配下に更新してあげる必要があります。ここ、ややつまづきポイント。
$ sudo cp /etc/letsencrypt/live/[your domain]/fullchain.pem /opt/bitnami/apache2/conf/server.crt
$ sudo cp /etc/letsencrypt/live/[your domain]/privkey.pem /opt/bitnami/apache2/conf/server.key
ここがどんな構成でWordpressを運営しているかによって若干変わってきます。
最後にApacheサーバーの再起動をします。私はAWSのbitnami-wordpressというマシンイメージ(AMI)を使っているのでApacheサーバーの再起動はbitnamiのコマンドを使います。
# Apacheサーバーの再起動
$ sudo /etc/init.d/bitnami restart apache
Unmonitored apache
Syntax OK
/opt/bitnami/apache2/scripts/ctl.sh : httpd stopped
Syntax OK
/opt/bitnami/apache2/scripts/ctl.sh : httpd started at port 80
Monitored apache

余談
3か月後にはまた期限が切れるわけですが、更新作業を自動化するためにはLinuxのcronを設定してあげる必要があるみたいです。
cronはスクリプトを自動実行するためのデーモンプロセスで、crontabファイルを編集することで自動実行をしてくれます。
ちなみにこのデーモンは”Daemon”なので守護神という意味だと思うのですが。

自動化はとりあえず3ヶ月後にやろうと思います。