跳到主要内容
ssl证书按装

NGINX、Apache、Tomcat、IIS SSL 证书安装配置全指南(2026 版)

NGINX、Apache、Tomcat、IIS SSL 证书安装配置全指南(2026 版) — NGINX、Apache、Tomcat、IIS SSL 证书安装配置全指南(2026 版)

问题背景与根因:HTTP 明文、证书链与有效期变化

浏览器已把 HTTP 页面标记为“不安全”,小程序、支付、摄像头、API 回调等场景也普遍强制 HTTPS。SSL/TLS 解决的不只是加密,还包括服务器身份认证与传输完整性。很多“证书装了但不生效”的根因并不是证书没买对,而是只部署了叶子证书、缺少中间证书,或私钥与证书不匹配。

2026 年必须注意有效期规则:CA/Browser Forum SC-081 生效后,公共信任 TLS 证书最长有效期自 2026-03-15 起已从 398 天缩短为 200 天,2027-03-15 起进一步缩短为 100 天;Let’s Encrypt 仍为 90 天。因此不能再按“一年一装”管理,应建立到期监控与自动续期。

快速诊断可先执行:

curl -I http://example.com
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null

若输出中出现 unable to get local issuer certificate、key values mismatch、证书链不完整,分别对应缺中间证书、私钥不匹配、链文件顺序错误。下面按通用准备、四类服务器配置、对比、排错与验证展开。

证书选型、申请与通用文件准备

按验证等级分为 DV、OV、EV:DV 几分钟签发,适合个人站点、测试、API;OV 需 3-5 个工作日审核企业身份;EV 曾用于地址栏显示组织名,但主流浏览器已弱化该 UI,金融、电商更多是合规选型。按域名覆盖分为单域名、多域名、通配符。通配符 *.example.com 只覆盖一级子域,不覆盖 example.com,也不覆盖 a.b.example.com。

申请前准备域名管理权限做 DNS-01 TXT 验证,并确认 Web 服务器版本。生成私钥与 CSR:

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'
openssl req -in example.com.csr -noout -text | grep -A1 'Subject Alternative Name'

签发后合并证书链,顺序为服务器证书在前、中间证书在后,不要放根证书:

cat example.com.crt intermediate.crt > fullchain.pem
openssl verify -CAfile root.crt -untrusted intermediate.crt example.com.crt
openssl x509 -in fullchain.pem -noout -dates -subject -issuer

私钥权限建议 600,属主为 root 或服务运行账户。Nginx、Apache 使用 PEM;Tomcat 使用 PKCS12/JKS;IIS 使用 PFX。部署前先确认 fullchain.pem 中含 2 张以上证书,且私钥未加密或已正确配置密码。

NGINX 安装配置:fullchain 与私钥分离

创建目录并上传文件,证书放 fullchain.pem,私钥放 example.com.key:

mkdir -p /etc/nginx/ssl
chmod 700 /etc/nginx/ssl
chmod 600 /etc/nginx/ssl/example.com.key
chmod 644 /etc/nginx/ssl/fullchain.pem

在 server 块中启用 443。Nginx 1.25.1+ 建议使用 http2 on;旧版本用 listen 443 ssl http2。配置示例:

server {
    listen 443 ssl;
    http2 on;
    server_name example.com www.example.com;
    ssl_certificate /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;
    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;
    root /var/www/html;
}
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

执行 nginx -t 检查语法,再平滑重载:

nginx -t && systemctl reload nginx
curl -I https://example.com
curl -I http://example.com

若 Nginx 报 SSL_CTX_use_PrivateKey_file failed,优先检查证书与私钥是否同一对。若浏览器提示证书链不完整,确认 ssl_certificate 指向 fullchain.pem,而不是单张 example.com.crt。

Apache 安装配置:SSLCertificateFile 与链证书

Debian/Ubuntu 执行 a2enmod ssl;RHEL/CentOS 安装 mod_ssl。Apache 2.4.8 及以后可将 fullchain.pem 直接配置到 SSLCertificateFile;更老版本才需要 SSLCertificateChainFile 单独指定中间证书。

a2enmod ssl
apachectl configtest
systemctl reload apache2

VirtualHost 配置示例:

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html
    SSLEngine on
    SSLCertificateFile /etc/httpd/ssl/fullchain.pem
    SSLCertificateKeyFile /etc/httpd/ssl/example.com.key
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
    Header always set Strict-Transport-Security max-age=31536000
</VirtualHost>
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

检查配置并重载:

apachectl configtest && systemctl reload apache2
curl -I https://example.com
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null

常见报错 AH02565: Certificate and PrivateKey do not match 表示证书与私钥不匹配;AH02564 多与链文件缺失有关。RHEL 上服务名通常是 httpd,重载命令改为 systemctl reload httpd。

Tomcat 安装配置:PKCS12/JKS 与 Connector

Tomcat 传统使用 JKS,现代版本也支持 PKCS12,部分版本可直接配置 PEM。先用 OpenSSL 生成 PKCS12,再转为 JKS:

openssl pkcs12 -export -in fullchain.pem -inkey example.com.key -out example.com.p12 -name tomcat -CAfile chain.pem -caname root -password pass:ChangeIt
keytool -importkeystore -deststorepass ChangeIt -destkeypass ChangeIt -destkeystore example.com.jks -srckeystore example.com.p12 -srcstoretype PKCS12 -srcstorepass ChangeIt -alias tomcat
keytool -list -v -keystore example.com.jks -alias tomcat

在 conf/server.xml 中配置 8443 Connector;若前面有 Nginx/Apache 反向代理,可只监听内网 8443:

<Connector port='8443' protocol='org.apache.coyote.http11.Http11NioProtocol' maxThreads='200' SSLEnabled='true' scheme='https' secure='true'>
    <SSLHostConfig>
        <Certificate certificateKeystoreFile='/opt/tomcat/conf/example.com.jks' certificateKeystorePassword='ChangeIt' certificateKeyAlias='tomcat' type='RSA' />
    </SSLHostConfig>
</Connector>

重启并验证:

systemctl restart tomcat
curl -k -I https://127.0.0.1:8443
keytool -list -v -keystore /opt/tomcat/conf/example.com.jks -alias tomcat

若报 Keystore was tampered with, or password was incorrect,检查 storepass、keypass、alias 是否一致;若报 No available certificate or key corresponds to the SSL cipher suites,通常是别名错误或证书链未导入。Tomcat 9+ 也可用 certificateFile 指向 fullchain.pem、certificateKeyFile 指向私钥。

IIS 安装配置:PFX 导入、SNI 绑定与权限

IIS 使用 PFX。先在 Linux 或 OpenSSL 环境导出包含私钥和链的 PFX:

openssl pkcs12 -export -out example.com.pfx -inkey example.com.key -in fullchain.pem -certfile chain.pem -password pass:ChangeIt

在 Windows PowerShell 管理员窗口导入到本地计算机个人存储:

$pwd = ConvertTo-SecureString -String 'ChangeIt' -AsPlainText -Force
Import-PfxCertificate -FilePath C:/ssl/example.com.pfx -CertStoreLocation Cert:LocalMachineMy -Password $pwd
Get-ChildItem Cert:LocalMachineMy | Where-Object {$_.Subject -like '*example.com*'} | Select Thumbprint,Subject

创建 SNI 绑定,SslFlags 1 表示启用 SNI:

New-WebBinding -Name 'Default Web Site' -Protocol https -Port 443 -HostHeader 'example.com' -SslFlags 1
$cert = Get-Item Cert:LocalMachineMy<THUMBPRINT>
New-Item -Path 'IIS:SslBindings.0.0.0!443!example.com' -Value $cert -Force

也可用 netsh 绑定:

netsh http add sslcert hostnameport=example.com:443 certhash=<THUMBPRINT> appid={4dc3e181-e14b-4a21-b022-59fc669b0914} certstorename=MY

若出现 0x80070005 或 A specified logon session does not exist,在 certlm.msc 中右键证书→所有任务→管理私钥,给 IIS_IUSRS 读取权限;同时确认站点绑定勾选 SNI,且 HostHeader 与证书 SAN 一致。

四类 Web 服务器配置对比与选型表

项目 NGINX Apache Tomcat IIS
证书格式 PEM:fullchain.pem + key PEM:fullchain.pem + key JKS/PKCS12,部分版本支持 PEM PFX
链证书处理 合并进 fullchain.pem 2.4.8+ 合并进 SSLCertificateFile;老版本用 ChainFile 导入时包含完整链 PFX 中包含链
配置入口 nginx.conf / conf.d/*.conf httpd-ssl.conf / sites-available conf/server.xml IIS 管理器或 PowerShell
重载命令 nginx -t && systemctl reload nginx apachectl configtest && systemctl reload apache2 systemctl restart tomcat iisreset /restart
SNI 默认支持 默认支持 由 Connector 处理 绑定需 SslFlags 1
常见坑 证书与私钥不匹配、缺链 老版本漏配 ChainFile JKS 密码/别名错误 私钥权限、HostHeader 不一致

选型建议:Nginx 和 Apache 适合直接终止 TLS,也常作为 Tomcat 的反向代理;Tomcat 若直接对外,需同时管理 JKS 与 Connector;IIS 必须关注证书存储、SNI 和私钥 ACL。容器环境优先把证书挂载为只读 Secret,避免打进镜像。

常见错误排查与效果验证

真实报错与处理:

  • nginx: [emerg] SSL_CTX_use_PrivateKey_file failed (SSL: error:0B080074:X509_check_private_key:key values mismatch):私钥与证书不匹配。用 openssl x509 -noout -modulus -in fullchain.pem | openssl md5 与 openssl rsa -noout -modulus -in example.com.key | openssl md5 对比。
  • curl: (60) SSL certificate problem: unable to get local issuer certificate:缺中间证书。Nginx 用 fullchain,Apache 配 fullchain 或 ChainFile,Tomcat 导入完整链,IIS 的 PFX 要包含链。
  • AH02565: Certificate and PrivateKey do not match:Apache 的 SSLCertificateFile 与 SSLCertificateKeyFile 配错。
  • java.io.IOException: Keystore was tampered with, or password was incorrect:JKS 密码或别名错误,执行 keytool -list -v 验证。
  • 浏览器 Mixed Content:页面仍引用 http:// 资源,改为 https:// 或相对协议,并检查 CSP。
  • IIS 0x80070005:私钥权限未授予 IIS_IUSRS,或绑定 HostHeader 与证书不匹配。

验证命令:

curl -I https://example.com
curl -I http://example.com
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates
ss -lntp | grep ':443'
openssl x509 -checkend 2592000 -noout -in fullchain.pem
certbot renew --dry-run

浏览器 DevTools→Security 可查看证书链;再用 SSL Labs 检查评分与链完整性。HSTS 先设置较短 max-age 测试。续期后 Nginx 执行 nginx -t && systemctl reload nginx,Apache 执行 apachectl graceful。openssl x509 -checkend 2592000 返回非 0 表示 30 天内到期,应接入监控告警。

常见问题

证书最长有效期到底是 398 天还是 200 天?

398 天是旧规则。自 2026-03-15 起,公共信任 TLS 证书最长有效期为 200 天;2027-03-15 起缩短为 100 天。Let’s Encrypt 仍为 90 天,应使用自动续期而不是手工年检。

Let’s Encrypt 证书也是 90 天吗?续期怎么配?

是,Let’s Encrypt 公共证书有效期为 90 天,官方建议在到期前 30 天续期。Certbot 可执行 certbot renew –dry-run 演练,并用 systemd timer 或 cron 定时运行 certbot renew,续期后 reload Nginx/Apache。

通配符证书能覆盖主域名和二级子域吗?

*.example.com 只覆盖 a.example.com 这类一级子域,不覆盖 example.com,也不覆盖 a.b.example.com。若主域名也要用,需在 SAN 中同时加入 example.com,或分别申请证书。

为什么浏览器地址栏不显示公司名?EV 证书还有用吗?

主流浏览器已弱化 EV 地址栏展示,不能把“显示公司名”作为唯一目标。EV 仍可用于合规、品牌与特定行业要求,但普通站点优先保证证书链完整、TLS 版本正确和自动续期。

Tomcat 必须用 JKS 吗?

不必。Tomcat 支持 JKS、PKCS12,较新版本也支持 PEM。关键是 Connector 中的 certificateKeystoreFile、password、alias 与实际文件一致;若用 PEM,则配置 certificateFile 与 certificateKeyFile。

如何确认中间证书是否缺失?

执行 openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null,应看到服务器证书和至少一张中间证书。若 curl 报 unable to get local issuer certificate,而浏览器正常,通常是客户端未拿到完整链。

内容审核

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