2024-05-29

[Certbot] ImportError: cannot import name 'appengine' from 'urllib3.contrib' ...

*Certbot 是一個由 Let's Encrypt 的組織維護的一個用來申請加密憑證的工具

之前公司網站的 https 憑證是透過 Certbot 的來輔助申請使用的,公司網站的系統是 Ubuntu 18.04 LTS ,在安裝好 Certbot 後,它會在 /etc/cron.d 中增加一個 certbot 的排程,定期是每天的「0 */12 * * *」會執行 renew 檢測操作

憑證一般申請後的有效期是三個月,但一般應該是可以提前幾天就讓你 renew 展延

而最近我會發現異常是因為在五月初收到來自 Let's Encrypt Expir 寄過來告知還有 xx 天就要過期了,要記得去 renew ,當時我就覺得怪怪的,因為公司這個憑證已經運行有幾年了,我還是第一次收到通知信,不過當時我還有別的事在忙,想說因為有排程會去自動 renew 也就沒有多想

直到前幾天我又收到同一封告知即將要過期的信,我才認真的去檢查

一開始先查看是不是 /etc/cron.d/certbot 是不是被刪或裡面的排程設定被動過了,但檢查後覺得是正常的,再來就是去檢查排程執行時所記錄的 syslog ,在查看「grep -i "certbot" /var/log/syslog」後發現,certbot 的排程執行時有異常,大至的訊息如下

systemd[1]: Starting Certbot
certbot[...]: Traceback (most recent call last):
certbot[...]:  File "/usr/lib/python3/dist-packages/requests_toolbelt/_compat.py"....
...
...
...
certbot[...]: ImportError: cannot import name 'appengine' from 'urllib3.contrib' ...
systemd[1]: certbot.service: Main process exited, code=exited, status=1/FAILURE
systemd[1]: certbot.service: Failed with result 'exit-code'.
systemd[1]: Failed to start Certbot

在 google 之後,大部份的建議都是將 python library 中的 urllib3 給移除就可以了,然後執行「sudo pip uninstall urllib3」,再輸入 y 確定要移除
*用 sudo 是因為 certbot 是以 root 身份執行的,所以 urllib3 的套件是安裝在 root 的 python library 之下

然後再手動讓 certbot 跑 renew 就沒有出現上述的異常訊息,而且憑證也更新了
*手動 renew 指定「sudo certbot -q renew」

沒有留言: