跳到主要内容
Apache

SSL.com SSL 证书 Apache 部署实战:fullchain、证书链与排错指南

问题背景与根因:Apache 只配叶子证书为何“浏览器正常、程序报错”

SSL.com 是美国 CA,支持企业采购流程与多种付款方式,产品线覆盖 DV、OV、EV 与文档签名证书。把 SSL.com 签发的证书部署到 Apache 时,最常见的问题不是私钥错,而是证书链不完整:SSLCertificateFile 只放了站点叶子证书,没有把中间证书按“叶子证书 → 中间证书”的顺序合并。浏览器通常可通过 AIA 补链或缓存正常显示小锁,但 curl、Java、微信小程序、部分 SDK 会严格校验证书链,直接报 unable to get local issuer certificate 或 PKIX path building failed。

另一个根因是版本与文件格式不匹配。Apache 2.4.8+ 支持 PEM,推荐把站点证书与中间证书合并为 fullchain,并让 SSLCertificateFile 指向该文件;Apache 2.2 及 2.4.8 以下需要单独配置 SSLCertificateChainFile。公开可信 TLS 证书的单张最长有效期目前为 398 天,Let’s Encrypt 默认有效期为 90 天,续期后必须 reload/restart Apache 并确认配置指向新文件。

快速确认环境:

apache2 -v
openssl version -a
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null | grep -E 's:|i:'

若输出中只看到站点证书和根证书、缺少中间证书,或 Java/curl 报链错误,优先检查 fullchain。CSR 只是申请证书时提交的公钥与主题信息,不包含私钥;部署阶段真正需要的是签发后的 .crt、中间证书和未泄露的 .key。

完整实操步骤:生成 CSR、准备 fullchain 与私钥

以下命令可直接在 Linux 执行。先生成 RSA 2048 位私钥和 CSR,并把常用域名写入 SAN;若使用 ECDSA,将 rsa:2048 改为 ec:<curve> 或单独用 ecparam 生成。私钥权限必须收紧。

mkdir -p /etc/ssl/sslcom/example.com
cd /etc/ssl/sslcom/example.com
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'
chmod 600 example.com.key
openssl req -in example.com.csr -noout -subject -text | grep -A2 'Subject Alternative Name'
openssl req -in example.com.csr -noout -verify

收到 SSL.com 证书后,确认文件类型:站点证书 example.com.crt、中间证书 intermediate.crt、私钥 example.com.key。fullchain 的顺序必须是叶子证书在前、中间证书在后;不要放根证书,根证书应留在客户端信任库。合并并检查:

cat example.com.crt intermediate.crt > example.com_fullchain.crt
awk 'BEGIN{c=0} /BEGIN CERTIFICATE/{c++} END{print c}' example.com_fullchain.crt
openssl x509 -in example.com.crt -noout -subject -issuer -dates
openssl verify -CAfile root.crt -untrusted intermediate.crt example.com.crt

若只有 .pfx/.p12,先转换:openssl pkcs12 -in bundle.pfx -clcerts -nokeys -out example.com.crt 与 openssl pkcs12 -in bundle.pfx -nocerts -nodes -out example.com.key。转换后仍要按上述顺序合并 fullchain。CSR 与私钥需匹配:用同一条命令分别提取公钥指纹比对,避免申请时用 A 私钥、部署时用 B 私钥。

openssl x509 -in example.com.crt -noout -pubkey | openssl pkey -pubin -outform der | openssl md5
openssl pkey -in example.com.key -pubout -outform der | openssl md5

Apache 配置示例:2.4.8+ fullchain 与旧版 ChainFile 写法

Debian/Ubuntu 先启用模块;RHEL/CentOS 用 dnf 安装 mod_ssl 并检查模块。配置前备份站点 vhost,避免 reload 失败影响线上。

sudo a2enmod ssl headers
sudo apachectl configtest
sudo systemctl restart apache2
# RHEL/CentOS
sudo dnf install -y mod_ssl
sudo httpd -M | grep -E 'ssl|headers'

Apache 2.4.8+ 推荐配置如下,SSLCertificateFile 指向 fullchain,不再配置 SSLCertificateChainFile。TLS 只保留 1.2/1.3,HSTS 仅在 HTTPS 稳定后启用;若站点还有 HTTP 入口,增加 301 跳转。

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    SSLEngine on
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
    SSLCertificateFile      /etc/ssl/sslcom/example.com/example.com_fullchain.crt
    SSLCertificateKeyFile   /etc/ssl/sslcom/example.com/example.com.key
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    DocumentRoot /var/www/html
</VirtualHost>

Apache 2.2/2.4.8 以下需分开配置:SSLCertificateFile 放叶子证书,SSLCertificateChainFile 放中间证书。不要在两个指令中重复放同一张中间证书。修改后先 apachectl configtest,输出 Syntax OK 再 systemctl reload apache2;reload 不生效时再 restart,并检查是否有多个 vhost 抢占 443。

# 旧版示例
SSLCertificateFile    /etc/ssl/sslcom/example.com/example.com.crt
SSLCertificateChainFile /etc/ssl/sslcom/example.com/intermediate.crt
SSLCertificateKeyFile /etc/ssl/sslcom/example.com/example.com.key

证书文件与 Apache 版本对比表:该用 fullchain 还是 ChainFile

判断标准不是证书品牌,而是 Apache 版本与文件内容。下表可直接对照;若同一台服务器有多个 vhost,逐个检查 SSLCertificateFile 是否指向对应域名的 fullchain,避免串证。

场景 Apache 版本 SSLCertificateFile SSLCertificateChainFile 说明
推荐写法 2.4.8+ 叶子证书 + 中间证书的 fullchain 不配置 PEM 顺序:站点证书在前,中间证书在后
旧版写法 2.2/2.4.8 以下 仅叶子证书 中间证书 ChainFile 只放中间证书,不放根证书
仅叶子证书 任意 example.com.crt 无 浏览器可能正常,curl/Java 常报链错误
fullchain 含根证书 任意 叶子 + 中间 + 根 无 不推荐,增加体积且可能引发兼容问题
SSL.com DV/OV/EV 2.4.8+ 对应域名 fullchain 不配置 证书最长有效期 398 天,按到期日续期
Let’s Encrypt 2.4.8+ fullchain.pem 不配置 默认 90 天,自动化续期后 reload

文件内容检查:grep -c 'BEGIN CERTIFICATE' example.com_fullchain.crt 正常应大于等于 2;若为 1,说明只有叶子证书。再用 openssl crl2pkcs7 -nocrl -certfile example.com_fullchain.crt | openssl pkcs7 -print_certs -noout 查看 subject/issuer 链。若输出中出现根证书 subject 与 issuer 相同,说明根证书被误合并,应移除。

对比 fullchain 与分开配置:fullchain 便于迁移和容器化,减少漏配 ChainFile;分开配置在老版本 Apache 上更清晰,但容易在续期时只替换叶子证书而忘记中间证书。无论哪种方式,私钥都必须是 600 权限,且不能放在 Web 根目录。CSR 文件可归档,但不要与私钥一起公开。

常见错误排查:真实报错与逐条修复

报错 1:curl: (60) SSL certificate problem: unable to get local issuer certificate。说明服务端未发送完整中间证书,或客户端 CA 库过旧。先在服务端执行 openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null,检查证书数量与 issuer;修复为 fullchain 后 reload。客户端临时验证可加 --cacert,但生产应修服务端链。

报错 2:Java 报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target。Java 不依赖浏览器 AIA 补链,必须收到中间证书。检查 fullchain 顺序,确认没有把根证书当中间证书;必要时把企业自建 CA 根证书导入 Java truststore:keytool -importcert -alias corp-root -file root.crt -keystore cacerts。

报错 3:Apache AH02565: Certificate and private key … do not match。核对公钥指纹:openssl x509 -in example.com.crt -noout -pubkey | openssl pkey -pubin -outform der | openssl md5 与 openssl pkey -in example.com.key -pubout -outform der | openssl md5 必须一致。RSA 也可用 openssl x509 -noout -modulus 与 openssl rsa -noout -modulus 比对。不一致只能换回匹配私钥或重签证书。

报错 4:AH02572: Failed to configure at least one certificate and key for example.com:443。多为路径错误、权限不足或 PEM 被破坏。用 ls -l 检查 Apache 运行用户可读,openssl x509 -in fullchain.crt -noout 检查文件可解析;SELinux 环境执行 restorecon -Rv /etc/ssl/sslcom/example.com。

报错 5:SSL_ERROR_RX_RECORD_TOO_LONG。通常是 443 端口被 HTTP 服务占用,或客户端用 https 访问了纯 HTTP 端口。检查 ss -lntp | grep ':443' 与 vhost 的 SSLEngine on。续期后仍旧证书:确认配置指向新文件,执行 systemctl reload apache2;多进程/容器要滚动重启,并清理 CDN 缓存。

效果验证:openssl s_client、curl、浏览器与自动化检查

部署完成后不要只看浏览器小锁。先验证证书链、有效期、SAN 和 TLS 版本。以下命令中 -servername 对 SNI 多站点必须带;-verify_return_error 让链验证失败时直接退出。

openssl s_client -connect example.com:443 -servername example.com -showcerts -verify_return_error </dev/null
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
openssl s_client -connect example.com:443 -servername example.com -tls1_2 </dev/null | grep 'Protocol'
curl -I https://example.com
curl -I https://www.example.com

检查 HSTS 是否返回:curl -sI https://example.com | grep -i strict-transport-security。检查 HTTP 跳转:curl -I http://example.com 应返回 301/308 且 Location 为 https。检查证书剩余天数:openssl x509 -in example.com_fullchain.crt -noout -checkend 2592000,返回 not expire 表示 30 天内不过期;用于监控时每天执行。

自动化验证建议:用 openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt -untrusted intermediate.crt example.com.crt 验证链;用 curl --fail --silent --show-error https://example.com/health 验证业务;用 Java 或小程序 SDK 各跑一次真实请求,因为它们的信任库与浏览器不同。最后确认 443 仅启用 TLS 1.2/1.3:openssl s_client -connect example.com:443 -servername example.com -tls1_1 </dev/null 应失败。

若验证通过,记录证书序列号、到期日、fullchain 路径和 reload 命令,纳入变更记录。公开证书最长 398 天,Let’s Encrypt 90 天,监控至少提前 30 天告警;不要等浏览器提示过期再处理。

续期与运维:398 天与 90 天有效期下的 reload 策略

公开可信 TLS 证书单张最长有效期为 398 天,SSL.com 的 DV/OV/EV 证书按签发规则通常不超过该上限;Let’s Encrypt 默认 90 天。续期不是覆盖文件就结束,Apache 进程必须重新加载证书。先备份旧文件,再替换 fullchain 与私钥,执行 configtest 后 reload。

cp -a example.com_fullchain.crt example.com_fullchain.crt.bak.$(date +%F)
cp -a example.com.key example.com.key.bak.$(date +%F)
# 替换新证书与私钥后
sudo apachectl configtest
sudo systemctl reload apache2
sudo systemctl status apache2 --no-pager
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -serial -dates -subject

Certbot 场景:certbot renew --dry-run 先演练,确认 deploy hook 会 reload Apache。示例 hook:certbot renew --deploy-hook 'systemctl reload apache2'。若使用容器或负载均衡,证书可能挂在网关而非 Apache,需在网关和回源同时检查;CDN 要单独上传新证书,否则源站更新后边缘仍旧证书。

续期后仍显示旧证书的排查顺序:1)openssl x509 -in 配置路径 -noout -serial -dates 看文件是否新;2)apachectl -S 看 443 vhost 实际加载的配置;3)systemctl reload apache2 是否成功;4)是否有多个 Apache 实例或容器未重启;5)CDN/代理缓存。最后用 openssl s_client 比对 serial 与文件 serial,一致才算生效。

常见问题

不需要。2.4.8+ 推荐把站点证书与中间证书合并为 fullchain,并让 SSLCertificateFile 指向该文件;不要再配置 SSLCertificateChainFile。只有 Apache 2.2/2.4.8 以下才用 ChainFile 单独指定中间证书。


执行 openssl s_client -connect 域名:443 -servername 域名 -showcerts,若只看到叶子证书、缺少中间证书,或 curl 报 unable to get local issuer certificate、Java 报 PKIX path building failed,基本可判定链不完整,应改用 fullchain。


分别提取公钥指纹比对:openssl x509 -in cert -noout -pubkey 与 openssl pkey -in key -pubout 后转 DER 做 md5,结果必须一致。RSA 也可用 openssl x509 -noout -modulus 与 openssl rsa -noout -modulus 比对。


先确认配置路径指向新 fullchain,执行 apachectl configtest 后 systemctl reload apache2;再用 openssl s_client 比对 serial 与文件 serial。若前面有 CDN、负载均衡或容器,需同步更新并滚动重启,清理缓存。


公开可信 TLS 证书单张最长有效期为 398 天,SSL.com 的 DV/OV/EV 证书按签发规则不超过该上限;Let’s Encrypt 默认有效期为 90 天。两者续期后都要 reload/restart Apache 并验证链完整。

内容审核

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