meta-description: Let's Encrypt泛域名证书配置完整教程,使用acme.sh和certbot实现自动续期。
keywords: Let's Encrypt,泛域名证书,acme.sh,certbot,SSL证书,自动续期
# Let's Encrypt泛域名证书配置与自动续期
本文详细介绍如何使用acme.sh和certbot申请Let's Encrypt泛域名证书,并配置自动续期。
## 泛域名证书优势
| 优势 | 说明 |
|------|------|
| 一张证书覆盖所有子域名 | *.example.com |
| 节省费用 | 免费 |
| 简化管理 | 一个证书搞定 |
| 自动续期 | 90天自动更新 |
## 使用acme.sh(推荐)
### 安装acme.sh
# 安装
curl https://get.acme.sh | sh
# 重新加载环境
source ~/.bashrc
# 验证
acme.sh --version
### 使用CloudFlare API申请泛域名证书
# 设置CloudFlare API Token
export CF_Token="your_cloudflare_api_token"
export CF_Account_ID="your_account_id"
# 申请泛域名证书
acme.sh --issue --dns dns_cf -d example.com -d *.example.com
# 安装证书到指定目录
acme.sh --installcert -d example.com \
--key-file /etc/nginx/ssl/example.com.key \
--fullchain-file /etc/nginx/ssl/example.com.crt \
--reloadcmd "systemctl reload nginx"
### 使用Aliyun(阿里云)DNS申请
# 设置阿里云API密钥
export Ali_Key="your_aliyun_access_key"
export Ali_Secret="your_aliyun_access_secret"
# 申请证书
acme.sh --issue --dns dns_ali -d example.com -d *.example.com
### 使用DNSPod(腾讯云)申请
# 设置DNSPod API密钥
export DP_Id="your_dnspod_id"
export DP_Key="your_dnspod_token"
# 申请证书
acme.sh --issue --dns dns_dp -d example.com -d *.example.com
## 使用certbot申请
### 安装certbot
# Ubuntu/Debian
sudo apt update
sudo apt install certbot -y
# CentOS/Rocky
sudo dnf install certbot -y
### 使用DNS插件申请泛域名
# 安装CloudFlare插件
sudo apt install python3-certbot-dns-cloudflare -y
# 配置CloudFlare API Token
echo "dns_cloudflare_api_token = your_token" > /etc/letsencrypt/cloudflare.ini
chmod 600 /etc/letsencrypt/cloudflare.ini
# 申请泛域名证书
sudo certbot certonly --dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
-d example.com \
-d *.example.com
## Nginx配置泛域名证书
server {
listen 443 ssl http2;
server_name example.com *.example.com;
# 证书路径(acme.sh)
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
# 或者(certbot)
# 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-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512;
ssl_prefer_server_ciphers off;
# 其他配置
root /var/www/html;
index index.php index.html;
}
## 自动续期配置
### acme.sh自动续期
# acme.sh会自动添加cron任务
crontab -l
# 应该看到类似:
# 0 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
# 手动测试续期
acme.sh --renew -d example.com --force
### certbot自动续期
# certbot会自动配置systemd timer
sudo systemctl status certbot.timer
# 手动测试续期
sudo certbot renew --dry-run
# 查看续期配置
sudo cat /etc/cron.d/certbot
## 多域名配置
# 一个证书包含多个域名
acme.sh --issue --dns dns_cf \
-d example.com \
-d *.example.com \
-d api.example.com \
-d admin.example.com
## 常见问题
| 问题 | 解决方案 |
|------|----------|
| DNS验证失败 | 检查API Token权限 |
| 证书续期失败 | 检查cron任务是否运行 |
| Nginx启动失败 | 检查证书路径权限 |
| 混合内容错误 | 确保所有资源使用HTTPS |
## 证书有效期检查
# 检查证书到期时间
openssl x509 -in /etc/nginx/ssl/example.com.crt -noout -dates
# 使用acme.sh检查
acme.sh --list
# 使用在线工具
# https://www.ssllabs.com/ssltest/
## 安全最佳实践
1. 使用ECC证书(更小更快)
`bash
acme.sh --issue --dns dns_cf -d example.com --keylength ec-256
`
2. 启用OCSP Stapling
`nginx
ssl_stapling on;
ssl_stapling_verify on;
`
3. 启用HSTS
`nginx
add_header Strict-Transport-Security "max-age=63072000" always;
`
泛域名证书大大简化了多子域名站点的SSL管理,配合自动续期可做到完全免维护。

评论(0)