# 建立 SSL 憑證

本教學簡要介紹了為面板和機翼建立新的 SSL 憑證。

:::: 選項卡 ::: 選項卡“方法 1:Certbot” 首先,我們將安裝 certbot,這是一個簡單的腳本,可以自動更新我們的憑證並允許很多操作 更容易創建它們。下面的指令適用於 Ubuntu 發行版,但您可以隨時查看 Certbot 官方 site (opens new window) 了解安裝說明。我們還在下面添加了一個命令來安裝 certbot Nginx/Apache 插件,這樣您就不必停止您的網頁伺服器。

sudo apt update
sudo apt install -y certbot
# Run this if you use Nginx
sudo apt install -y python3-certbot-nginx
# Run this if you use Apache
sudo apt install -y python3-certbot-apache

# 建立證書

安裝certbot後,我們需要產生憑證。有幾種方法可以做到這一點,但最簡單的 是使用您剛安裝的特定於 Web 伺服器的 certbot 外掛程式。對於不需要 Web 伺服器的僅 Wings 計算機,請使用 certbot 的獨立或 DNS 方法,因為您不需要 Web 伺服器。

然後,在下面的命令中,您應該將 example.com 替換為您想要產生憑證的網域 為了。 當您有多個網域需要證書時,只需在網域中新增更多 -d anotherdomain.com 標誌即可 命令。您也可以考慮產生通配符證書,但這不在本教學中介紹。

當您使用 certbot 的 Nginx/Apache 外掛程式時,您無需重新啟動網頁伺服器即可獲得證書 假設您已依照 web server configuration step (opens new window) 中的說明將網頁伺服器設定為使用 SSL,則套用此操作。

# HTTP 挑戰

HTTP 質詢要求您公開連接埠 80 以進行質詢驗證。

# Nginx
certbot certonly --nginx -d example.com
# Apache
certbot certonly --apache -d example.com
# Standalone - Use this if neither works. Make sure to stop your webserver first when using this method.
certbot certonly --standalone -d example.com

# DNS challenge

DNS 質詢要求您建立新的 TXT DNS 記錄來驗證網域擁有權,而不必公開連接埠 80。當您執行下面的 certbot 指令時,將顯示相關說明。

certbot -d example.com --manual --preferred-challenges dns certonly

# 自動續訂

您可能還需要配置憑證的自動續訂以防止憑證意外過期。 您可以使用 sudo crontab -e 打開 crontab,並將下面的行新增到其底部,以便在每天 23 點(晚上 11 點)嘗試更新。

更新成功後,部署掛鉤將重新啟動 Nginx 服務以套用新憑證。變更重新啟動命令中的 nginx 以滿足您自己的需要,例如變更為 apachewings

對於進階用戶,我們建議安裝和使用 acme.sh (opens new window) 它提供了更多選項,並且比 certbot 更強大。

0 23 * * * certbot renew --quiet --deploy-hook "systemctl restart nginx"

# 故障排除

如果您在嘗試存取面板或 Wing 時收到 Insecure Connection 或 SSL/TLS 相關錯誤,則憑證可能已過期。 透過更新 SSL 憑證可以輕鬆解決此問題,但如果連接埠 80 正在使用,則使用命令 certbot renew 可能無法完成此任務,因為它會傳回類似下列錯誤: Error: Attempting to renew cert (domain) from /etc/letsencrypt/renew/domain.conf produced an unexpected error

當您執行 Nginx 而不是 Apache 時,尤其會發生這種情況。解決方案是使用具有 --nginx--apache 的 Nginx 或 Apache 插件。或者,您可以停止 Nginx,然後續訂證書,最後重新啟動 Nginx。如果您要續訂 Wings 的證書,請將 nginx 替換為您自己的 Web 伺服器或 wings

停止 Nginx:

systemctl stop nginx

更新憑證:

certbot renew

過程完成後,您可以重新啟動 Nginx 服務:

systemctl start nginx

您可能還需要重新啟動 Wings,因為並非每個服務都能夠自動套用更新的憑證:

systemctl restart wings

::: ::: 選項卡“方法 2:acme.sh(使用 Cloudflare API)” 這是針對高級用戶的,他們的伺服器系統無法存取連接埠 80。以下的命令適用於 Ubuntu 發行版和 CloudFlare API(您可以透過 google 搜尋其他 DNS 提供者的其他 API),但您始終可以檢查 acme.sh's official site (opens new window) 以取得安裝說明。請務必閱讀這兩個說明,因為有些人可能已經轉移到 CloudFlare 的 new authorization system (opens new window)(現代),但其他人可能已轉移到 have not (opens new window)(舊版)。

curl https://get.acme.sh | sh

# 取得 CloudFlare API 金鑰(舊版)

安裝 acme.sh 後,我們需要取得 CloudFlare API 金鑰。在Cloudfare的網站上,選擇您的域名,然後在右側複製您的“區域ID”和“帳戶ID”,然後點擊“取得您的API令牌”,點擊“建立令牌”>選擇範本“編輯區域DNS”>選擇“區域資源”的範圍,然後點擊“繼續摘要”,複製您的令牌。

# 建立證書

由於設定檔是基於Certbot的,因此我們需要手動建立該資料夾。

sudo mkdir -p /etc/letsencrypt/live/example.com

安裝 acme.sh 並取得 CloudFlare API 金鑰後,我們需要產生憑證。首先,輸入 CloudFlare API 憑證。

export CF_Token="Your_CloudFlare_API_Key"
export CF_Account_ID="Your_CloudFlare_Account_ID"
export CF_Zone_ID="Your_CloudFlare_Zone_ID"

# 取得 CloudFlare API 金鑰(現代)

安裝 acme.sh 後,我們需要取得 CloudFlare API 金鑰。在 Cloudfare 網站上,點擊右上角的個人資料。然後轉到“我的個人資料”,在左側您將找到“API 令牌”。單擊它,它會將您帶到 the API tokens page (opens new window) 。選擇“建立令牌”並使用“編輯區域 DNS”範本。然後在下一頁上,轉到“區域資源”和“包含”-“特定區域”-(選擇您要使用的網域)。然後繼續總結。確認您想要建立令牌。

# 建立證書

由於設定檔是基於Certbot的,因此我們需要手動建立該資料夾。

sudo mkdir -p /etc/letsencrypt/live/example.com

安裝 acme.sh 並取得 CloudFlare API 金鑰後,我們需要產生憑證。首先,輸入 CloudFlare API 憑證。

export CF_Key="Your_CloudFlare_API_Key"
export CF_Email="Your_CloudFlare_Email"

然後創建證書。由於 API 金鑰綁定到網域,Cloudflare 應該允許您產生一個。

acme.sh --issue --dns dns_cf -d "example.com" --server letsencrypt \
--key-file /etc/letsencrypt/live/example.com/privkey.pem \
--fullchain-file /etc/letsencrypt/live/example.com/fullchain.pem

# 自動續訂

第一次執行該腳本後,它會自動新增到 crontab 中。您可以編輯 crontab 來編輯自動續訂間隔。

sudo crontab -e

::: ::: 選項卡“方法 3:Caddy(使用 Cloudflare API)” 這適用於在代理模式下執行 Cloudflare 或無權存取連接埠 80 的高階使用者。

# 使用 Cloudflare DNS 外掛程式安裝 Caddy

Caddy 預設不附帶 Cloudflare DNS 插件,需要您自行安裝。

主要有兩種方法:

  1. 使用 xcaddy - CLI 工具建立您自己的 Caddy 版本
  2. Caddy's download page (opens new window) 下載預先建置的二進位。 3.使用Ansible下載並安裝附有插件的Caddy。請參閱caddy-ansible (opens new window)

# 在您的伺服器上使用 xcaddy 建置 Caddy

請參閱 Caddy docs on building Caddy (opens new window)

# 取得 CloudFlare API 令牌

安裝 acme.sh 後,我們需要取得 CloudFlare API 金鑰。請確保 DNS 記錄(A 或 CNAME 記錄)指向您的目標節點,並將雲端設定為灰色(繞過 CloudFlare 代理)。然後轉到“我的個人資料”>“API 金鑰”,在“全域 API 金鑰”子選項卡上,按一下“檢視”,輸入您的 CloudFlare 密碼,然後將 API 金鑰複製到剪貼簿。

安裝帶有 Cloudflare DNS 插件的 Caddy 後,我們需要取得 Cloudflare API 令牌。請確保 DNS 記錄(A 或 CNAME 記錄)指向您的目標節點。然後轉到我的個人資料 > API 令牌,然後在 API 令牌上按一下「建立令牌」。建立 API 令牌 > API 令牌模板,在「編輯區域 DNS」行末尾,按一下「使用模板」。在區域資源下,選擇您要為其建立 API 令牌的 DNS 區域,然後按一下「繼續摘要」。查看 API 令牌摘要並按一下「建立令牌」。最後將 API 令牌複製到剪貼簿。

# 重新配置 Caddy 以使用 Cloudflare DNS 取得證書

建立一個環境變數文件(如 .env ),請記住該文件包含機密,不應被公眾存取。

我們建議您在下列位置建立機密文件: /etc/caddy/.secrets.env

CLOUDFLARE_API_TOKEN=<your cloudflare api token>

出於安全原因,我們建議將權限設為 0600 (只有擁有者可以讀取或寫入該檔案)。

# Set ownership of the `.secrets.env` file to `caddy` system user
chown caddy:caddy /etc/caddy/.secrets.env

# Set read-write permissions only to owner - the `caddy` system user
chmod 0600 /etc/caddy/.secrets.env

修改 systemd 單元文件,從檔案載入環境變數(將 --envfile /etc/caddy/.secrets.env 標誌加入 ExecStart ),預設的 systemd 單元檔案位置是 /etc/systemd/system/caddy.service












 










[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --envfile /etc/caddy/.secrets.env --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

您可以將 tls 區塊新增至 Caddyfile ,在面板配置的 <domain> 區塊下,Caddy 設定檔位置是 /etc/caddy/Caddyfile





 
 
 

<domain> {
  # ...

  tls {
    dns cloudflare {env.CLOUDFLARE_API_TOKEN}
  }
}

::: ::::