跳到主要内容
ssl证书按装

Let’s Encrypt 部署 HTTPS 证书全流程:Nginx、Apache、IIS 与小程序域名认证

Let's Encrypt 部署 HTTPS 证书全流程:Nginx、Apache、IIS 与小程序域名认证 — Let's Encrypt 部署 HTTPS 证书全流程:Nginx、Apache、IIS 与小程序域名认证

问题背景与根因:为什么 Let’s Encrypt 部署后仍会 HTTPS/小程序失败

Let’s Encrypt 通过 ACME 协议免费签发受信任 TLS 证书,单张证书有效期 90 天,推荐每 60 天自动续期;按 CA/Browser Forum 规则,公开信任 TLS 证书最长有效期 398 天,因此不要再购买或上传超长有效期证书。HTTPS 失败通常不是“证书没申请下来”,而是 DNS、80/443 端口、SNI、证书链、系统时间或域名备案不满足要求。微信小程序服务器域名要求 HTTPS,域名需 ICP 备案,不能填 IP 或 localhost,证书必须被系统信任且链完整,TLS 建议 1.2 及以上;IIS 还常见 PFX 导入后私钥权限、SNI 绑定和中间证书缺失问题。

常见现象包括浏览器地址栏仍显示不安全、curl 报 SSL certificate problem、小程序保存域名提示证书无效、IIS 站点 443 无法启动。排查顺序:DNS 是否指向当前服务器;80/443 是否放行;ACME HTTP-01 是否能访问 /.well-known/acme-challenge/;Nginx/Apache 是否加载 fullchain;IIS 是否绑定正确站点和主机名;证书是否过期或系统时间错误;CDN/WAF 是否回源阻断。下面按“准备→签发→部署→排错→验证”给出可直接执行的流程。

准备域名、端口与 ACME 客户端

先把业务域名和 www、api 等子域解析到服务器公网 IP,AAAA 记录没有 IPv6 就删除,避免 ACME 回源到错误地址。云安全组和本机防火墙放行 80/443;HTTP-01 必须让外网访问 80,DNS-01 不需要 80 但要求能写 TXT。以 Ubuntu/Debian 为例安装 Certbot:

sudo apt update
sudo apt install -y certbot python3-certbot-nginx python3-certbot-apache
sudo systemctl enable --now nginx
sudo ss -lntp | grep -E ':80|:443'
curl -I http://example.com/.well-known/acme-challenge/test

CentOS/Rocky 可用 sudo dnf install -y certbot python3-certbot-nginx。泛域名和 DNS API 自动化建议用 acme.sh:

curl https://get.acme.sh | sh -s email=admin@example.com
source ~/.bashrc
~/.acme.sh/acme.sh --version
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt

测试阶段务必加 --staging 或 --server https://acme-staging-v02.api.letsencrypt.org/directory,避免触发正式环境速率限制。检查时间同步:timedatectl status,偏差过大会导致 not yet valid。

实操一:Certbot 签发独立域名/多域名证书(Nginx/Apache)

Nginx 站点已能通过 HTTP 访问时,用插件自动改配置并跳转 HTTPS:

sudo certbot --nginx -d example.com -d www.example.com --agree-tos -m admin@example.com --redirect
sudo certbot certificates
sudo certbot renew --dry-run

Apache 把插件换成 python3-certbot-apache,命令为 sudo certbot --apache -d example.com -d www.example.com --agree-tos -m admin@example.com --redirect。若不想让 Certbot 改配置,用 webroot 只签发:

sudo mkdir -p /var/www/letsencrypt/.well-known/acme-challenge
sudo certbot certonly --webroot -w /var/www/letsencrypt -d example.com -d www.example.com --agree-tos -m admin@example.com
ls -l /etc/letsencrypt/live/example.com/

签发后重点看 fullchain.pem 和 privkey.pem:Web 服务器必须用 fullchain,不能只配 cert.pem,否则安卓、小程序和部分 Java 客户端会报证书链不完整。Certbot 的 systemd 定时器通常每天检查两次,可执行 systemctl list-timers | grep certbot 确认;续期后重载服务用 certbot renew --deploy-hook 'systemctl reload nginx'。

实操二:acme.sh 签发泛域名并自动续期

泛域名只能走 DNS-01。以 Cloudflare 为例,在账户创建仅限 Zone:DNS:Edit 的 API Token,导入环境变量后签发:

export CF_Token='你的Cloudflare_API_Token'
~/.acme.sh/acme.sh --issue --dns dns_cf -d example.com -d '*.example.com' --keylength ec-256
~/.acme.sh/acme.sh --install-cert -d example.com --ecc --key-file /etc/nginx/ssl/example.com.key --fullchain-file /etc/nginx/ssl/example.com.fullchain.pem --reloadcmd 'systemctl reload nginx'

阿里云 DNS 用 --dns dns_ali 并设置 Ali_Key、Ali_Secret;腾讯云用 --dns dns_tencent。首次可加 --staging 验证 TXT 写入和传播。acme.sh 默认安装 cron:

crontab -l | grep acme.sh
~/.acme.sh/acme.sh --cron --home ~/.acme.sh
~/.acme.sh/acme.sh --list

注意 DNS API 的 TXT 记录可能被 CDN 的 CNAME 冲突覆盖,_acme-challenge.example.com 不要手工建 A 记录。泛域名证书不会自动覆盖新业务子域,新增子域若不在 *.example.com 层级需重新签发或改用多域名证书。

配置示例:Nginx、Apache 与 IIS 绑定证书

Nginx 最小可用配置,必须同时开 80 跳转和 443 TLS:

server {
listen 80;
server_name example.com www.example.com;
location /.well-known/acme-challenge/ { root /var/www/letsencrypt; }
location / { return 301 https://$host$request_uri; }
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
add_header Strict-Transport-Security 'max-age=31536000' always;
location / { proxy_pass http://127.0.0.1:8080; }
}

Apache 在 VirtualHost *:443 中写 SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem、SSLCertificateKeyFile .../privkey.pem,并执行 apachectl configtest 后重载。IIS 推荐 win-acme 自动申请:

wacs.exe --target iis --siteid 1 --host example.com --installation iis --accepttos --emailaddress admin@example.com
Import-PfxCertificate -FilePath C:/certs/example.com.pfx -CertStoreLocation Cert:/LocalMachine/My -Password (ConvertTo-SecureString -String 'PfxPassword' -AsPlainText -Force)
Get-ChildItem Cert:/LocalMachine/My | Where-Object {$_.Subject -like '*example.com*'}

IIS 绑定 443 时勾选 SNI,主机名填 example.com,并在中间证书机构中确认已导入 Intermediate。多个站点共用 443 必须用 SNI,否则会串证。

证书类型与部署方式对比表

选择方式时不要只看“免费”,要看续期是否自动、是否支持泛域名、能否覆盖 IIS。Let’s Encrypt 证书 90 天有效,公开 TLS 证书上限 398 天;自动续期失败后 30 天内必须修复。

方式 适用场景 自动续期 泛域名 备注
Certbot + Nginx/Apache 单域名/多域名 是,systemd timer 否,需 DNS 插件 改配置少,适合 Linux Web
acme.sh + DNS API 泛域名/多域名 是,cron 是 需 DNS API 权限
win-acme + IIS IIS 站点 是,Windows 计划任务 是,DNS 验证 自动绑定,注意私钥权限
Certbot manual DNS 临时测试 否 是 不适合生产
商业 OV/EV 企业展示 视产品 是 有效期也不超过 398 天

若只跑 Nginx 单站点,优先 Certbot;有泛域名和 CDN 多子域,优先 acme.sh + DNS API;Windows IIS 用 win-acme;临时测试用 staging。不要用 manual DNS 长期生产,因为续期需人工。

常见错误排查(真实报错与修复)

1)Challenge failed for domain example.com 伴随 Timeout during connect (likely firewall problem):80 被安全组、CDN 或 Nginx 默认站点拦截。执行 curl -I http://example.com/.well-known/acme-challenge/test,确认返回 200 而不是 301/403;云 WAF 要放行该路径。

2)too many certificates already issued for exact set of domains:正式环境同一域名集一周内重复签发触发限流。测试改用 --staging,等待限流窗口恢复,不要反复重试。

3)浏览器报 NET::ERR_CERT_AUTHORITY_INVALID 或 Java 报 unable to get local issuer certificate:只部署了 leaf 证书。Nginx/Apache 改用 fullchain.pem;IIS 导入 PFX 时包含中间证书;用 openssl verify -CAfile chain.pem cert.pem 验证。

4)ERR_SSL_PROTOCOL_ERROR 或 sslv3 alert handshake failure:协议或套件不匹配。配置 ssl_protocols TLSv1.2 TLSv1.3;,关闭 TLS1.0/1.1;老客户端需升级,不要为兼容牺牲安全。

5)小程序报 request:fail ssl hand shake error:优先查证书链、系统时间、域名备案和 TLS 版本。执行 echo | openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts,看到 Verify return code: 0 (ok) 才算链完整。

6)IIS 报 Cannot find the certificate 或 443 绑定后访问串站:证书需在 LocalMachine/My,私钥授予 IIS_IUSRS 读取权限,绑定勾选 SNI 并填写主机名;PFX 密码错误会报 The specified network password is not correct。

效果验证:浏览器、OpenSSL、小程序与续期演练

先验证 HTTP 跳转和证书链:

curl -I http://example.com
curl -I https://example.com
echo | openssl s_client -connect example.com:443 -servername example.com -showcerts
echo | openssl s_client -connect example.com:443 -servername example.com -tls1_2
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates

输出中 Verify return code: 0 (ok) 表示链验证通过;notAfter 应为未来 90 天左右。再检查 HSTS、TLS 版本和证书 SAN:openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -text | grep -A1 'Subject Alternative Name'。小程序后台“开发管理→开发设置→服务器域名”添加 https://api.example.com,要求域名已备案、证书受信任、不能是 IP/localhost;保存后用真机请求 /health 验证,不要只在小程序开发者工具里测。

最后做续期演练:sudo certbot renew --dry-run;acme.sh 执行 ~/.acme.sh/acme.sh --cron --home ~/.acme.sh;IIS 在 win-acme 中查看计划任务和最近运行日志。续期后必须重载 Nginx/Apache 或重启 IIS 站点,否则磁盘证书已更新但内存仍是旧证书。监控证书剩余天数,低于 30 天告警。

常见问题

Let’s Encrypt 公开信任证书有效期为 90 天,建议每 60 天自动续期;公开 TLS 证书最长有效期受规则限制为 398 天,因此不要上传超长有效期证书。

HTTP-01 只能验证具体域名,无法为未枚举的泛域名逐个放置验证文件;泛域名必须使用 DNS-01,在 _acme-challenge 下写入 TXT 记录完成验证。

先查域名是否 ICP 备案、是否填成 IP/localhost、证书链是否完整、TLS 是否 1.2 以上、系统时间是否准确;用 openssl s_client 确认 Verify return code 为 0。

必须配 fullchain.pem。cert.pem 只有站点证书,缺少中间证书会导致安卓、小程序、Java 等客户端报证书链不完整,fullchain.pem 才包含完整链。

检查 PFX 是否包含中间证书、证书是否导入 LocalMachine/My、443 绑定是否勾选 SNI 并填对主机名,同时给 IIS_IUSRS 私钥读取权限,再重启站点。

续期只更新磁盘文件,Nginx/Apache 需 reload,IIS 需重启站点或重新绑定;在 renew 命令加 deploy-hook,或让 acme.sh 的 reloadcmd 执行重载。

内容审核

长期从事 SSL/TLS 证书部署与 HTTPS 运维,负责本站证书选型、安装配置与故障排查内容的编写与审核。