[Let’s Encrypt] certbot renew でエラー Could not bind TCP port 443 because it is already in use by another process on this system の解決方法

Let’s Encrypt で SSL 証明書の更新コマンド certbot renew で Could not bind TCP port 443 because it is already in use by another process on this system というエラーメッセージが発生したときの解決方法をご紹介します。

certbot renew のエラーメッセージ

certbot renew したところ下記のようなエラーメッセージが発生しました。

$ sudo /usr/bin/certbot renew --renew-hook "/bin/systemctl reload nginx"
[sudo] password for admin: 

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

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/yourdomain.example.com.conf
-------------------------------------------------------------------------------
Cert is due for renewal, auto-renewing...
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
Renewing an existing certificate
Performing the following challenges:
tls-sni-01 challenge for yourdomain.example.com
Cleaning up challenges
Attempting to renew cert from /etc/letsencrypt/renewal/yourdomain.example.com.conf produced an unexpected error: Could not bind TCP port 443 because it is already in use by another process on this system (such as a web server). Please stop the program in question and then try again.. Skipping.

All renewal attempts failed. The following certs could not be renewed:
  /etc/letsencrypt/live/yourdomain.example.com/fullchain.pem (failure)
1 renew failure(s), 0 parse failure(s)

Attempting to renew cert from /etc/letsencrypt/renewal/yourdomain.example.com.conf produced an unexpected error: Could not bind TCP port 443 because it is already in use by another process on this system (such as a web server). と書いてある通り、TCP 443 ポートを他のプロセスが利用しているので cert renew できなかったようです。

エラー無しで certbot renew する方法

正しい certbot renew 対応方法

certbot には –apache や –nginx オプションがあるのでこのオプションを指定して実行すれば、エラー無しで certbot renew できます。

  • –apache Use the Apache plugin for authentication & installation
  • –nginx Use the Nginx plugin for authentication & installation
sudo /usr/bin/certbot renew \
--nginx \
--renew-hook "/bin/systemctl reload nginx"

間違った certbot renew 対応方法

以下のように一瞬 Nginx を stop してから certbot renew しても更新できますが、ダウンタイムが一瞬発生するのであまりオススメしません。

sudo systemctl stop nginx
sudo /usr/bin/certbot renew \
--renew-hook "/bin/systemctl start nginx"

ダウンタイムが発生する問題は –nginx オプションで解決済み

数秒間、ダウンタイムが発生するので、そこだけ少し気になってます。アクセス数の少ない深夜帯に更新すればいいんですが、万が一 Nginx プロセスの start に失敗したら、障害対応をおこなわないといけないので、なるべく起きてる時間帯に自動更新させたいんですよね。

Nginx プロセスを停止させずに certbot renew で Let’s Encrypt の SSL 証明書を更新する方法があれば、どなたか教えてください。

–nginx オプションを付ければエラー無しで Nginx プロセスを停止させずに certbot renew できました。

以上、エラー無く certbot renew したい現場からお送りしました。

参考情報