前言

最近我收到了來自 Let’s encrypt 的通知
說是我的 SSL 證書快要到期了

我心想那有可能啊
我一直使用 cronjob 來更新證書都沒有出過問題啊

然後手動跑一次證書更新指令
我的伺服器竟然給 Let’s encrypt 的驗證
給了一個 403 (沒有權限)
好家伙
這是自我放棄身份了

最後發現是我在攔截機械人攻擊的時候
把 Let’s encrypt 的驗證都給攔截了

想知道我是怎樣攔截機械人攻擊的話
可以到下面這篇來看看

要解決這個問題也很簡單
從 HTTP challenge 換為 DNS challenge
把驗證的責任交給 DNS 就好


事前準備

設置 name server

HTTP challenge 就是直接向伺服器發送 HTTP 請求
並驗證 HTTP response

DNS challenge 就是向 DNS 伺服器查問
由 DNS 伺服器進行驗證

所以在使用 Cloudflare DNS 驗證前
你需要用 Cloudflare DNS 作為你的域名(domain) 的 name server
詳細做法是每家域名注册商(domain name registrar)都不一樣
不過都是大同小異的
可以參考這篇【2025】Cloudflare 教學:3 步驟免費加速網站,提升安全性

 

申請 API Token

然後你就要到 Cloudflare 申請一個 API token 了
先到 Cloudflare token 點擊 「Create Token」

 

然後使用「Edit zone DNS」的 template

 

Permission 選擇
– Zone, DNS, Edit

Zone Resources 選擇
– Include, All zones

Client IP Address Filtering 什麼都不選

然後點「Continue to summary」

 

點擊「Create Token」
 

然後把 token 複製一下
之後會用到

安裝 server packages

接下來就是在你的伺服器安裝要用到的套件了

apt install certbot python3-certbot-dns-cloudflare

 

設置 token 文件

建立 token 文件

mkdir ~/.secrets
vim ~/.secrets/cloudflare.ini

 

點擊 i 以進行輸入
然後在文件輸入以下內容
記得要把 <API_TOKEN> 換為你自己的 token

dns_cloudflare_api_token = <API_TOKEN>

然後點 esc, :, w, q, enter 進行保存

 

調整檔案權限

chmod 0700 ~/.secrets/
chmod 0400 ~/.secrets/cloudflare.ini

申請證書

執行下面這個指令來申請證書
記得要把 example.com 換為你的域名
並把 [email protected] 換為你的收件電郵地址
不過Let’s encrypt 好像說是快要不再發電郵了🫣

sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials ~/.secrets/cloudflare.ini \
-d example.com,*.example.com \
--preferred-challenges dns-01 \
--dns-cloudflare-propagation-seconds 60 \
--email [email protected]

執行後若是看到類似下面這樣的訊息
就算是申請成功了

# Saving debug log to /var/log/letsencrypt/letsencrypt.log
# Plugins selected: Authenticator dns-cloudflare, Installer None
# Cert is due for renewal, auto-renewing...
# Renewing an existing certificate
# Performing the following challenges:
# dns-01 challenge for example.com
# dns-01 challenge for example.com
# Waiting 60 seconds for DNS changes to propagate
# Waiting for verification...
# Cleaning up challenges
# 
# IMPORTANT NOTES:
#  - Congratulations! Your certificate and chain have been saved at:
#    /etc/letsencrypt/live/example.com/fullchain.pem
#    Your key file has been saved at:
#    /etc/letsencrypt/live/example.com/privkey.pem
#    Your cert will expire on 2024-01-01. To obtain a new or tweaked
#    version of this certificate in the future, simply run certbot
#    again. To non-interactively renew *all* of your certificates, run
#    "certbot 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

自動更新證書

你可以使用 root 身份去執行 cronjob 以避免權限問題

sudo crontab -e

然後把這句放到你的 cronjob 並保存

0 0 1 * * /usr/bin/certbot renew --quiet --no-self-upgrade --post-hook "apachectl -k graceful"

這樣你的伺服器就會每月嘗試更新一次證書了

你也可以運行下面這句看一下在更新證書期間會不會有什麼問題

sudo certbot renew --dry-run

如果看到像下面這樣的結果就是沒問題了

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for example.com and *.example.com
Waiting 60 seconds for DNS changes to propagate

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/example.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

後記

會搞出這麼一個問題是我沒有想過的
不過要處理也不算很麻煩
Cloudflare 免費提供這麼多的服務
真的是太佛心了
再一次表達感謝
也非常感謝 Let’s Encrypt
能為我們這些窮苦大眾提供免費SSL證書
🫡🫡🫡

如果日後證書更新有什麼問題的話會再更新這篇的
也非常歡迎大家在下面留言討論


參考資料

https://blog.ty-wu.com/posts/certbotwithcloudflare


廣告欄位
本站致力於為大家提供乾淨舒服的獲取資訊途徑
並盡量把廣告對閱讀體驗的影響降至最低
若想支持本站營運
可點擊此處打開廣告
廣告的內容並非由本站控制
請小心驗證廣告內容
感謝各位看客的支持 ( •̀ ω •́ )y


發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


Trending