:::info ## acme.sh 介紹 **acme.sh** 是一個純 Unix Shell 腳本,專門用於申請與管理 **SSL 憑證**。 - Github 上擁有超過 **4 萬顆星** ⭐ - 支援多種 CA(授權伺服器),如 **Let’s Encrypt**、**ZeroSSL** - 可與各種 DNS API 整合,支援 **自動化憑證續期** ::: 流程示意圖 ```mermaid graph TD; A[使用者 / Server] -->|呼叫 acme.sh| B(acme.sh 腳本) B -->|使用 CF_Token 認證| C[Cloudflare API] C -->|建立/驗證 DNS TXT 紀錄| D[Cloudflare DNS] D -->|回應驗證結果| C C --> B B -->|驗證成功後向 CA 申請憑證| E[CA: Let's Encrypt / ZeroSSL] E --> B B -->|安裝並保存憑證| A ``` --- # 事前準備 在使用 Cloudflare API 與 acme.sh 整合之前,需要先建立一個 **API Token**: 1. 前往 Cloudflare Dashboard [https://dash.cloudflare.com/profile/api-tokens](https://dash.cloudflare.com/profile/api-tokens) 2. 點擊 **「Create Token」** 3. 選擇 **「Custom token」** 模板 4. 設定權限: - **Zone:Zone:Read** - **Zone:DNS:Edit** 5. 設定區域資源:選擇需要操作的 **特定網域** 6. 建立完成後,**複製 Token** 保存備用 --- # 安裝 acme.sh ```bash curl https://get.acme.sh | sh source ~/.bashrc ``` # 註冊帳號 ```bash acme.sh --register-account -m your-email@example.com ``` 完成註冊後,acme.sh 會將 **ACCOUNT\_THUMBPRINT**、帳號金鑰與註冊資訊 保存於 `~/.acme.sh/ca/` 目錄下。 # 設定 Cloudflare API Token ```bash export CF_Token="your_cloudflare_api_token_here" ``` 注意:以上設定僅在 **當前 Session 有效**,登出或重開機後會消失。 建議永久保存環境變數: - **bash** → `~/.bashrc` 或 `~/.bash_profile` - **zsh** → `~/.zshrc` 在檔案中新增: ```bash export CF_Token="your_cloudflare_api_token_here" ``` # 申請憑證 ### 單網域憑證 ```bash acme.sh --issue --dns dns_cf -d example.com ``` ### 通配符憑證 ```bash acme.sh --issue --dns dns_cf -d example.com -d '*.example.com' ``` ### 多網域憑證 ```bash acme.sh --issue --dns dns_cf \ -d example.com \ -d '*.example.com' \ -d demo.com \ -d '*.demo.com' ``` # 常用操作 ```bash acme.sh --list # 查看所有已申請的憑證 ``` ```bash acme.sh --set-default-ca --server [letsencrypt|zerossl|buypass] # 切換預設 CA ```