LoginSignup
13
12

More than 5 years have passed since last update.

さくらのVPS(Cent OS6.8)でLet's Encryptを自動更新する方法

Last updated at Posted at 2016-07-04

Cent OSのVPSでLet's Encryptに対応させる

Let's Encryptを用いてさくらVPSをHTTPS対応させる - Qiita

こちらに書かれている記事とほとんど同一の方法で対応しました。
バーチャルホストによる対応も一緒です。

Let's Encryptを自動更新させる

Let's EncryptのSSL証明書を自動更新して、結果をメール通知するスクリプトを書いてみた(WEBROOT、NGINX用) - KITA Eng.

更新方法としてはこちらに書いてあるスクリプトを参考に設置してみたものの、動かしてみるとNo package python-virtualenv available.みたいなエラーが出て、動きません。Pythonのバージョン違いが原因ですね。

そのためsclで対応しますが、こちらのコードではsclに対応した書き方ではないため、少し改良したのが以下のコードです。あとnginx用だったので、apache用にも変更してます。

/root/crontab/letsencrypt-renew.sh
#
# Copyright (c) 2016 Katsuya SAITO
#
# @(#) letsencrypt_cert_autoupdate_webroot.sh ver.0.1.0 2016.01.31
#
#######################################################

# CONFIGURE SECTION ---#

CMD_PATH=/home/user/letsencrypt
LOG_FILE=/var/log/letsencrypt/renew.log
CN="domain"
MAIL_TO="root"
today=`date +%F-%a-%T`

echo "# Let's Encrypt Cert autopudate Start: $today" >$LOG_FILE
/etc/init.d/httpd stop
scl enable python27 "$CMD_PATH/letsencrypt-auto certonly --standalone -d $CN --renew-by-default" >>$LOG_FILE
/etc/init.d/httpd start
today=`date +%F-%a-%T`
echo "# Let's Encrypt Cert AutoUpdate End: $today" >>$LOG_FILE

cat -v $LOG_FILE | mail -s "[Let's Encrypt Auto Update] Update Report for $CN" $MAIL_TO

exit 0

ポイントは
scl enable python27 "$CMD_PATH/letsencrypt-auto certonly --standalone -d $CN --renew-by-default"
のところかと思います。
sclをcronで利用するには、
scl enable cmd_name bash
ではなく、
scl enable cmd_name ""
というようにsclで処理を囲んであげる必要があるようです。

更新に成功すると、以下のようなメールが飛んで来ます。

# Let's Encrypt Cert autopudate Start: 2016-07-01-M-eM-^\M-^_-03:00:00
Upgrading certbot-auto 0.8.0 to 0.8.1...
Replacing certbot-auto...
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Version: 1.1-20080819
Version: 1.1-20080819
^[[?1034hIMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/domain-name/fullchain.pem. Your cert will
   expire on 2016-09-30. To obtain a new or tweaked version of this
   certificate in the future, simply run letsencrypt-auto again. To
   non-interactively renew *all* of your certificates, run
   "letsencrypt-auto 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

# Let's Encrypt Cert AutoUpdate End: 2016-07-01-M-eM-^\M-^_-03:01:28

あとはこれをcronで
sh letsencrypt-renew.sh
とかで月一回動かしてあげれば、自動更新されると思います。
複数ドメインをVirtual Hostで運用していても、ドメイン別に用意すればよいので管理が楽になると思います。

0 3 1 * * sh /home/domain1/crontab/letsencrypt-renew.sh
0 3 1 * * sh /home/domain2/crontab/letsencrypt-renew.sh
13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12