跳到主要内容
ssl证书按装

CA 证书与 HTTPS 配置服务实战:Nginx/Apache/Tomcat/IIS/Caddy 部署、排错与验证

CA 证书与 HTTPS 配置服务实战:Nginx/Apache/Tomcat/IIS/Caddy 部署、排错与验证 — CA 证书与 HTTPS 配置服务实战:Nginx/Apache/Tomcat/IIS/Caddy 部署、排错与验证

本文面向需要把网站从 HTTP 迁移到 HTTPS 的运维与开发人员,覆盖 CA 证书配置、Let’s Encrypt 免费证书签发、Nginx/Apache/Tomcat/IIS/Caddy 部署、证书链修复、续期自动化和真实报错排查。服务边界不变:只做 HTTPS 技术实施,不售卖 CA 证书;没有证书时可配置 Let’s Encrypt。

问题背景与根因:HTTPS 配置为什么失败

HTTPS 是经 TLS 加密的 HTTP,解决浏览器到服务器之间的传输加密与身份可信问题,但不修复网站漏洞。CA 签发证书时,CSR 中的公钥和域名会进入证书,最终通过 SAN 绑定实际访问域名。配置失败通常不是单一原因,而是 DNS 解析、80/443 端口、域名绑定、证书链、私钥匹配、服务器权限、网站混合内容七类根因叠加。

原文强调云服务器才可安装证书,虚拟主机通常不具备 443 监听、证书目录写入和 SNI 绑定能力;部署前必须告知 Web 服务器类型与安装目录。证书有效期也要按新规规划:公共 TLS 证书单张最长有效期已从旧资料的 398 天缩短为 200 天(2026-03-15 起),后续还会继续缩短;Let’s Encrypt 默认 90 天,并可申请更短周期证书。不要把 398 天或 825 天当作续期依据。

先做四项硬检查:

dig +short A example.com
dig +short AAAA example.com
ss -lntp | grep -E ':80|:443'
openssl x509 -in cert.pem -noout -subject -issuer -dates -ext subjectAltName

如果 SAN 不含 www,浏览器会报域名不匹配;如果只部署站点证书而缺少中间链,curl 会报 unable to get local issuer certificate。私钥是否匹配可用模数比对:

openssl x509 -noout -modulus -in cert.pem | openssl md5
openssl rsa -noout -modulus -in privkey.pem | openssl md5

两个 md5 必须一致。EC 证书改用 openssl pkey -pubout 比对公钥。域名绑定错误、证书链不完整、私钥不匹配是 HTTPS 配置失败中最常见、也最容易误判为“证书没生效”的三类根因。

准备清单:证书、CSR、DNS、端口与服务器权限

先确认证书来源:已有商业证书需准备 fullchain.pem、privkey.pem、chain.pem,或 .crt/.key/.ca-bundle;没有证书则用 Let’s Encrypt。生成 CSR 时把主域名和 www 都写入 SAN,避免后续补签:

openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr -subj '/CN=example.com' -addext 'subjectAltName=DNS:example.com,DNS:www.example.com'
openssl req -in example.com.csr -noout -text | grep -A1 'Subject Alternative Name'

DNS 必须指向当前服务器:A 记录给 IPv4,AAAA 给 IPv6;使用 CDN 时要在源站或 CDN 控制台上传证书,否则回源会失败。端口检查用 ss -lntp | grep -E ‘:80|:443’,防火墙放行 80/443:

ufw allow 80,443/tcp
firewall-cmd --add-service={http,https} --permanent && firewall-cmd --reload

云安全组也要放行。权限方面需要 root 或 sudo,Nginx/Apache 的证书目录必须可读;自定义证书目录在 SELinux 下可能需 chcon -R -t httpd_sys_content_t /etc/nginx/ssl。虚拟主机通常无法修改 443 监听和证书绑定,应改用云服务器/VPS。IIS 需要管理员权限和 PFX;Tomcat 需要 PKCS12/JKS;Caddy 自动 HTTPS 要求 80/443 可达。部署前把服务器类型、安装目录、域名列表、证书文件路径四项信息确认清楚,可显著减少返工。

完整实操步骤:用 Certbot 签发并部署 Let’s Encrypt 证书

以 Ubuntu/Debian + Nginx 为例,安装插件并一键签发。–redirect 会自动把 HTTP 跳转到 HTTPS:

sudo apt update && sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com --email admin@example.com --agree-tos --no-eff-email --redirect

RHEL/Rocky 用 dnf install -y certbot python3-certbot-nginx;Apache 用 python3-certbot-apache,然后执行 certbot –apache。若 Web 服务器不方便自动改配置,用 webroot 手动签发:

sudo mkdir -p /var/www/html/.well-known/acme-challenge
sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com --email admin@example.com --agree-tos --no-eff-email

Nginx 需放行验证路径:location ^~ /.well-known/acme-challenge/ { root /var/www/html; }。签发后证书在 /etc/letsencrypt/live/example.com/ 下:fullchain.pem 是站点证书加中间链,privkey.pem 是私钥,chain.pem 是中间链。不要手工复制到别处,续期会更新软链。

续期检查必须做:sudo certbot renew –dry-run;sudo certbot certificates 查看到期时间;systemctl list-timers | grep certbot 确认 timer 存在。若用 cron,可写 0 3 * * * /usr/bin/certbot renew –quiet –deploy-hook ‘systemctl reload nginx’。只续期不重载,浏览器仍可能读到旧证书。申请失败时先访问 http://example.com/.well-known/acme-challenge/test,确认 80 端口没有被 CDN、防火墙或旧跳转拦截。

配置示例:Nginx、Apache、Tomcat、IIS、Caddy

Nginx 最小可用配置如下,注意使用 fullchain.pem,并关闭 TLS 1.0/1.1:

server {
    listen 80;
    server_name example.com www.example.com;
    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;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains' always;
    location / { root /var/www/html; index index.html; }
}

Apache 在 VirtualHost 中启用 SSL,2.4.8 以上直接用 fullchain:

<VirtualHost *:443>
  ServerName example.com
  ServerAlias www.example.com
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  Protocols h2 http/1.1
  Header always set Strict-Transport-Security 'max-age=31536000; includeSubDomains'
  DocumentRoot /var/www/html
</VirtualHost>

Tomcat 需先转 PKCS12,再改 server.xml 的 Connector。keystorePass 不要用默认值:

sudo openssl pkcs12 -export -in /etc/letsencrypt/live/example.com/fullchain.pem -inkey /etc/letsencrypt/live/example.com/privkey.pem -out /etc/tomcat/example.p12 -name tomcat -CAfile /etc/letsencrypt/live/example.com/chain.pem -caname root -password pass:ChangeMe
<Connector port='8443' protocol='org.apache.coyote.http11.Http11NioProtocol'
  SSLEnabled='true' scheme='https' secure='true'
  keystoreFile='/etc/tomcat/example.p12' keystoreType='PKCS12'
  keystorePass='ChangeMe' />

IIS 导入 PFX 后在站点绑定 443 并勾选 SNI:certutil -f -p ChangeMe -importPFX C:/certs/example.pfx;也可用 New-WebBinding -Name ‘Default Web Site’ -Protocol https -Port 443 -HostHeader example.com -SslFlags 1。Caddy 最简单,Caddyfile 写域名和反代即可自动申请与续期:

example.com {
    encode gzip
    reverse_proxy 127.0.0.1:8080
}

修改后分别执行 nginx -t、apachectl configtest、caddy validate –config /etc/caddy/Caddyfile,再 reload。Tomcat 改完需重启,IIS 需确认绑定和证书存储为 LocalMachine/My。

证书链、TLS 参数、HSTS 与自动续期

证书链是 HTTPS 配置的高频坑。fullchain.pem 包含站点证书和中间证书,chain.pem 只有中间链;Nginx 必须用 fullchain,Apache 2.4.8+ 用 fullchain 替代旧 SSLCertificateChainFile,Tomcat 导入 PKCS12 时用 -CAfile 带入链。验证链是否完整:

openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt /etc/letsencrypt/live/example.com/fullchain.pem
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates

输出 Verify return code: 0 (ok) 才算链正常。TLS 参数建议只开 TLSv1.2 和 TLSv1.3;Apache 用 SSLProtocol -all +TLSv1.2 +TLSv1.3。HSTS 要等全站 HTTPS 稳定后再加,先 max-age=300 观察,再升到 31536000;includeSubDomains 和 preload 要谨慎,错误提交后很难回退。

OCSP stapling 需按 CA 现状处理:Let’s Encrypt 已逐步停用 OCSP、转向 CRL,不要照旧资料强行配置;商业证书可配 ssl_stapling on、ssl_stapling_verify on、ssl_trusted_certificate chain.pem 和 resolver。自动续期必须同时做两件事:certbot renew –dry-run 验证续期,deploy-hook 重载 Web 服务器。Nginx 用 systemctl reload nginx,Apache 用 systemctl reload apache2,Tomcat 需重新导出 PKCS12 并重启,IIS 用 win-acme 安装后自动更新绑定。续期后抽查 openssl x509 -noout -dates -in cert.pem,确认 notAfter 已更新。

方案对比表格:证书类型与 Web 服务器配置差异

证书类型先按验证方式和有效期选,不要只看价格。下表把常见方案放在一起对比:

方案 验证方式 默认有效期 信任范围 适用场景 注意
Let’s Encrypt DV,HTTP-01 或 DNS-01 90 天,可选短期 主流浏览器 个人站、中小企业站 免费,自动续期;不售卖
商业 DV 域名验证 最长 200 天(2026-03 起) 全浏览器 企业官网、活动页 需购买,注意 SAN 数量
商业 OV/EV 组织验证 最长 200 天 全浏览器 金融、政企、招投标 EV 不再显示绿色地址栏
自签名 无 自定义 需手动信任 内网测试 浏览器会告警,不适合公网

Web 服务器差异主要在证书格式和重载方式:

Web 服务器 证书格式 配置入口 自动续期 关键点
Nginx PEM fullchain + key server 块 certbot timer + reload 必须 fullchain
Apache PEM fullchain + key VirtualHost certbot timer + reload 2.4.8+ 用 fullchain
Tomcat PKCS12/JKS server.xml Connector 脚本转换 + 重启 keystorePass 与转换密码一致
IIS PFX 站点绑定 443/SNI win-acme 需管理员,导入 LocalMachine/My
Caddy 自动管理 Caddyfile 内置 80/443 必须可达

选择时还要考虑:证书 SAN 是否覆盖 www、m、api 等子域;是否有 CDN 需要上传证书;是否使用国密或内网 CA。云服务器才能改 443 和证书目录,虚拟主机通常只能使用面板提供的 SSL 上传功能。有效期规划按 200 天以内执行,Let’s Encrypt 仍按 90 天默认周期自动续期。

常见错误排查:真实报错与处理

下面这些报错在 HTTPS 配置中很典型,按报错定位比反复重装证书有效:

  • SSL: certificate subject name ‘example.com’ does not match target host name ‘www.example.com’,或浏览器 NET::ERR_CERT_COMMON_NAME_INVALID:证书 SAN 缺域名。重新签发时加 -d example.com -d www.example.com,用 openssl x509 -noout -ext subjectAltName 核对。泛域名必须用 DNS-01。
  • curl: (60) SSL certificate problem: unable to get local issuer certificate:链不完整。Nginx 改用 fullchain.pem,Apache 用 fullchain,Tomcat 导入 chain.pem,IIS 用含完整链的 PFX。
  • nginx: [emerg] cannot load certificate … BIO_new_file() failed:路径错或权限不足。用 namei -l /etc/letsencrypt/live/example.com/fullchain.pem 检查目录权限,证书 644、私钥 600,确保 Nginx master 可读。
  • bind() to 0.0.0.0:443 failed (98: Address already in use):443 被占用。ss -lntp | grep ‘:443’ 找进程,停掉旧服务或改监听端口。
  • Problem binding to port 80: Could not bind to IPv4 or IPv6:80 被占用、防火墙或安全组未放行。ss -lntp | grep ‘:80’,再用 curl -I http://example.com 验证。
  • Tomcat 报 java.io.IOException: Keystore was tampered with, or password was incorrect:keystorePass 与导出密码不一致。重新导出 PKCS12,并用 keytool -list -v -keystore example.p12 -storetype PKCS12 验证。
  • IIS 报 The server does not support the certificate private key:PFX 未含私钥或导入到错误存储。导入 LocalMachine/My,绑定 443 时勾选 SNI;虚拟主机无法完成此操作。
  • 浏览器 Console 报 Mixed Content: The page at ‘https://…’ was loaded over HTTPS, but requested an insecure resource ‘http://…’:页面硬编码 http。把图片、JS、CSS、接口改为 https 或相对协议,并检查反向代理 X-Forwarded-Proto。
  • DNS problem: NXDOMAIN looking up A for example.com:DNS 未生效或解析错。dig +short A example.com 确认,等待 TTL 或修正记录。

排查顺序建议:先看 DNS 和 80/443,再看证书 SAN 和链,最后看私钥、Web 服务器配置和网站混合内容。虚拟主机无法安装证书不是配置技巧问题,而是权限与架构限制。

效果验证:从命令行到浏览器

配置完成后不要只看浏览器是否出现锁标志,按下面顺序验证:

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 https://example.com
curl -I http://example.com
ss -lntp | grep ':443'
nginx -t
apachectl configtest
caddy validate --config /etc/caddy/Caddyfile
certbot certificates
certbot renew --dry-run

curl -I https://example.com 应返回 HTTP/2 200 或 301;curl -I http://example.com 应返回 301 到 https。openssl 输出中 Verify return code 应为 0,SAN 必须包含实际访问域名。Nginx/Apache/Caddy 配置测试通过后再 reload,Tomcat 重启后检查 logs/catalina.out 是否有 Connector 启动异常。

在线检查可用 SSL Labs,目标 A 或 A+;重点看证书链完整、TLS 1.0/1.1 关闭、HSTS 是否生效。浏览器开发者工具 Security 面板可看证书链,Console 看 Mixed Content,Network 看 301/200。日志排查用 tail -f /var/log/nginx/error.log、journalctl -u nginx -n 50、tail -f /var/log/apache2/error.log。续期后再次执行 openssl x509 -noout -dates -in /etc/letsencrypt/live/example.com/cert.pem,确认 notAfter 更新,并确认 Web 服务器已 reload。

常见问题

可以。本服务不售卖证书,只做配置;没有证书时可申请 Let’s Encrypt 免费 DV 证书,默认有效期 90 天,通过 HTTP-01 或 DNS-01 验证域名,Certbot 自动续期。前提是域名已解析到云服务器/VPS,80 或 443 可达,且有 root 或管理员权限;虚拟主机通常无法安装。


虚拟主机通常只给控制面板和共享 IP,不能写 Web 服务器 443 监听、证书目录和 SNI 绑定,也无法重启 Nginx/Apache/IIS。证书安装需要服务器管理员权限、独立 443 端口和可修改的站点配置。请改用云服务器/VPS,或让主机商在面板中提供 SSL 上传功能。


398 天是旧规则。按 CA/B Forum 计划,2026 年 3 月 15 日起公共 TLS 证书单张最长有效期为 200 天,2027 年起 100 天,2029 年起 47 天。Let’s Encrypt 默认仍为 90 天,并可申请更短周期证书。续期策略应按 200 天以内规划,不要照搬 398 天或 825 天旧资料。


先看浏览器 Console 是否报 Mixed Content。常见原因是页面硬编码 http:// 的图片、JS、CSS、接口或 CDN 资源。把资源改为 https:// 或相对协议,数据库和模板中批量替换 http:// 为 https://,并检查反向代理的 X-Forwarded-Proto。若系统限制端口或写死 http,需要改代码或加代理。


常见报错是 SSL: certificate subject name ‘example.com’ does not match target host name ‘www.example.com’,浏览器显示 NET::ERR_CERT_COMMON_NAME_INVALID。原因是证书 SAN 未包含实际访问域名。重新签发时同时加 -d example.com -d www.example.com,并用 openssl x509 -noout -ext subjectAltName 核对。泛域名需用 DNS-01 验证。

内容审核

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