LightSail(bitnami)環境に複数WordPress設定からのLetsEncryptによるSSL化と自動更新設定の備忘

ちょっと前にスターサーバ(旧WPBlog)のWordPressをAWS-LightSailのWordPressに移行しました。最終的にはサイトをSSL化することができ自己満足ではありますが「完成!!」みたいな感じ終っていたのですが、LetsEncryptは証明書の有効期限が発行後3ヶ月のため2ヶ月に一度はサイトの証明書を更新しないとブラウザだったりクローラーなどに嫌われてしまいます。今回はLetsEncryptの証明書自動更新設定の備忘です

LetsEncryptの証明書発行に関しては下記を参照してください。

証明書の新規発行時のコマンド

証明書発行コマンド1行+証明書と秘密鍵ファイルをコピーするコマンド2行だけでした。

./letsencrypt-auto certonly --webroot -w /opt/bitnami/apps/wordpress/htdocs/ -d blog.life-type.com
sudo cp /etc/letsencrypt/live/blog.life-type.com/fullchain.pem /opt/bitnami/apps/wordpress/conf/certs/server.crt
sudo cp /etc/letsencrypt/live/blog.life-type.com/privkey.pem /opt/bitnami/apps/wordpress/conf/certs/server.key

私の環境はLightSsil(Bitnami)にWordPressをマルチではなく2個の単独サイトをインストールしている環境です。それぞれ別に稼働していますがDBは一緒でヴァーチャルホストでリクエストサイトによりアクセスを分けています。詳しくは下記のブログを参照してください。

文字で表現するとこんな漢字です

LightSail(Bitnami)
┣━WordPress-blog.life-type.com
┗━WordPress-house.life-type.com

証明書更新テスト

ちょっと不安なので証明書の更新テストを実行してコマンドが有効なのか検証してみます。コマンドのパラメータに –dry-run を入れればテストができます。

/root/letsencrypt/letsencrypt-auto renew --force-renewal --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/blog.life-type.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for blog.life-type.com
Using the webroot path /opt/bitnami/apps/wordpress/htdocs for all unmatched domains.
Waiting for verification...
Cleaning up challenges
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/blog.life-type.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/house.life-type.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for house.life-type.com
Using the webroot path /opt/bitnami/apps/wordpress2/htdocs for all unmatched domains.
Waiting for verification...
Cleaning up challenges
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/house.life-type.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** 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/blog.life-type.com/fullchain.pem (success)
  /etc/letsencrypt/live/house.life-type.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.

テストの結果 サブドメイン両方の証明書更新テストが成功したとメッセージが表示されました。

シャルコマンド作成

サブドメインで2個のWordPressが稼動しておりますので証明書も2個更新しなければいけませんが本当に便利ですよね。同時に2個の証明書の更新が完了するので証明書更新コマンド1行と各WordPressのフォルダに証明書と秘密鍵のコピーするコマンド2行 そして最後にApacheを再起動します。

#!/bin/sh
/root/letsencrypt/letsencrypt-auto renew --force-renewal 
cp /etc/letsencrypt/live/blog.life-type.com/fullchain.pem /opt/bitnami/apps/wordpress/conf/certs/server.crt
cp /etc/letsencrypt/live/blog.life-type.com/privkey.pem /opt/bitnami/apps/wordpress/conf/certs/server.key
cp /etc/letsencrypt/live/house.life-type.com/fullchain.pem /opt/bitnami/apps/wordpress2/conf/certs/server.crt
cp /etc/letsencrypt/live/house.life-type.com/privkey.pem /opt/bitnami/apps/wordpress2/conf/certs/server.key
/opt/bitnami/ctlscript.sh restart apache

毎月更新をcronに設定したら動作確認

シャルを作成したらcronに毎月◯日に実行としておけばOKです。あとは実際に更新しているかをブラウザのURL部にある鍵マークで証明書の有効期間を見れば更新されているか確認できます。

タイトルとURLをコピーしました