HTTPS 部署与 SSL 数字证书配置安装完整指南
问题背景与根因:为什么 HTTPS 部署不是装个证书就结束
原文把 SSL 证书当作一次性商品,并默认提供一年有效期,这个说法已经过时。公开信任 TLS 证书在 2020-09 至 2026-03 的历史最长有效期为 398 天;按 CA/B Forum 计划,2026-03-15 起新签发证书最长 200 天,2027-03-15 起 100 天,2029-03-15 起 47 天。Let’s Encrypt 仍为 90 天。因此不能再按一年证书规划,必须把自动续期、证书链、私钥权限、443 端口和 HTTP 301 跳转一起纳入部署。HTTPS 失败的根因通常不是证书没买,而是域名 SAN 不匹配、中间证书缺失、私钥与证书不配对、安全组未放行 443、或续期任务未执行。小程序、iOS ATS 和现代浏览器都要求可信 TLS,DV 证书已能满足大多数个人站和企业展示站,OV/EV 主要增加组织身份审核,EV 不再让浏览器地址栏常驻公司名。部署前先用命令确认现状:openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName。如果输出中没有目标域名、日期已过期或 issuer 不是受信任 CA,就应先处理证书再改配置。证书是域名身份、密钥、证书链、TLS 协议、端口和续期机制的组合,任何一项缺失都会导致浏览器报错。
申请前准备:域名、DNS、CSR、私钥与 443 端口
申请前准备四类材料:目标域名及管理权限、可添加 DNS TXT 记录的账号、Web 服务器类型与版本、可对外访问的 80/443 端口。单域名证书只覆盖一个 FQDN;多域名证书通过 SAN 覆盖多个域名;通配符证书覆盖 *.example.com 一级子域,但不覆盖 example.com 本身,也不覆盖 a.b.example.com。生成私钥和 CSR 时建议使用 RSA 2048 或 ECDSA P-256,并把 SAN 写全:openssl req -new -newkey rsa:2048 -sha256 -nodes -keyout example.com.key -out example.com.csr -subj '/C=CN/ST=Beijing/L=Beijing/O=Example/CN=example.com' -addext 'subjectAltName=DNS:example.com,DNS:www.example.com'。执行后立即收紧权限:chmod 600 example.com.key。检查 CSR:openssl req -in example.com.csr -noout -text | grep -E 'Subject:|DNS:'。商业 CA 通常要求 DNS TXT 验证,记录名可能是 _dnsauth.example.com 或 _acme-challenge.example.com,用 dig +short TXT _dnsauth.example.com 和 dig @8.8.8.8 TXT _dnsauth.example.com +short 检查传播。服务器侧确认监听:ss -lntp | grep ':443';防火墙放行:firewall-cmd --add-service=https --permanent && firewall-cmd --reload 或 ufw allow 443/tcp。云上 ECS 还要在安全组入方向放行 TCP 443,HTTP-01 验证还需 80。私钥不要长期交给第三方保存,代维完成后应更换或收回。
完整实操:Let’s Encrypt 自动签发与商业证书安装流程
推荐优先使用 ACME 自动签发。Debian/Ubuntu 上执行:apt update && apt install certbot python3-certbot-nginx,然后签发并自动改 Nginx:certbot --nginx -d example.com -d www.example.com --agree-tos -m ops@example.com --redirect。验证续期:certbot renew --dry-run;查看定时器:systemctl list-timers certbot.timer。若 80 端口不可用,用 DNS-01:certbot certonly --manual --preferred-challenges dns -d example.com -d www.example.com,按提示添加 TXT 记录并等待传播。使用 acme.sh 的示例:curl https://get.acme.sh | sh -s email=ops@example.com,然后 ~/.acme.sh/acme.sh --issue -d example.com -d www.example.com --nginx,安装证书:~/.acme.sh/acme.sh --install-cert -d example.com --key-file /etc/nginx/ssl/example.com.key --fullchain-file /etc/nginx/ssl/fullchain.pem --reloadcmd 'systemctl reload nginx'。商业证书流程是:提交 CSR,完成 DNS TXT 或文件验证,CA 签发后下载 fullchain 证书和私钥,把 fullchain.pem 配到服务器,私钥单独保存。Let’s Encrypt 有效期 90 天,商业公开信任证书当前新签发最长 200 天,都必须自动续期。不要只下载 leaf 证书,Nginx 的 ssl_certificate 应指向包含中间证书的 fullchain 文件;否则部分 Android、Java 客户端会报 unable to get local issuer certificate。
Nginx 配置示例:301 跳转、TLS 1.2/1.3、HSTS 与证书链
Nginx 最小可用配置如下。80 端口只做 301,443 端口加载 fullchain 和私钥,启用 TLS 1.2/1.3,关闭不安全协议。将域名和路径替换为实际值后执行 nginx -t && systemctl reload nginx。
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
http2 on;
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;
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_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
add_header Strict-Transport-Security 'max-age=31536000' always;
root /var/www/html;
index index.html;
}
HSTS 首次上线不要直接加 includeSubDomains 和 preload,先用较小 max-age 观察;确认所有子域都支持 HTTPS 后再扩大。证书路径必须存在且 Nginx 进程可读,私钥权限建议 600。若使用商业证书,把 fullchain 和 key 放到 /etc/nginx/ssl/ 并保持同样配置。验证配置语法时常见报错 nginx: [emerg] cannot load certificate ... PEM_read_bio_X509_AUX() failed,多数是路径错误、文件不是 PEM 或权限不足。配置完成后用 curl -I http://example.com 应返回 301,curl -I https://example.com 应返回 200 或业务状态码。
Apache、IIS 与 Tomcat 配置示例
Apache 2.4 配置:80 跳转和 443 加载证书。Apache 2.4.8 及以上可让 SSLCertificateFile 指向 fullchain;旧版本需单独配置 SSLCertificateChainFile。修改后执行 apachectl configtest && systemctl reload httpd 或 systemctl reload apache2。
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /etc/ssl/example.com/fullchain.pem
SSLCertificateKeyFile /etc/ssl/example.com/example.com.key
</VirtualHost>
IIS 把 CA 签发的 .pfx 导入本地计算机个人证书存储,在站点绑定中新增 https、端口 443、主机名 example.com,并勾选 SNI。PowerShell 示例:New-WebBinding -Name 'Default Web Site' -Protocol https -Port 443 -HostHeader example.com -SslFlags 1。Tomcat 需要 JKS 或 PKCS12:openssl pkcs12 -export -in fullchain.pem -inkey example.com.key -out example.com.p12 -name tomcat,再导入 JKS。server.xml 中配置 <Connector port='443' protocol='org.apache.coyote.http11.Http11NioProtocol' SSLEnabled='true' scheme='https' secure='true' keystoreFile='/opt/tomcat/conf/example.com.jks' keystorePass='changeit'/>。Tomcat 常见报错 java.io.IOException: Keystore was tampered with, or password was incorrect 表示密码错误或 JKS 生成不完整。IIS 若报证书不存在,检查绑定主机名与证书 SAN 是否一致,以及是否导入到计算机账户而不是当前用户。
证书类型与部署格式对比表
选择证书先看覆盖范围,再看验证等级。DV 只验证域名控制权,几分钟可签发;OV 验证企业身份,通常 1 至 5 个工作日;EV 审核更严,但现代浏览器不再把公司名常驻地址栏。多数小程序、博客、下载站和内部系统用 DV 足够。若一个证书要覆盖多个独立域名,选多域名 SAN 证书;若覆盖大量一级子域,选通配符。公开信任证书当前新签发最长 200 天,Let’s Encrypt 90 天,历史 398 天上限已不适用于 2026-03-15 后的新证书。部署格式也要匹配服务器。
| 类型/项目 | 验证内容 | 签发时间 | 适用场景 | 注意事项 |
|---|---|---|---|---|
| DV | 域名控制权 | 几分钟到数小时 | 个人站、小程序、下载站 | 最常见,支持自动化 |
| OV | 域名+企业身份 | 1-5 工作日 | 企业门户、SaaS | 需营业执照等材料 |
| EV | 严格企业审核 | 数天到数周 | 金融、电商品牌站 | 浏览器不再常驻公司名 |
| 单域名 | 一个 FQDN | 同验证等级 | 单站点 | 不覆盖 www 需另加 |
| 多域名 SAN | 多个 FQDN | 同验证等级 | 多业务域名 | 注意 SAN 数量上限 |
| 通配符 | *.example.com | 需 DNS 验证 | 大量一级子域 | 不覆盖主域名和二级子域 |
| Let’s Encrypt | DV | 自动签发 | 可自动化站点 | 90 天,必须自动续期 |
| 商业 DV | DV | 分钟级 | 需要发票/服务支持 | 当前最长 200 天 |
部署格式:Nginx 用 fullchain.pem + privkey.pem;Apache 用 fullchain.pem + key,旧版加 chain;IIS 用 pfx;Tomcat 用 JKS 或 PKCS12。不要把私钥上传到公开仓库,不要用同一私钥跨多个不相关域名,证书到期前至少提前 30 天确认续期任务正常。
- NGINX / APACHE / TOMCAT / IIS 证书配置 · SSL证书安装
- APP/小程序 HTTPS · SSL网站加密证书 · CA证书配置
- 中小企业云服务器安全代运维(防攻击方案)
常见错误排查:真实报错与命令
1. 浏览器报 NET::ERR_CERT_COMMON_NAME_INVALID 或 curl 报 SSL: no alternative certificate subject name matches target host name:证书 SAN 不含访问域名。检查:openssl x509 -in cert.pem -noout -text | grep -A1 'Subject Alternative Name',重新签发时同时加入 example.com 和 www.example.com。2. 报 NET::ERR_CERT_DATE_INVALID 或 curl: (60) SSL certificate problem: certificate has expired:证书过期或系统时间错误。执行 date -u 和 openssl x509 -in fullchain.pem -noout -dates,修复时间同步并续期。3. 报 curl: (60) SSL certificate problem: unable to get local issuer certificate:缺中间证书。Nginx 的 ssl_certificate 必须指向 fullchain.pem;用 openssl s_client -connect example.com:443 -servername example.com -showcerts 检查链。4. Nginx 启动报 nginx: [emerg] cannot load certificate ... PEM_read_bio_X509_AUX() failed:路径错误、文件不是 PEM、权限不足。执行 nginx -t,检查 ls -l 和 tail -n 50 /var/log/nginx/error.log。5. 443 连接超时:ss -lntp | grep 443 看监听,firewall-cmd --add-service=https --permanent,ufw allow 443/tcp,云安全组放行 TCP 443。6. Tomcat 报 Keystore was tampered with, or password was incorrect:keystorePass 与生成 JKS 时不一致,重新导出并核对密码。7. 页面报 Mixed Content:把 http:// 资源改为 https:// 或加 CSP upgrade-insecure-requests。8. HSTS 配错导致无法回退:首次不要 preload,先小 max-age 观察。
效果验证与自动化巡检
部署后不要只看浏览器绿锁。先验证证书链和 SAN:openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName。再验证跳转:curl -I http://example.com 应返回 301 且 Location 为 https;curl -I https://example.com 应返回业务状态码。检查 HSTS:curl -sI https://example.com | grep -i strict-transport-security。检查 TLS 版本:openssl s_client -connect example.com:443 -servername example.com -tls1_1 应失败,TLS 1.2/1.3 应成功。检查到期:openssl x509 -checkend 2592000 -noout -in fullchain.pem; echo $?,返回 0 表示 30 天内不过期。自动续期验证:certbot renew --dry-run,certbot certificates,systemctl list-timers | grep certbot;acme.sh 用 ~/.acme.sh/acme.sh --list 和 ~/.acme.sh/acme.sh --cron。生产环境建议加 blackbox_exporter 或云监控拨测 443,并监控证书剩余天数。商业证书没有自动续期时,建立到期前 30 天工单。最后用 SSL Labs 或 testssl.sh example.com 做外部评级,确认没有弱协议、弱加密套件和证书链缺失。
常见问题
证书现在还能签一年吗?
不能按一年规划。公开信任 TLS 证书历史最长 398 天,2026-03-15 起新签发最长 200 天,后续还会缩短到 100 天和 47 天;Let’s Encrypt 为 90 天。必须自动续期。
Let’s Encrypt 90 天证书需要手动续吗?
不需要。certbot 或 acme.sh 安装后会配置定时任务,执行 certbot renew --dry-run 验证即可。关键是 80 或 DNS 验证通道长期可用。
DV、OV、EV 怎么选?
个人站、小程序、下载站选 DV;企业门户需要组织信息可选 OV;金融电商若合规要求可选 EV。EV 不再让浏览器地址栏常驻公司名,不必为显示公司名多花钱。
通配符证书能覆盖主域名吗?
不能。*.example.com 只覆盖一级子域,不覆盖 example.com,也不覆盖 a.b.example.com。需要把主域名加入 SAN 或单独签发。
443 端口已放行为什么还超时?
检查云安全组、系统防火墙和监听地址:ss -lntp | grep 443。若只监听 127.0.0.1,外部仍不可达;Nginx 应监听 443 或 0.0.0.0:443。
证书链需要单独配置吗?
Nginx 直接把 fullchain.pem 配到 ssl_certificate;Apache 2.4.8+ 可用 fullchain,旧版加 SSLCertificateChainFile;IIS 导入 pfx 时包含链;Tomcat 生成 JKS 时导入完整链。