跳到主要内容
ssl证书按装

SSL证书申请安装配置全流程:Nginx/Apache/Tomcat/IIS 实操与排错

SSL证书申请安装配置全流程:Nginx/Apache/Tomcat/IIS 实操与排错 — SSL证书申请安装配置全流程:Nginx/Apache/Tomcat/IIS 实操与排错

问题背景与根因:为什么“证书装上了”仍会出现 HTTPS 告警

原文把 SSL 证书安装配置拆成 Nginx、Apache、Tomcat、IIS 四类,这个方向没有错,但只列平台名称不足以落地。实际故障通常不是“证书没买”,而是申请材料、私钥、证书链、协议版本、端口监听和重定向没有形成闭环。公共信任 TLS 证书自 2020 年 9 月起单张最长有效期按 398 天执行,Let’s Encrypt 证书有效期为 90 天,因此“装一次管多年”的旧思路必须改为自动续期。本文讨论的是配置服务:基础价 100 元,只提供配置、调试和故障排查,不包含 SSL 证书本身,证书需用户提前购买;质保期外在线支持按服务定价。

根因主要有六类:一是私钥与证书不匹配,Nginx 报 key values mismatch;二是只部署了站点证书,没有部署中间证书,移动端和 curl 报 unable to get local issuer certificate;三是 80 端口未跳转 443,或页面内仍有 http:// 资源导致混合内容;四是 Tomcat 仍用旧 JKS 或密码错误;五是 IIS 绑定未勾选 SNI 或证书私钥权限不足;六是证书到期未续期。排查前先确认版本和监听:

openssl version -a
nginx -v
apache2 -v
curl -I http://example.com
curl -I https://example.com
ss -lntp | grep -E ':80|:443'

如果 http 返回 200 而 https 超时,先查安全组和防火墙;如果 https 返回证书告警,先查 fullchain 和私钥;如果浏览器地址栏锁标正常但页面样式错乱,查混合内容。把“申请、部署、重载、验证、续期”五步做成清单,比反复重装证书有效。

完整实操:生成私钥、CSR 并申请证书

无论最终用哪个平台,第一步都是生成私钥和 CSR。私钥必须留在自己服务器,CSR 交给 CA。以下命令可直接执行,域名替换为实际域名,SAN 必须同时包含不带 www 和带 www 的域名:

mkdir -p /etc/ssl/private/example.com
cd /etc/ssl/private/example.com
openssl genrsa -out example.com.key 2048
chmod 600 example.com.key
openssl req -new -sha256 -key example.com.key -out example.com.csr -subj '/C=CN/ST=Zhejiang/L=Hangzhou/O=Example/CN=example.com' -addext 'subjectAltName=DNS:example.com,DNS:www.example.com'
openssl req -in example.com.csr -noout -subject -text | grep -A1 'Subject Alternative Name'
cat example.com.csr

商业 CA 下单时粘贴 CSR,签发后你会得到站点证书和中间证书。更常用的是 ACME 自动签发,以 Let’s Encrypt 为例,Webroot 方式要求 80 端口可访问:

apt-get update && apt-get install -y certbot
certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com --email admin@example.com --agree-tos --no-eff-email
ls -l /etc/letsencrypt/live/example.com/

重点认识四个文件:cert.pem 是站点证书,chain.pem 是中间证书,fullchain.pem 是站点证书加中间证书,privkey.pem 是私钥。Nginx 和 Apache 应使用 fullchain.pem 与 privkey.pem;Tomcat 和 IIS 需要先转 PKCS12/PFX。检查有效期和链:

openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -subject -issuer -dates
openssl verify -CAfile /etc/letsencrypt/live/example.com/chain.pem /etc/letsencrypt/live/example.com/cert.pem
openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -modulus | openssl md5
openssl rsa -in /etc/letsencrypt/live/example.com/privkey.pem -noout -modulus | openssl md5

两条 modulus 的 md5 必须一致。商业证书最长有效期按 398 天执行,Let’s Encrypt 为 90 天,建议在到期前 30 天续期。私钥权限保持 600,不要把 .key 放到 Web 根目录。CSR 中 CN 只写一个主域名,其他域名放 SAN;现代浏览器不只看 CN,缺少 SAN 会直接报错。

Nginx 配置示例:80 跳 443、证书链与 HSTS

Nginx 是最常见的入口层。下面配置实现 HTTP 永久跳转 HTTPS、TLS 1.2/1.3、证书链和 HSTS。将 example.com 替换为你的域名,proxy_pass 按实际后端修改:

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_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains' always;
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
    }
}

执行语法检查并平滑重载:

nginx -t
systemctl reload nginx
curl -I http://example.com
curl -I https://example.com
curl -sI https://example.com | grep -i 'strict-transport-security'

关键点:ssl_certificate 必须指向 fullchain.pem,只写 cert.pem 会缺中间证书;ssl_certificate_key 指向 privkey.pem,不要复制成 cert.pem;listen 443 ssl 不能漏 ssl 参数;新版本 Nginx 可用 listen 443 ssl 加 http2 on,旧写法 http2 仍可用但需确认版本。HSTS 第一次上线建议先用较短 max-age 验证,确认所有子域都支持 HTTPS 后再加 includeSubDomains。若后端应用生成 http:// 链接,需在应用层改 baseURL 或加 CSP upgrade-insecure-requests,否则会出现混合内容。

Apache、Tomcat、IIS 配置示例

Apache 使用 fullchain.pem 和 privkey.pem,80 跳转与 443 虚拟主机如下:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
    ServerName 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'
</VirtualHost>

检查并重载:apachectl configtest && systemctl reload apache2。新版 Apache 不再需要 SSLCertificateChainFile,fullchain.pem 已含中间证书。

Tomcat 不能直接读 PEM,先转 PKCS12:

openssl pkcs12 -export -in /etc/letsencrypt/live/example.com/fullchain.pem -inkey /etc/letsencrypt/live/example.com/privkey.pem -out /etc/tomcat/example.com.p12 -name tomcat -CAfile /etc/letsencrypt/live/example.com/chain.pem -caname root -password pass:ChangeMe
chown tomcat:tomcat /etc/tomcat/example.com.p12
chmod 600 /etc/tomcat/example.com.p12

在 server.xml 的 Connector 中配置:

<Connector port='8443' protocol='org.apache.coyote.http11.Http11NioProtocol'
           maxThreads='150' SSLEnabled='true' scheme='https' secure='true'
           clientAuth='false' sslProtocol='TLS'
           keystoreFile='/etc/tomcat/example.com.p12' keystoreType='PKCS12'
           keystorePass='ChangeMe' />

IIS 使用 PFX 和 PowerShell 绑定,主机头与 SNI 必须设置:

Import-PfxCertificate -FilePath 'C:/certs/example.com.pfx' -CertStoreLocation 'Cert:\LocalMachine\My' -Password (ConvertTo-SecureString -String 'ChangeMe' -AsPlainText -Force)
New-WebBinding -Name 'Default Web Site' -Protocol https -Port 443 -HostHeader 'example.com' -SslFlags 1
$thumbprint = (Get-ChildItem 'Cert:\LocalMachine\My' | Where-Object {$_.Subject -like '*CN=example.com*'}).Thumbprint
$binding = Get-WebBinding -Name 'Default Web Site' -Protocol https -HostHeader 'example.com'
$binding.AddSslCertificate($thumbprint,'my')
iisreset /noforce

Tomcat 改完需 systemctl restart tomcat;IIS 可用 iisreset /noforce 或重启站点。PFX 密码、别名和 keystorePass 必须一致,IIS 还需给 IIS_IUSRS 私钥读取权限。

平台对比表格:证书格式、配置入口与重载方式

平台 推荐格式 关键配置/命令 重载方式 常见坑
Nginx PEM:fullchain.pem + privkey.pem ssl_certificate、ssl_certificate_key、listen 443 ssl nginx -t && systemctl reload nginx 只配 cert.pem 缺中间证书;私钥不匹配
Apache PEM:fullchain.pem + privkey.pem SSLCertificateFile、SSLCertificateKeyFile、SSLEngine on apachectl configtest && systemctl reload apache2 旧指令 SSLCertificateChainFile 重复;路径权限
Tomcat PKCS12 或 JKS keystoreFile、keystoreType、keystorePass systemctl restart tomcat 密码/别名错误;PEM 未转换
IIS PFX Import-PfxCertificate、New-WebBinding、AddSslCertificate iisreset /noforce 未勾 SNI;私钥权限不足;主机头重复
Let’s Encrypt PEM,90 天 certbot certonly / renew deploy-hook 重载服务 80 端口不可达;续期未重载
商业证书 PEM/PFX,最长 398 天 CSR 提交、链文件合并 按平台重载 中间证书漏部署;到期未提醒

选择建议:Nginx/Apache 优先 PEM;Tomcat 用 PKCS12;IIS 用 PFX。Let’s Encrypt 免费但 90 天,必须自动续期;商业证书单张最长 398 天,也要设置到期监控。原文的 100 元是证书配置服务基础价,不含证书本身;商业证书费用由 CA 收取,Let’s Encrypt 为 0 元但需承担自动化和排障成本。若服务器在负载均衡后面,证书应部署在入口层,后端用 HTTP 并设置 X-Forwarded-Proto,避免后端误判协议。多域名站点不要用单域名证书硬撑,SAN 或通配符证书更合适,但通配符只覆盖一级子域。

常见错误排查:真实报错与修复命令

下面列出配置现场最常见的真实报错。先看报错原文,再按命令修复,不要直接重装。

1. Nginx 私钥不匹配。报错:nginx: [emerg] SSL_CTX_use_PrivateKey_file(...) failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch)。修复:分别计算证书和私钥 modulus,md5 必须一致:

openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -modulus | openssl md5
openssl rsa -in /etc/letsencrypt/live/example.com/privkey.pem -noout -modulus | openssl md5

不一致说明证书和私钥不是一对,重新签发或找回对应私钥。

2. Nginx 漏证书指令。报错:nginx: [emerg] no 'ssl_certificate' is defined for the 'listen ... ssl' directive in /etc/nginx/conf.d/example.conf:12。修复:在 listen 443 ssl 的 server 块内补 ssl_certificate 和 ssl_certificate_key,再 nginx -t。

3. Apache 证书路径错误。报错:AH00526: Syntax error on line 12 of /etc/apache2/sites-enabled/example-ssl.conf: SSLCertificateFile: file '/etc/ssl/certs/example.com.crt' does not exist or is empty。修复:ls -l 确认路径,SELinux 环境执行 restorecon,权限不足时给证书目录 755、私钥 600。

4. Tomcat 密钥库密码错误。报错:java.io.IOException: Keystore was tampered with, or password was incorrect。修复:重新执行 openssl pkcs12 -export,确认 keystorePass 与导出密码一致,keystoreType 写 PKCS12,别名不要写错。

5. IIS 绑定证书失败。报错:A specified logon session does not exist. It may already have been terminated. (Exception from HRESULT: 0x80070520)。修复:在证书管理器中给 IIS_IUSRS 私钥读取权限,删除重复 443 绑定,确认 SNI 已勾选,再 AddSslCertificate。

6. 证书链不完整。报错:curl: (60) SSL certificate problem: unable to get local issuer certificate。修复:Nginx/Apache 改用 fullchain.pem;Tomcat 导入 chain.pem;IIS 导入含中间证书的 PFX。用 openssl s_client 检查 Verify return code 是否为 0。

效果验证:从 curl 到 SSL Labs 的检查清单

部署完成后不要只看浏览器锁标。按下面命令逐项验证,输出应满足:http 返回 301 到 https;https 返回 200;证书 subject 含域名;issuer 为预期 CA;Verify return code 为 0;HSTS 头存在;HTTP/2 可用。

curl -I http://example.com
curl -I https://example.com
curl -vI https://example.com 2>&1 | grep -E 'SSL certificate verify|subject:|issuer:|expire'
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | grep 'Verify return code'
curl -sI https://example.com | grep -i 'strict-transport-security'
curl -I --http2 https://example.com

如果 Verify return code 不是 0,优先查中间证书;如果 curl 正常但手机报错,查证书链和旧 Android 兼容性;如果页面加载不完整,打开浏览器开发者工具 Console 查 Mixed Content,或执行 grep 搜页面中的 http:// 资源。多域名要分别验证 SAN 中的每个域名,尤其是 www 与裸域。HSTS 上线后浏览器会强制 HTTPS,回退困难,建议先用 max-age=300 验证,再逐步提高。最后用 SSL Labs 或 testssl.sh 扫描,目标 A 或 A+,重点看证书链、协议版本和弱套件。testssl.sh 可执行:

git clone --depth 1 https://github.com/drwetter/testssl.sh.git
cd testssl.sh
./testssl.sh --fast https://example.com

验证通过后记录证书到期时间,设置监控。可用 openssl 检查剩余天数:

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -checkend 2592000 -noout

返回 Certificate will not expire 表示 30 天内不会到期;无输出或报错则需立即续期。

自动化续期与运维:Let’s Encrypt 90 天与商业证书 398 天

Let’s Encrypt 证书 90 天到期,手工续期迟早出事。certbot 安装后通常自带 systemd timer,先检查:

systemctl list-timers | grep certbot
certbot renew --dry-run

dry-run 通过后配置 deploy-hook,续期成功自动重载服务:

certbot renew --quiet --deploy-hook 'systemctl reload nginx'

写入 crontab 可每天凌晨检查一次:

0 3 * * * /usr/bin/certbot renew --quiet --deploy-hook 'systemctl reload nginx'

Apache 将 deploy-hook 改为 systemctl reload apache2;Tomcat 需重启,可写 systemctl restart tomcat;IIS 建议用 win-acme 自动导入 PFX 并更新绑定。商业证书最长 398 天,虽然比 90 天长,但仍需到期监控。续期后必须验证新证书已生效:

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates -serial
curl -sI https://example.com | grep -i 'strict-transport-security'

运维要点:不要删除 /etc/letsencrypt/archive 和 renewal 目录;不要手工改 live 下的软链;多节点部署要把证书同步到每台入口机;负载均衡后面有多台后端时,证书只部署在入口层;使用通配符证书时注意 DNS-01 验证,HTTP-01 不能签发通配符。商业证书 398 天到期前 30 天联系 CA 续签,续签后按平台重新导出 PFX/PKCS12。配置服务只负责配置与排障,证书采购、CA 审核和质保外支持需按服务范围确认。

常见问题

不包含。该服务只提供 Nginx、Apache、Tomcat、IIS 的证书配置、调试和故障排查,证书需用户提前购买;Let’s Encrypt 可免费申请但有效期 90 天,商业证书最长 398 天,费用由 CA 收取。

最常见是只部署了站点证书,没有部署中间证书。Nginx 的 ssl_certificate 应指向 fullchain.pem,而不是 cert.pem;同时确认私钥与证书匹配,openssl modulus 的 md5 必须一致,再用 s_client 检查 Verify return code 是否为 0。

不需要手动。certbot 通常自带 systemd timer,执行 certbot renew –dry-run 验证后,用 deploy-hook 在续期成功后重载 Nginx 或 Apache。建议每天检查一次,并在到期前 30 天确认新证书已生效。

不能直接使用。Tomcat 需要先用 openssl pkcs12 转成 PKCS12,再在 server.xml 配置 keystoreFile、keystoreType 和 keystorePass;IIS 需要 PFX,通过 Import-PfxCertificate 导入并绑定,同时给 IIS_IUSRS 私钥读取权限。

398 天是公共信任 TLS 证书的单张最长有效期,不是永久有效;Let’s Encrypt 只有 90 天。到期前必须续期并重载服务,否则会出现证书过期告警。建议用 openssl x509 -checkend 或监控平台设置 30 天提醒。

内容审核

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