前幾天發現個人 letsencrypt 通配符證書快過時了,想爲這兩張證書續期(renew)。javascript
首先運行命令查看個人全部證書:php
$ certbot-auto certificates
其中證書名 simplehttps.com 都是須要續期的(renew)。java
接下來運行 renew 命令:git
$ certbot-auto renew --cert-name simplehttps.com
運行出現如圖錯誤:github
意思就是通配符證書申請、續期的時候只能經過 DNS 驗證,而 DNS 添加記錄只能手動,整個 cerbot 操做沒法作到自動化,因此 renew 就失敗了。ruby
那如何解決呢,幸好 cerbot 提供了一個 manual-auth-hook hook,什麼意思呢?能夠編寫一個腳本,由這個腳原本先完成 DNS 驗證,而後再進行 renew。對應的腳本會自動添加 DNS 記錄,從而完成 DNS 校驗,並自動 renew 證書。bash
根據這個解決思路:測試
惋惜的是這兩種腳本都不支持阿里雲的 DNS(個人域名所有由阿里雲 DNS 提供)。ui
爲解決這問題,我寫了個腳本,並放在 github 上,能夠自取。阿里雲
$ git clone "https://github.com/ywdblog/certbot-letencrypt-wildcardcertificates-alydns-au"
建議關注這個 github 倉庫,我會隨時更新
腳本就兩個文件:
首先看下 au.sh 的代碼:
#!/bin/bash PATH=$(cd `dirname $0`; pwd) echo $PATH"/alydns.php" # 調用 PHP 腳本,自動設置 DNS TXT 記錄。 # 第一個參數:須要爲那個域名設置 DNS 記錄 # 第二個參數:須要爲具體那個 RR 設置 # 第三個參數: letsencrypt 動態傳遞的 RR 值 echo $CRETBOT_DOMAIN"_acme-challenge"$CERTBOT_VALIDATION /usr/bin/php $PATH"/alydns.php" $CERTBOT_DOMAIN "_acme-challenge" $CERTBOT_VALIDATION >"/var/log/certdebug.log" # DNS TXT 記錄刷新時間 sleep 20
若是用戶要使用該腳本:
配置好後,執行下列測試命令:
$ certbot-auto renew --cert-name newyingyong.cn --manual-auth-hook /你的腳本目錄/au.sh --dry-run
重要提醒:爲避免遇到操做次數的限制,加入 dry-run 參數,可以避免操做限制,等執行無誤後,再進行真實的renew 操做。
輸出以下:
Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/newyingyong.cn/fullchain.pem (success)
確保無誤後,再進行真實的 renew 操做:
$ certbot-auto renew --cert-name newyingyong.cn --manual-auth-hook /你的腳本目錄/au.sh
證書更新成功後,運行以下命令查看證書:
$ openssl x509 -in /etc/letsencrypt/live/newyingyong.cn/fullchain.pem -noout -text
關鍵輸出以下:
Issuer: C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3 Validity Not Before: Jul 5 21:29:55 2018 GMT Not After : Oct 3 21:29:55 2018 GMT Subject: CN = *.newyingyong.cn
順利完成。