跳轉到內容

Let's Encrypt SSL 證書自動化管理完全指南 2026

Let's Encrypt SSL 證書

🔒 HTTPS 已不是選項,而是標配。 自 2020 年起,主流瀏覽器對所有 HTTP 網站標記"不安全",搜索引擎也優先排名 HTTPS 網站。Let's Encrypt 提供免費、自動化的 SSL 證書,是個人網站和小型項目的首選方案。本文帶你從零掌握證書申請、自動續期與全場景配置。

本文將帶你係統掌握:

  • ✅ SSL/TLS 基礎知識與證書類型
  • ✅ Certbot 安裝與使用
  • ✅ 單域名證書申請
  • ✅ 通配符證書(Wildcard)申請
  • ✅ Nginx / Apache SSL 配置
  • ✅ 自動續期與到期告警
  • ✅ Docker 環境下的證書管理
  • ✅ Cloudflare 模式下的 SSL 選擇
  • ✅ 常見問題排查與最佳實踐

一、SSL/TLS 基礎

1.1 為什麼需要 HTTPS

  • 加密傳輸:防止數據在傳輸中被竊聽
  • 數據完整性:防止內容被篡改
  • 身份驗證:證明網站真實身份
  • SEO 優勢:Google 優先排名 HTTPS 網站
  • 瀏覽器信任:避免"不安全"警告
  • 功能必需:HTTP/2、PWA、Geolocation 等需要 HTTPS

1.2 SSL vs TLS

項目SSLTLS
全稱Secure Sockets LayerTransport Layer Security
版本SSL 2.0 / 3.0(已廢棄)TLS 1.0 ~ 1.3
現狀已不安全,禁用TLS 1.2 / 1.3 為主流

💡 現在說的"SSL 證書"實際上都是 TLS 證書,只是習慣稱呼。

1.3 證書類型

類型說明驗證方式適用場景
DV(域名驗證)僅驗證域名所有權DNS / HTTP個人博客、小型網站
OV(組織驗證)驗證企業信息企業資料審核企業官網
EV(擴展驗證)最高級別驗證嚴格審核金融、電商

Let's Encrypt 只提供 DV 證書,但對大多數場景足夠。

1.4 Let's Encrypt 的特點

  • 完全免費:不收任何費用
  • 自動化:支持自動申請和續期
  • 開放標準:基於 ACME 協議
  • 有效期 90 天:鼓勵自動化續期
  • 支持通配符:可申請 *.example.com 通配符證書
  • 受信任:被所有主流瀏覽器信任

二、Certbot 安裝

2.1 什麼是 Certbot

Certbot 是 Let's Encrypt 官方推薦的 ACME 客戶端,用於:

  • 自動申請證書
  • 自動驗證域名
  • 自動配置 Web 服務器
  • 自動續期

2.2 安裝 Certbot

Ubuntu / Debian:

bash
# 更新包管理器
sudo apt update

# 安裝 Certbot 和 Nginx 插件
sudo apt install -y certbot python3-certbot-nginx

# 安裝 Apache 插件(如使用 Apache)
sudo apt install -y python3-certbot-apache

CentOS / RHEL:

bash
# 安裝 EPEL 倉庫
sudo yum install -y epel-release

# 安裝 Certbot
sudo yum install -y certbot python3-certbot-nginx

使用 Snap(官方推薦):

bash
# 安裝 snapd(如未安裝)
sudo apt install -y snapd

# 安裝 Certbot
sudo snap install --classic certbot

# 創建軟鏈接
sudo ln -s /snap/bin/certbot /usr/bin/certbot

使用 Docker:

bash
docker run --rm \
  -v /etc/letsencrypt:/etc/letsencrypt \
  -v /var/lib/letsencrypt:/var/lib/letsencrypt \
  -v /var/log/letsencrypt:/var/log/letsencrypt \
  certbot/certbot certonly

2.3 驗證安裝

bash
certbot --version
# 輸出: certbot 2.x.0

三、申請單域名證書

3.1 Webroot 方式(推薦,不中斷服務)

Webroot 方式通過在網站根目錄放置驗證文件來證明域名所有權。

前提條件:

  • 域名已解析到服務器
  • Web 服務器(Nginx/Apache)已運行
  • 80 端口可訪問

申請證書:

bash
sudo certbot certonly \
  --webroot \
  -w /var/www/html \
  -d example.com \
  -d www.example.com \
  --email your@email.com \
  --agree-tos \
  --no-eff-email

參數說明:

  • --webroot:使用 Webroot 驗證方式
  • -w:網站根目錄
  • -d:域名(可指定多個)
  • --email:用於到期提醒的郵箱
  • --agree-tos:同意服務條款

生成的文件位置:

/etc/letsencrypt/live/example.com/
├── cert.pem         # 證書文件
├── chain.pem        # 鏈證書
├── fullchain.pem    # 完整證書鏈(推薦使用)
├── privkey.pem      # 私鑰文件
└── README           # 說明文件

3.2 Standalone 方式

Standalone 方式會臨時啟動一個 Web 服務器進行驗證,需要停止現有 Web 服務器。

bash
# 先停止 Nginx
sudo systemctl stop nginx

# 申請證書
sudo certbot certonly \
  --standalone \
  -d example.com \
  --email your@email.com \
  --agree-tos

# 重新啟動 Nginx
sudo systemctl start nginx

3.3 Nginx 插件方式(最簡單)

Certbot 可以自動修改 Nginx 配置:

bash
sudo certbot --nginx \
  -d example.com \
  -d www.example.com \
  --email your@email.com \
  --agree-tos

Certbot 會自動:

  • 申請證書
  • 修改 Nginx 配置
  • 配置 HTTPS
  • 設置 HTTP 自動跳轉 HTTPS

四、申請通配符證書

4.1 為什麼需要通配符證書

通配符證書 *.example.com 可以:

  • 保護所有子域名
  • 無需為每個子域名單獨申請
  • 添加新子域名時無需重新申請

4.2 DNS 驗證方式

通配符證書必須使用 DNS 驗證:

bash
sudo certbot certonly \
  --manual \
  --preferred-challenges dns \
  -d *.example.com \
  -d example.com \
  --email your@email.com \
  --agree-tos

Certbot 會提示添加 DNS TXT 記錄:

Please deploy a DNS TXT record under the name:
_acme-challenge.example.com.

with the following value:
gfj9Xq...驗證碼...

添加 DNS 記錄:

  1. 登錄域名服務商(Cloudflare、阿里雲等)
  2. 添加 TXT 記錄
  3. 主機記錄:_acme-challenge
  4. 記錄值:Certbot 提供的驗證碼
  5. 等待 DNS 生效(通常 1-10 分鐘)
bash
# 驗證 DNS 是否生效
dig -t txt _acme-challenge.example.com

# 或使用 nslookup
nslookup -type=txt _acme-challenge.example.com

DNS 生效後,按回車繼續。

4.3 使用 DNS 插件自動驗證

手動添加 DNS 記錄比較麻煩,可以使用 DNS 插件自動化:

Cloudflare 插件:

bash
# 安裝 Cloudflare 插件
sudo apt install -y python3-certbot-dns-cloudflare

# 創建配置文件
sudo nano /etc/letsencrypt/cloudflare.ini

配置文件內容:

ini
dns_cloudflare_api_token = 你的Cloudflare_API_Token
bash
# 設置權限
sudo chmod 600 /etc/letsencrypt/cloudflare.ini

# 申請通配符證書
sudo certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
  -d *.example.com \
  -d example.com \
  --email your@email.com \
  --agree-tos

其他常用 DNS 插件:

DNS 服務商插件名
Cloudflaredns-cloudflare
阿里雲dns-aliyun(第三方)
騰訊雲dns-tencent(第三方)
DNSPoddns-dnspod(第三方)
AWS Route53dns-route53
Google Domainsdns-google

五、Nginx SSL 配置

5.1 基本配置

nginx
server {
    listen 80;
    server_name example.com www.example.com;
    # HTTP 跳轉 HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    # SSL 證書
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # 網站根目錄
    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

5.2 安全加固配置

nginx
server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # SSL 協議與加密套件
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # SSL 會話緩存
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # HSTS(HTTP 嚴格傳輸安全)
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

    # 其他安全頭
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header Referrer-Policy strict-origin-when-cross-origin;

    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    root /var/www/html;
    index index.html;
}

5.3 Apache SSL 配置

apache
<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

    # 安全配置
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256
    SSLHonorCipherOrder off
    Header always set Strict-Transport-Security "max-age=63072000"
</VirtualHost>

# HTTP 跳轉 HTTPS
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

六、自動續期

6.1 測試續期

Let's Encrypt 證書有效期 90 天,建議在到期前 30 天續期。

bash
# 測試續期(不會真正續期)
sudo certbot renew --dry-run

6.2 自動續期配置

Certbot 安裝時會自動創建定時任務:

bash
# 查看定時任務
systemctl list-timers | grep certbot

如果沒有,手動創建:

bash
# 編輯 crontab
sudo crontab -e

# 每天 3 點檢查並續期
0 3 * * * certbot renew --quiet --post-hook "systemctl reload nginx"

6.3 續期鉤子

bash
# 續期前執行
sudo certbot renew --pre-hook "systemctl stop nginx"

# 續期後執行
sudo certbot renew --post-hook "systemctl start nginx"

# 僅在證書更新後執行
sudo certbot renew --deploy-hook "systemctl reload nginx"

💡 使用 --deploy-hook 更好,只在證書真正更新後才執行 reload。

6.4 續期通知

郵件通知:

Certbot 會在證書即將到期時發送郵件(需配置 email)。

Webhook 通知:

bash
#!/bin/bash
# /opt/scripts/certbot-renew-hook.sh

WEBHOOK_URL="https://your-webhook-url"

# 發送通知
curl -X POST $WEBHOOK_URL \
  -H "Content-Type: application/json" \
  -d "{\"event\":\"ssl_renewed\",\"domain\":\"$RENEWED_DOMAINS\",\"date\":\"$(date)\"}"

配置:

bash
sudo certbot renew --deploy-hook /opt/scripts/certbot-renew-hook.sh

6.5 查看證書信息

bash
# 查看所有證書
sudo certbot certificates

# 查看證書詳情
sudo openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -text -noout | head -20

# 查看到期時間
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates

七、Docker 環境下的證書管理

7.1 Certbot 容器化

bash
# 申請證書
docker run -it --rm \
  -v /etc/letsencrypt:/etc/letsencrypt \
  -v /var/lib/letsencrypt:/var/lib/letsencrypt \
  -v /var/www/html:/webroot \
  certbot/certbot certonly \
  --webroot -w /webroot \
  -d example.com \
  --email your@email.com \
  --agree-tos

# 續期
docker run --rm \
  -v /etc/letsencrypt:/etc/letsencrypt \
  -v /var/www/html:/webroot \
  certbot/certbot renew

7.2 Docker Compose 集成

yaml
version: '3.8'

services:
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./html:/var/www/html
      - /etc/letsencrypt:/etc/letsencrypt:ro
    restart: always

  certbot:
    image: certbot/certbot
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt
      - /var/lib/letsencrypt:/var/lib/letsencrypt
      - ./html:/var/www/html
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h; done;'"
    restart: always

7.3 Nginx Proxy Manager

如果你不想手動配置,可以使用 Nginx Proxy Manager(NPM):

yaml
version: '3'
services:
  npm:
    image: jc21/nginx-proxy-manager:latest
    restart: always
    ports:
      - "80:80"
      - "443:443"
      - "81:81"
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt
    environment:
      DB_SQLITE_FILE: "/data/database.sqlite"

通過 Web 界面(端口 81)一鍵申請和管理 SSL 證書。


八、Cloudflare 模式下的 SSL

8.1 Cloudflare SSL 模式

模式說明適用場景
Off不加密不推薦
FlexibleCF 到客戶端加密,CF 到源站不加密源站不支持 SSL
Full全程加密,但不驗證證書自簽名證書
Full (Strict)全程加密且驗證證書推薦配置

8.2 使用 Cloudflare 代理

如果使用 Cloudflare 代理(橙色雲朵):

  1. Cloudflare 會提供邊緣證書(免費)
  2. 源服務器仍需要 SSL 證書
  3. 推薦使用 Full (Strict) 模式
  4. 可以使用 Cloudflare Origin CA 證書(有效期 15 年)

8.3 Cloudflare Origin CA 證書

bash
# 使用 Cloudflare API 申請 Origin 證書
curl -X POST "https://api.cloudflare.com/client/v4/certificates" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  --data '{
    "hostnames": ["example.com", "*.example.com"],
    "requested_validity": 5475,
    "request_type": "origin-rsa",
    "csr": ""
  }'

💡 Origin CA 證書有效期最長 15 年,無需頻繁續期,但僅在 Cloudflare 代理時有效。


九、常見問題排查

9.1 申請失敗

問題:域名驗證失敗

bash
# 檢查域名解析
dig example.com

# 檢查 80 端口是否可訪問
curl -I http://example.com

# 檢查防火牆
sudo ufw status
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

問題:DNS 驗證未生效

bash
# 檢查 TXT 記錄
dig -t txt _acme-challenge.example.com

# 等待更長時間(可能 DNS 緩存未刷新)
# 清除本地 DNS 緩存
sudo systemd-resolve --flush-caches

9.2 續期失敗

問題:Webroot 路徑錯誤

bash
# 確認網站根目錄
ls /var/www/html

# 檢查 Nginx 配置
nginx -t

問題:端口被佔用

bash
# 查看端口占用
sudo lsof -i :80
sudo lsof -i :443

9.3 查看日誌

bash
# Certbot 日誌
sudo cat /var/log/letsencrypt/letsencrypt.log

# Nginx 錯誤日誌
sudo tail -f /var/log/nginx/error.log

9.4 撤銷證書

bash
# 撤銷證書
sudo certbot revoke \
  --cert-path /etc/letsencrypt/live/example.com/cert.pem \
  --reason keycompromise

# 刪除證書
sudo certbot delete --cert-name example.com

十、最佳實踐

10.1 安全建議

  1. 使用 TLS 1.2+:禁用 TLS 1.0 和 1.1
  2. 啟用 HSTS:強制瀏覽器使用 HTTPS
  3. 啟用 OCSP Stapling:提升性能和隱私
  4. 使用強加密套件:優先使用 ECDHE
  5. 定期檢查評級:使用 SSL Labs 測試

10.2 性能優化

  1. 啟用 HTTP/2:多路複用,提升加載速度
  2. Session 緩存:減少握手開銷
  3. Session Tickets:簡化會話恢復
  4. ECC 證書:比 RSA 更快,密鑰更小

10.3 運維規範

規範說明
監控到期設置告警,提前 30 天提醒
自動續期配置 crontab 或 systemd timer
定期測試定期執行 --dry-run 測試
備份證書備份 /etc/letsencrypt/ 目錄
記錄文檔記錄所有域名和證書對應關係

10.4 檢查 SSL 評級

使用在線工具檢查 SSL 配置:


十一、常用命令速查

bash
# 申請證書
certbot certonly --webroot -w /var/www/html -d example.com --email you@email.com --agree-tos

# 申請通配符證書
certbot certonly --manual --preferred-challenges dns -d *.example.com -d example.com

# Nginx 自動配置
certbot --nginx -d example.com

# 續期
certbot renew                    # 續期所有
certbot renew --dry-run          # 測試續期
certbot renew --cert-name example.com  # 續期指定證書

# 查看
certbot certificates             # 查看所有證書
openssl x509 -in cert.pem -text -noout  # 查看證書詳情

# 刪除
certbot delete --cert-name example.com  # 刪除證書

# 撤銷
certbot revoke --cert-path cert.pem

十二、總結

Let's Encrypt 讓 HTTPS 普惠化:

  1. 完全免費:零成本獲得受信任的 SSL 證書
  2. 自動化:Certbot 讓申請和續期全自動
  3. 安全可靠:被所有主流瀏覽器信任
  4. 通配符支持:一張證書覆蓋所有子域名
  5. 生態完善:支持各種 Web 服務器和 DNS 服務商

🎯 配置好 SSL 證書和自動續期,從此告別瀏覽器"不安全"警告,讓網站更專業、更安全。


相關文章推薦:

最後更新於: