有馬総一郎のブログ

(彼氏の事情)

2020年11月01日 01:33:36 JST - 4 minute read - Comments - Linux

Let's Encrypt(cerbot)でサーバー(Apache)を止めずに証明書を自動更新する

自分自身はそんなに面倒とは思ってないが、Let’s Encryptの更新3ヶ月、手動で行うというのは面倒な人には辛いかも知れない。

しかし 怠慢こそプログラマーの美徳 と言われる。自動化させようと思ったが、はてな…そもそもapache2停止させないと証明書更新できないんだけど…

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/mydomain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Plugins selected: Authenticator standalone, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for mydomain.com
Cleaning up challenges
Attempting to renew cert (mydomain.com) from /etc/letsencrypt/renewal/mydomain.com.conf produced an unexpected error: Problem binding to port 80:Could not bind to IPv4 or IPv6.. Skipping.
All renewal attempts failed. The following certs could not be renewed:
  /etc/letsencrypt/live/mydomain.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/mydomain.com/fullchain.pem (failure)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: service apache2 restart
1 renew failure(s), 0 parse failure(s)
arimasou16@ubuntu:~$

止めずに行うとunexpected error: Problem binding to port 80:Could not bind to IPv4 or IPv6とか言われちゃう…

standaloneからwebrootに設定変更

調べると、それを解決するのがcertbotのオプションwebrootらしい。しかし、自分はそもそもstandaloneで登録しちゃってるけどな、削除して登録しなきゃいけないのかな?とか思ったら、

Let’s Encryptでstandaloneからwebrootに設定変更する | Apitore blogという先駆者がいた。 更新するドメイン .com.confを編集するだけでstandaloneからwebrootに設定変更できるみたいだ。

ここで問題が…私はこのドメインのサーバーにsubsonic, jpsonic, nextcloudをインストールしてるのだけど、その場合のWebサーバのドキュメントルートディレクトリはどうなるんだろうか。ドメインは同じで、mydomain.com/subsonic mydomain.com/jpsonic mydomain.com/nextcloud となっているのだけど、コンテンツの中身は /var/subsonic /var/jpsonic /var/www/nextcloud にある。

とりあえず/var/www/htmlにしとく。

/etc/letsencrypt/renewal/[更新するドメイン].com.conf

@@ -8,6 +8,9 @@

 # Options used in the renewal process
 [renewalparams]
-authenticator = standalone
+#authenticator = standalone
+authenticator = webroot
+webroot-path = /var/www/html
+wakamoth272.dip.jp = /var/www/html
 account = abc123def456ghi789jkl012mno345pq
 server = https://acme-v02.api.letsencrypt.org/directory

試走

sudo certbot renew --dry-run --webroot-path /var/www/html --post-hook "systemctl reload apache2"で試す。

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/mydomain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for mydomain.com
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/mydomain.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/mydomain.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: systemctl reload apache2

上手く行ってるっぽい。じゃあ、あとは実際に更新して確認すればいいか。

実際に更新してみると、問題なく証明書が更新されている。三つのサイトともに適用されている。

cron設定

証明書を自動更新するためにcronの設定を行う。 r と間違えると危険なcrontabで編集する。また、rootで実行する必要があるので、sudo crontab -eで実行。

0 0 1 * * /usr/bin/certbot renew --webroot-path /var/www/html --post-hook "systemctl reload apache2" > /var/log/certbot.log 2>&1

ログを確認するとちゃんと実行されている。

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/mydomain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/mydomain.com/fullchain.pem expires on 2021-01-28 (skipped)
No renewals were attempted.
No hooks were run.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

今回期限まで30日未満でないので更新されなかったが、次月から更新期限が30日を切ったら自動更新、そしてapache2再起動されるはず。

Tags: Ubuntu Server

Ubuntu 16.04から18.04にアップグレードしたらcertbot-autoが失敗するようになった Ubuntu16.04から18.04にアップグレードしたら、unboundが動かない…

comments powered by Disqus