跳到主要内容
安装配置教程

apache https证书配置怎么做:多站点 SNI、证书链与平滑 reload 的 6 步清单(Apache 2.4.6x)

apache https证书配置 — apache https证书配置怎么做:多站点 SNI、证书链与平滑 reload 的 6 步清单(Apache 2.4.6x)
核心结论

apache https证书配置的核心是:在 443 虚拟主机中写 SSLEngine on,SSLCertificateFile 指向包含中间证书的 fullchain.pem,SSLCertificateKeyFile 指向 privkey.pem,apachectl configtest 通过后执行 apachectl graceful;Apache 2.4.8 起 SSLCertificateChainFile 已弃用。 证书链自检用 openssl x509 -in fullchain.pem -noout -issuer -ext subjectAltName;只部署 leaf 证书时,Java/Android 客户端常见报错是 unable to get local issuer certificate。 多站点靠 SNI:每个 443 vhost 配自己的 fullchain.pem,apachectl -S 查看默认 vhost;无 SNI 的老客户端只会拿到第一个证书。 验证生效看三条:apachectl configtest 输出 Syntax OK、openssl s_client 返回 Verify return code: 0 (ok)、curl -w '%{http_version}' 返回 2 表示 HTTP/2 已生效。 Let's Encrypt 90 天证书用 certbot renew --deploy-hook 'systemctl reload apache2' 自动续期,先 --dry-run 验证。

apache https证书配置为什么会出现“443 通了但证书不对”?6 个根因

“443 端口能连上”和“证书配对了”是两件独立的事。前者只说明 httpd 进程在监听,后者要经过 SNI 选 vhost、加载证书链、协议协商三道关。下面 6 个根因覆盖了 apache https证书配置 现场 90% 的“端口通、证书错/握手失败”工单,每条都给出可直接复制的验证命令。

根因 1:只验证了监听,没验证证书绑定。 ss 只读内核 socket 表,不碰 TLS 层;真正的证书要看 openssl s_client 的 subject= 与 Verify return code(0 才是链完整)。

# 1) 确认监听(有 LISTEN 不代表证书对)
ss -ltnp '( sport = :443 )'

# 2) 带 SNI 探测真实下发的证书链(关键:-servername 决定 Apache 选哪个 vhost)
openssl s_client -connect 127.0.0.1:443 -servername example.com -showcerts </dev/null 2>/dev/null 
  | openssl x509 -noout -subject -issuer -dates
# 期望:subject 含 CN=example.com;Verify return code: 0 (ok)
# 若出现 unable to get local issuer certificate → 链不全,见根因 2

根因 2:SSLCertificateFile 指向了单证书而非全链。 据 Let’s Encrypt 与 Apache 2.4 官方文档,自 2.4.8 起 推荐 SSLCertificateFile 直接指向 fullchain.pem(站点证书 + 中间证书),SSLCertificateChainFile 已标记废弃(仍可用,但混用易漏中间证书)。只放 domain.crt 时,Chrome/Firefox 因内置 AIA 补链可能“看起来正常”,而 Java、curl、Python requests 会直接报 unable to get local issuer certificate——这类“部分客户端报错”几乎全是缺中间证书。

# /etc/httpd/conf.d/ssl-example.conf  (Apache 2.4.6x)
SSLEngine on
SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem   # 站点证书+中间证书
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem     # 必须与证书公钥配对
# SSLCertificateChainFile 在 2.4.8+ 无需再配;配置了反而可能与 fullchain 重复下发
配置写法 下发内容 典型客户端结果
SSLCertificateFile domain.crt 仅站点证书 curl/Java 报 unable to get local issuer certificate
SSLCertificateFile fullchain.pem 站点 + 中间证书 Verify return code: 0,全客户端正常
SSLCertificateFile domain.crt + SSLCertificateChainFile chain.pem 等效全链 可用,但 2.4.8+ 已废弃该指令

根因 3:SNI 多站点的默认 vhost 顺序。 RFC 6066 定义的 SNI 要求客户端在 ClientHello 里带主机名;Apache 2.4.6x 配合 OpenSSL 1.1.1/3.x 能正确按 SNI 选 vhost,但不带 SNI 的旧客户端(老 Java 6/7、部分嵌入式设备、直接用 IP 访问)只会命中第一个 443 vhost 的证书。用 apachectl -S 看 default vhost 落在谁身上,把最重要的域名或一张多域名 SAN 证书放在首位。

apachectl -S | grep -A3 '443'
# 输出示例:
# *:443  is a NameVirtualHost
#          default server www.example.com (/etc/httpd/conf.d/ssl-example.conf:1)
#          port 443 namevhost www.example.com (...)
#          port 443 namevhost api.example.com (...)   <-- 无 SNI 客户端拿不到这张证书

根因 4:协议/套件收紧后握手直接失败。 按 Mozilla SSL Configuration Generator(intermediate 档)执行 SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1 后,只支持 TLS 1.0 的老客户端会在 ClientHello 阶段被打回,error_log 里出现 sslv3 alert handshake failure(对端报)或 no shared cipher(套件不交集)。SSLProtocol 在 2.4.x 的默认值是 all -SSLv3,即仍允许 TLS 1.0/1.1——这就是“改完配置才出问题”的原因。

根因 5:证书过期或服务器时间漂移。 有效期与系统时钟任一不满足都会失败,且报错文案不同:

openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -dates
# notBefore=...
# notAfter=...
timedatectl status | grep -E 'System clock|synchronized'   # 偏差 >5 分钟即可能触发下两条
# certificate is not yet valid  → 服务器时间早于 notBefore
# certificate has expired      → 服务器时间晚于 notAfter

根因 6:mod_ssl 未加载,configtest 直接失败。 这种情况下 Apache 根本起不来,谈不上“443 通”,但常被误判为证书问题。Debian/Ubuntu 用 a2enmod ssl headers http2,RHEL/CentOS 系 dnf install mod_ssl;报错固定为 AH00526: Syntax error ... Invalid command 'SSLEngine'。改完务必 apachectl configtest 通过再 systemctl reload httpd(graceful reload 不中断现有连接,是 apache https证书配置 变更后的标准动作)。

apache https证书配置的 6 步实操:从模块加载到 graceful reload

Apache 上配 HTTPS 出问题,九成不是证书错,而是版本、模块或 reload 方式没对齐。下面这份 6 步清单按顺序执行,每步都有可复制的验证命令。据 Apache HTTP Server 2.4 官方文档,SSLCertificateChainFile 自 2.4.8 起已废弃,证书链改为直接拼接进 SSLCertificateFile,这是很多人从旧教程照抄后报 AH02559 的根因。

步骤 1:确认版本与模块。先确认你手里是 2.4.62 还是更老的 2.4.6,模块名和指令行为在 2.4.x 内有差异。

# 查看版本(Debian/Ubuntu 用 apache2,RHEL 系用 httpd)
apache2 -v      # 示例输出:Server version: Apache/2.4.62 (Ubuntu)
httpd -v        # RHEL/Alma 示例:Server version: Apache/2.4.62 (AlmaLinux)

# Debian/Ubuntu:启用模块(ssl 必需,headers 供 HSTS,http2 可选)
a2enmod ssl headers http2
# RHEL/Alma/Rocky:装包即带 ssl 模块,无需 a2enmod
dnf install -y mod_ssl

# 验证模块已加载,应看到 ssl_module 与 http2_module
apachectl -M | grep -E 'ssl|http2'
#  ssl_module (shared)
#  http2_module (shared)

步骤 2:准备证书文件。Let’s Encrypt 的 certbot 默认输出到 /etc/letsencrypt/live/<域名>/,其中 fullchain.pem 已是 leaf → intermediate 的正确顺序,据 Let’s Encrypt 文档,切勿手工重排或把 root 证书塞进去。

ls -l /etc/letsencrypt/live/example.com/
# fullchain.pem  privkey.pem  chain.pem  cert.pem
chmod 600 /etc/letsencrypt/live/example.com/privkey.pem
chown root:root /etc/letsencrypt/live/example.com/privkey.pem

步骤 3:写 443 虚拟主机。注意不要再写 SSLCertificateChainFile。

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example
    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    # 默认值:SSLProtocol all -SSLv3;建议显式收紧
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
</VirtualHost>

步骤 4:80 跳 443。首次上线先用 302,确认无误再改 301——301 会被浏览器长期缓存,回滚极痛苦。

<VirtualHost *:80>
    ServerName example.com
    # 验证阶段用 302
    Redirect temp / https://example.com/
    # 确认后改为:Redirect permanent / https://example.com/
</VirtualHost>

步骤 5:HSTS 与 OCSP Stapling。HSTS 从 max-age=300 起步,稳定后再逐步加长(常见终值 31536000 秒),否则一旦配置出错,访客在有效期内无法回退 HTTP。

# 全局(httpd.conf 或 conf-available/ssl.conf):OCSP 缓存
SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"

# vhost 内追加
Header always set Strict-Transport-Security "max-age=300" env=HTTPS
SSLUseStapling on

步骤 6:语法检查与平滑生效。多站点生产环境不要 restart,它会断开在途连接;graceful 让老进程处理完现存请求再退出。

apachectl configtest          # 必须输出 Syntax OK
systemctl reload apache2      # Debian/Ubuntu
systemctl reload httpd        # RHEL/Alma
apachectl graceful            # 等效,适合无 systemd 场景

# 验证:应返回 TLS 1.2/1.3 与证书链信息
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null
操作 行为 适用场景
apachectl graceful / systemctl reload 重新读取配置,不中断在途连接 证书续期、vhost 变更(推荐)
systemctl restart 终止所有子进程,连接被断 换 MPM、改监听端口等结构性变更

多站点 SNI 无需额外开关:Apache 2.4 默认编译 mod_ssl 即启用 SNI,每个 <VirtualHost *:443> 带独立 ServerName 与证书即可。用 openssl s_client -servername a.example.com 与 -servername b.example.com 分别握手,返回不同 CN 即说明 SNI 分流正常。据 Mozilla SSL Configuration Generator,2.4.62 的现代兼容配置还应显式设置 SSLSessionTickets off 与 SSLHonorCipherOrder off(TLS 1.3 下前者影响前向安全)。

apache https证书配置示例:SNI 多站点、fullchain 与 OCSP Stapling 片段

问:一段能直接跑起来的 443 配置长什么样?

先把 mod_ssl、mod_headers、mod_http2 启用(Debian/Ubuntu 用 a2enmod ssl headers http2),再写入下面片段。fullchain.pem 必须是“叶子证书 + 中间证书”的拼接体——据 Let’s Encrypt 文档,只配 cert.pem 会让部分 Android 与 Java 客户端报 unable to get local issuer certificate。

# /etc/apache2/sites-enabled/example.com-le-ssl.conf
<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example.com

    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem   # 含中间证书
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem     # 权限 600,属主 root

    SSLProtocol           all -SSLv3 -TLSv1 -TLSv1.1   # 2.4 默认 all -SSLv3,这里显式收紧
    SSLCipherSuite        ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305  # 建议替换为 Mozilla intermediate 生成值
    Protocols             h2 http/1.1                  # 需 mod_http2;2.4 默认仅 http/1.1
</VirtualHost>

问:同一台机器上第二个域名怎么加,SNI 会不会串证书?

再开一个 *:443 的 vhost,只改 ServerName 与两条证书路径即可;Apache 2.4 在客户端发送 SNI 时按 ServerName 选证书。风险点在于“没有 SNI 的老客户端”(如 Python 2.7.8 以前、部分嵌入式设备)会落到 default vhost,所以务必核对:

api.example.com 的 vhost 里:ServerName api.example.com
SSLCertificateFile    /etc/letsencrypt/live/api.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/api.example.com/privkey.pem

$ apachectl -S
*:443  is a NameVirtualHost
         default server example.com (/etc/apache2/sites-enabled/example.com-le-ssl.conf:2)
         port 443 namevhost example.com (/etc/apache2/sites-enabled/example.com-le-ssl.conf:2)
         port 443 namevhost api.example.com (/etc/apache2/sites-enabled/api-le-ssl.conf:2)

第一行 default server 就是无 SNI 时的兜底站点,把它设成访问量最大、证书最“通用”的域名。

问:OCSP Stapling 怎么开、怎么验证?

全局(httpd.conf 或 ssl.conf)配缓存,vhost 内开启;缓存路径是相对 ServerRoot 的,写成绝对路径更稳:

SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"   # 未配置而 vhost 开了 SSLUseStapling,日志会告警 no SSLStaplingCache configured

# vhost 内
SSLUseStapling on
SSLStaplingResponderTimeout 5
SSLStaplingReturnResponderErrors off

$ openssl s_client -status -connect example.com:443 -servername example.com </dev/null 2>/dev/null | grep -A3 "OCSP Response Status"
    OCSP Response Status: successful (0x0)        # 若为 unsuccessful: tryLater (2) 或整段缺失,多为出站 80 端口被封

问:HSTS 一上来就写一年会不会出事?

会。includeSubDomains 一旦被浏览器缓存,任何仍走 HTTP 的子域立刻不可访问。建议两阶段:

# 上线首日:/etc/apache2/sites-enabled/example.com-le-ssl.conf 内
Header always set Strict-Transport-Security "max-age=300" env=HTTPS
# 确认图片、JS、API 全部 HTTPS 后改为:
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS

env=HTTPS 保证只在 TLS 连接上下发该头,需 mod_headers。

问:证书续期后怎么不断连接地生效?

用 --deploy-hook(仅续期成功时触发),reload 不杀进程、不断开已有连接:

certbot renew --deploy-hook 'systemctl reload apache2'    # Debian/Ubuntu;RHEL 系用 systemctl reload httpd

# 轮换前留底,便于回滚
install -m 600 /etc/letsencrypt/live/example.com/fullchain.pem 
        /etc/ssl/backup/example.com-fullchain-$(date +%F).pem
# 回滚:把 SSLCertificateFile 指回备份路径,然后
apachectl configtest && apachectl graceful

问:老系统上 TLS 1.3 到底能不能开?

环境 OpenSSL TLS 1.3 建议
Ubuntu 22.04 / Debian 12 + Apache 2.4.62 3.0.x 支持 用 Mozilla intermediate 配置,SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
RHEL 9 + Apache 2.4.5x 3.0.x 支持 同上,注意 SELinux 下证书路径上下文
CentOS 7 + Apache 2.4.6 1.0.2k 不支持 在 SSLCipherSuite 里写 TLS_AES_128_GCM_SHA256 不会生效,握手仍停在 TLS 1.2

据 Mozilla SSL Configuration Generator,intermediate 档兼容 TLS 1.2 及以上的主流客户端,适合绝大多数公开站点;modern 档要求 TLS 1.3,会切断旧 Android 与部分 IoT 设备。TLS 1.3 的套件由 OpenSSL 自身管理,SSLCipherSuite 只作用于 TLS 1.2 及以下,这是排障时最容易踩空的一点。

apache https证书配置选型表:证书文件、TLS 档位与生效方式

选型决定你在 Apache 2.4.6x 上少踩多少坑。先看证书文件:Apache 2.4.8 起支持把服务器证书与中间证书合并在同一个文件里,因此 fullchain.pem 配 SSLCertificateFile 是首选;据 Apache 官方文档 SSLCertificateChainFile 自 2.4.8 起已被标记弃用(deprecated),在 2.4.6x 上继续使用会触发 AH02559: The SSLCertificateChainFile directive is deprecated, SSLCertificateFile should be used instead 告警——它仍能工作,但升级到 2.4.8+ 后行为会变。

证书文件形态 适用版本 Apache 指令 结论
fullchain.pem(站点证书 + 中间证书) 2.4.8+ SSLCertificateFile 推荐,单一文件不易漏配
cert.pem + chain.pem 分离 2.4.6x 可用 SSLCertificateFile + SSLCertificateChainFile 2.4.8 起弃用,2.4.6x 报 AH02559
PFX / P12 不直接支持 需先转换 不推荐

PFX 必须先拆成 PEM,否则 Apache 启动直接报 AH02565: Certificate and private key ... do not match 一类的错:

# 1) 从 PFX 导出证书与私钥(-nodes 表示私钥不加密,便于 Apache 读取)
openssl pkcs12 -in cert.pfx -nodes -out all.pem

# 2) 拆分为私钥与证书链
openssl pkcs12 -in cert.pfx -nocerts -nodes -out server.key   # 私钥
openssl pkcs12 -in cert.pfx -clcerts -nokeys -out cert.pem    # 站点证书
openssl pkcs12 -in cert.pfx -cacerts -nokeys -out chain.pem   # 中间证书

# 3) 合并为 fullchain.pem(顺序:站点证书在前,中间证书在后)
cat cert.pem chain.pem > fullchain.pem

# 4) 校验私钥与证书是否配对(两个输出的公钥哈希必须一致)
openssl x509 -noout -pubkey -in fullchain.pem | openssl sha256
openssl pkey -pubout -in server.key | openssl sha256

TLS 档位照抄 Mozilla SSL Configuration Generator 即可:intermediate 档(TLS 1.2 + 1.3)兼容 Android 4.4+ 与 Java 8+,是公开站点的默认答案;modern 档只开 TLS 1.3,会把一批老客户端挡在门外,只适合内部 API;含 TLS 1.0 的 old 档仅用于存量设备,且与 PCI DSS 4.0 要求 TLS 1.2 及以上相冲突。

生效方式上,apachectl graceful(等价于 kill -USR1 <pid>)让子进程处理完当前请求再退出,不断开已有连接;systemctl restart apache2 会中断连接。生产多站点建议先备份 vhost,再平滑重载:

# 备份当前 vhost 配置(Debian/Ubuntu 路径;RHEL 系为 /etc/httpd/conf.d/)
sudo cp /etc/apache2/sites-available/example-ssl.conf{,.bak.$(date +%F)}

# 先做语法检查,避免 graceful 后整站不可用
sudo apachectl configtest      # 期望输出:Syntax OK

# 平滑重载(不断开现有连接)
sudo apachectl graceful

# 验证证书链、协议版本与到期时间
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates
curl -sI https://example.com | grep -i strict-transport-security

证书类型也要先想清楚:单域名证书只覆盖一个 FQDN;SAN 多域名证书可覆盖 example.com、www.example.com、api.example.com;通配符 *.example.com 不覆盖裸域 example.com,也不覆盖 a.b.example.com 这类多级子域。据 Let’s Encrypt 文档,通配符证书只能走 DNS-01 挑战,无法用 HTTP-01。上线前用 openssl s_client、curl -I、SSL Labs 扫描和 error_log 里的 AH0 错误码交叉验证,能把 SNI、证书链与协议配置问题一次定位。

apache https证书配置常见报错:AH01909、AH02559 与链缺失怎么排查

Apache 的 HTTPS 报错几乎都能归到三类根因:名字对不上(ServerName 与证书 SAN 不一致)、材料不配套(私钥/证书/中间链不匹配或缺失)、协议或 OCSP 配置不当。下表是速查入口,后面逐条给出定位命令。

报错关键字 根因 一条定位命令
AH01909 ServerName/ServerAlias 不在证书 SAN 内 openssl x509 -noout -ext subjectAltName -in fullchain.pem
AH02559 Apache 2.4.8+ 仍使用已弃用的 SSLCertificateChainFile apache2 -v / httpd -v
Unable to configure RSA server private key / key values mismatch 私钥与证书不配对 openssl x509 -noout -modulus … | openssl md5
sslv3 alert handshake failure 客户端与服务器无共同协议/套件 openssl s_client -tls1_2 -connect …
unable to get local issuer certificate 服务器未下发中间证书 openssl s_client -showcerts … | grep -c 'BEGIN CERT'
AH02217 ssl_stapling_init_cert OCSP 找不到 issuer 或缓存路径不可写 grep -r SSLStaplingCache /etc/apache2

AH01909 报“证书不含匹配服务器名的 ID”怎么办?完整报错是 AH01909: RSA certificate configured for example.com:443 does NOT include an ID which matches the server name。它不阻断启动,但会让浏览器对该 vhost 报名称不匹配。按 RFC 6125 与 RFC 5280,现代客户端只看 SAN、忽略 CN,所以先看 SAN 里到底有什么:

# 列出 SAN,确认是否覆盖该 vhost 的 ServerName 与所有 ServerAlias
openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -ext subjectAltName
# 期望输出示例:
# X509v3 Subject Alternative Name:
#     DNS:example.com, DNS:www.example.com

若 SAN 缺域名,改 vhost 的 ServerName/ServerAlias 使其落在 SAN 集合内,或用 certbot --expand -d example.com -d www.example.com -d api.example.com 重签。注意 SNI 场景下每个 <VirtualHost *:443> 必须显式写 ServerName,否则 Apache 用全局名去比对,必然误报。

AH02559 提示 SSLCertificateChainFile 已弃用,要删吗?取决于版本。Apache 2.4.8 起(见 Apache HTTP Server 2.4 变更日志)中间链由 SSLCertificateFile 指向的 fullchain 文件一并加载,该指令仅保留兼容性。RHEL/CentOS 7 自带的是 2.4.6,此时用 SSLCertificateChainFile 是正常做法、不会告警;一旦升到 2.4.8+ 仍保留就会每次 reload 刷告警。

apache2 -v   # Debian/Ubuntu;RHEL 系用 httpd -v
# 输出示例:Server version: Apache/2.4.6 (CentOS)

# 2.4.8+ 写法:删掉 SSLCertificateChainFile,链在 fullchain.pem 里(leaf → intermediate)
SSLCertificateFile      /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem
# SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem  ← 2.4.8+ 删除此行

# 改完先语法检查再平滑重载,避免中断在线连接
apachectl configtest && apachectl graceful

“Unable to configure RSA server private key”或“key values mismatch”怎么确认配对?这是私钥与证书不是同一对,常见于 certbot 续期后只替换了其中一个文件。用 modulus 摘要比对,两行输出必须逐字符相同:

openssl x509 -noout -modulus -in /etc/letsencrypt/live/example.com/fullchain.pem | openssl md5
openssl rsa  -noout -modulus -in /etc/letsencrypt/live/example.com/privkey.pem  | openssl md5
# 两行都是 (stdin)= 8f3c… 才算配对;不同则重新下发证书或回滚备份

客户端报 sslv3 alert handshake failure?这是对端(客户端)发回的 alert,含义是双方没有共同协议版本或密码套件,典型场景是老旧 Java 6/7、Windows XP 或嵌入式设备只支持 TLS 1.0。先用指定协议版本复现,再决定是否放开:

# 只测 TLS 1.2;若成功而默认握手失败,说明是版本协商问题
openssl s_client -tls1_2 -connect example.com:443 -servername example.com </dev/null
# 查看服务端实际启用的协议(Apache 2.4.6 默认 SSLProtocol all -SSLv3)
apachectl -D DUMP_RUN_CFG 2>/dev/null | grep -i sslprotocol

据 Mozilla SSL Configuration Generator 的 Intermediate 配置,公开站点维持 SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 即可;只有明确存在 TLS 1.0 客户端时才降级兼容,并记录为技术债。

浏览器报 unable to get local issuer certificate 是服务器的问题吗?是。该错误出现在客户端侧,说明服务器只发了叶子证书、没发中间证书。统计服务端实际下发的证书条数即可确认,正常至少 2 张(leaf + intermediate):

openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null 
  | grep -c 'BEGIN CERTIFICATE'
# 返回 1 → 缺中间证书;返回 2 或更多 → 链已下发
# 另可验证链完整性:输出中应出现 Verify return code: 0 (ok)

若 fullchain.pem 里顺序被写反成 intermediate → leaf,同样会失败。修复方式:重新从 CA 下载 fullchain,或 cat cert.pem chain.pem > fullchain.pem 保证 leaf 在前。

AH02217: ssl_stapling_init_cert: can’t retrieve issuer certificate! 怎么修?OCSP Stapling 需要在证书链里找到 issuer 才能取响应。检查三点:fullchain.pem 是否含中间证书(见上一条);SSLStaplingCache 的共享内存路径是否可写;SSLUseStapling 是否只写在 443 的 vhost 内(写在全局会让没有证书的 vhost 也尝试初始化)。

# 放在 :443 VirtualHost 内部,不要放全局
SSLUseStapling on
SSLStaplingCache "shmcb:/var/run/apache2/ssl_stapling(32768)"
# 目录需存在且属主为运行用户(www-data / apache)
mkdir -p /var/run/apache2 && chown www-data:www-data /var/run/apache2

配置后可用 openssl s_client -status -connect example.com:443 查看是否返回 OCSP Response Status: successful;首次启动缓存为空,需等一次客户端握手后才有内容。

apache https证书配置后怎么验证生效?6 个命令与判定标准

证书配完不等于生效。下面 6 个检查按”从本机到外部”的顺序执行,任何一步判定不通过就先修,不要继续往后走——否则后面的报错会被前一步的问题污染。

1. 语法与 vhost 拓扑:configtest 与 -S

# 语法检查:必须先拿到 Syntax OK 再 reload
sudo apachectl configtest          # 等价于 httpd -t,Apache 2.4 全系列通用
# 期望输出:Syntax OK

# 查看 vhost 拓扑:确认 443 站点的 ServerName、端口与 default vhost
sudo apachectl -S                  # 2.4.x 支持;老版本用 httpd -S

apachectl -S 会打印 port 443 namevhost example.com (/etc/httpd/conf.d/ssl.conf:56) 这样的行,并单独标注 default server。判定标准有两条:每个域名的 443 vhost 都出现且 ServerName 与证书 SAN 一致;默认 vhost 指向一个”占位”站点而不是业务主站。原因是 SNI 未被客户端发送时(老 Java 客户端、部分监控探针),握手会落到默认 vhost,若它持有主站证书,就会出现”访问 A 域名却返回 B 站点”的串站现象。

2. 证书链与有效期:s_client + x509

# -servername 显式发送 SNI;</dev/null 防止命令挂起等待输入
openssl s_client -connect example.com:443 -servername example.com -showcerts /dev/null 
  | openssl x509 -noout -subject -issuer -dates

同时必须在完整输出里看到 Verify return code: 0 (ok)——非 0 通常意味着中间证书缺失(unable to get local issuer certificate)或链顺序错误。有效期以 notAfter 为准,Let’s Encrypt 证书为 90 天,据 Let’s Encrypt 官方文档,续期应在剩余 30 天时完成,因此 notAfter 距今天数应始终大于 30。

3. 状态码与协议版本:curl 一行判定

curl -sS -o /dev/null -w '%{http_code} %{ssl_verify_result} %{http_version}n' https://example.com
# 期望:200 0 2   (2 = HTTP/2,需 mod_http2 已加载)

ssl_verify_result 为 0 表示本机信任链校验通过;若返回 60,说明 CA 库不认这张证书。HTTP/2 返回 1.1 时,检查 LoadModule http2_module 与 Protocols h2 http/1.1(Apache 2.4.17 起提供)。

4. HSTS 响应头

curl -sI https://example.com | grep -i strict-transport-security
# 测试期建议:max-age=300; includeSubDomains 先不加

据 MDN 与 RFC 6797,max-age 一旦下发便无法撤回,浏览器会在有效期内强制 HTTPS。上线初期用 300 秒便于回滚,稳定后再逐步升到 31536000(1 年)。includeSubDomains 只在所有子域都已启用 HTTPS 时添加,否则会把未配证书的子域一起打死。

5. OCSP Stapling 是否真的生效

openssl s_client -status -connect example.com:443 -servername example.com /dev/null 
  | grep -A3 "OCSP Response Status"
# 期望:OCSP Response Status: successful (0x0)

输出 no response sent 说明 Stapling 未工作。常见原因:SSLUseStapling on 已开但 SSLStaplingCache "shmcb:logs/ssl_stapling(32768)" 未配置,或服务器出站 80 端口被防火墙拦截导致无法访问 OCSP 响应方。Apache 2.4 中该功能默认关闭。

6. 日志与外部扫描

sudo tail -f /var/log/apache2/error.log | grep -E 'AH0|ssl'
# 重点告警:AH01909(证书 CN 与 ServerName 不匹配)、AH02217(未配置 SSLStaplingCache)

最后用 Qualys SSL Labs 扫描一次:评级 A 以上、证书链显示 “Chain issues: None”、TLS 1.2 与 1.3 均可握手,才算这次 apache https 证书配置真正闭环。若链不完整,回头补 SSLCertificateChainFile(2.4.8 之前)或把中间证书拼进 SSLCertificateFile(2.4.8 起推荐)。

检查项 命令 通过判定 失败典型症状
语法 apachectl configtest Syntax OK reload 失败、旧进程继续服务
vhost/SNI apachectl -S 443 域名齐全、默认站点为占位 串站、证书与域名不匹配
证书链 openssl s_client -showcerts Verify return code: 0 中间证书缺失
协议 curl -w '%{http_version}' 200 0 2 降级到 HTTP/1.1
HSTS curl -sI | grep -i hsts max-age 符合阶段 头缺失或 max-age 过大
Stapling s_client -status successful (0x0) no response sent

apache https证书配置的轮换、回滚与自动化(Let's Encrypt / acme.sh)

先回答一个运维群里高频出现的问题:certbot renew 日志明明显示续期成功,浏览器却仍报 NET::ERR_CERT_DATE_INVALID,为什么?因为 certbot 只把新证书写进 /etc/letsencrypt/live/<域名>/,Apache 进程仍持有启动时读入内存的旧证书;只要不 reload,这次续期就等于白做。这也是 apache https证书配置 里最容易被漏掉的一环。据 Let’s Encrypt 文档,其证书有效期固定 90 天,官方建议在到期前 30 天完成续期。

第 1 步:先演练再上线。certbot 2.x 自 2.0 起默认走 ACME v2 端点(RFC 8555),--dry-run 会请求 staging 环境,不消耗正式速率配额:

# 适用 certbot 2.x;RHEL/CentOS 需先启用 EPEL
sudo certbot renew --dry-run
# 成功输出关键行:Congratulations, all simulated renewals succeeded
# 失败常见报错:Problem binding to port 80: Could not bind to IPv4 or IPv6.
#   → standalone 插件与 Apache 抢 80 端口,改用 --webroot 或 --apache 插件

第 2 步:正式续期任务。用 --deploy-hook 而非 --post-hook,前者只在证书真正更新时触发,避免每天无谓 reload:

# /etc/cron.d/certbot-renew  —— 注意 cron 中 % 必须转义
# 02:17 + 最多 30 分钟随机延迟,避开 ACME 端点整点高峰
17 2 * * * root sleep $((RANDOM % 1800)) && certbot renew --quiet 
  --deploy-hook 'systemctl reload apache2'
# RHEL/CentOS 服务名是 httpd:systemctl reload httpd

为什么要加随机延迟?Let’s Encrypt 文档明确提示,大量客户端在同一时刻发起续期会造成 ACME 端点排队,并触发 too many failed authorizations recently 的限流。另外,certbot 2.x 安装包自带的 certbot.timer 默认每天运行两次且已内置随机延迟——若你用 systemd timer,就不要再叠加这条 cron,否则重复续期。

第 3 步:certbot 还是 acme.sh?两者都能完成 apache https证书配置 的证书侧工作,差别在安装形态与钩子名称:

维度 certbot 2.x acme.sh 3.x
安装方式 apt/yum 包管理器,随发行版更新 curl 单脚本安装到 ~/.acme.sh,无 root 依赖
默认续期触发 certbot.timer,每天 2 次带随机延迟 安装时写入 crontab,默认每日 1 次
reload 钩子 --deploy-hook --reloadcmd
DNS-01 / 通配符 需额外安装 certbot-dns-* 插件包 内置 100+ DNS API,单条命令签发通配符
适合场景 标准 LAMP/LEMP,系统包统一管理 无包管理的容器/内网、需 DNS API 的通配符证书
# acme.sh 安装证书并绑定 reload(一次性执行,之后由 cron 自动复用)
acme.sh --install-cert -d example.com 
  --key-file       /etc/ssl/private/example.key 
  --fullchain-file /etc/ssl/certs/example.fullchain.pem 
  --reloadcmd      'systemctl reload apache2'
# 注意:reloadcmd 会被持久化,后续每次续期自动执行,改路径后需重跑本命令

第 4 步:盯住 30 天窗口。不要只靠”续期任务没报错”,用 OpenSSL 主动判断:

# 剩余有效期 < 2592000 秒(30 天)时返回码为 1,否则为 0
openssl x509 -checkend 2592000 -noout -in /etc/ssl/certs/example.fullchain.pem 
  || echo "证书 30 天内过期,检查续期链路"
# 顺带确认链是否完整(应输出 2 段以上 BEGIN CERTIFICATE)
openssl crl2pkcs7 -nocrl -certfile example.fullchain.pem | openssl pkcs7 -print_certs -noout

第 5 步:留好回滚路径。变更前备份 vhost 与 /etc/letsencrypt/archive(live/ 是符号链接,只备份它无效);新证书异常时把 vhost 里的 SSLCertificateFile 指回旧 fullchain,再执行 apachectl configtest && apachectl graceful。旧证书至少保留 7–14 天,覆盖客户端 OCSP 缓存与回滚窗口。若 reload 后日志出现 AH02032: Hostname ... provided via SNI and hostname ... provided via HTTP have no compatible SSL setup,说明 SNI 与 vhost 的证书不匹配,正是回滚信号。

第 6 步:把证书做成可观测指标。用 Prometheus blackbox_exporter 的 https 模块对 443 每日探测,关注 probe_ssl_earliest_cert_expiry(换算成剩余天数 < 21 天告警)与 probe_tls_version_info;Zabbix 侧可用 web.page.get 配合 openssl s_client -connect example.com:443 -servername example.com 取到期时间。同时在 error_log 上按 AH 错误码聚合告警(AH01909 证书与 ServerName 不符、AH02032 SNI 冲突),TLS 参数基线可参照 Mozilla SSL Configuration Generator 的 intermediate 配置。

OpenSSL 版本不够,TLS 1.3 是不是配了也白配?是。Apache 的 TLS 1.3 完全由底层 OpenSSL 提供,OpenSSL 低于 1.1.1(例如 CentOS 7 自带的 1.0.2k)时,哪怕写 SSLProtocol all 也协商不出 TLS 1.3。上线前先核对版本,Apache 2.4.62 官方推荐搭配 OpenSSL 3.0.x:

# 适用 Apache 2.4.62 + OpenSSL 3.0.x;两者版本必须同时满足
apachectl -v            # 期望输出:Server version: Apache/2.4.62 (Debian)
openssl version         # 期望输出:OpenSSL 3.0.13 ...;<1.1.1 则无 TLS1.3
httpd -M | grep -E 'ssl|http2|headers'   # 确认 ssl_module / http2_module / headers_module 已加载

一个可直接落地的 vhost 片段如下,注意 SSLProtocol 在 Apache 2.4 中的默认值是 all,多数发行版已在 ssl.conf 里收紧过,vhost 内重写会覆盖全局值:

<VirtualHost *:443>
    ServerName example.com
    SSLEngine on
    # 关键:必须是 fullchain.pem,缺中间证书会导致链不可验证(RFC 8446 对 TLS1.3 同样要求)
    SSLCertificateFile      /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem

    # 保留 TLS1.2/1.3,禁用 SSLv3/TLS1.0/TLS1.1
    SSLProtocol             all -SSLv3 -TLSv1 -TLSv1.1
    # 只约束 TLS1.2 及以下;TLS1.3 套件由 OpenSSL 默认策略管理,此指令管不到
    SSLCipherSuite          ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384

    # HTTP/2:需 mod_http2;不支持的客户端自动回退 http/1.1,不影响证书校验
    Protocols               h2 http/1.1
    # HSTS:先短 max-age 灰度,确认全部子域有 HTTPS 后再加 includeSubDomains/preload
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

    SSLUseStapling          on
    SSLStaplingCache        "shmcb:logs/ssl_stapling(32768)"
</VirtualHost>

上述套件串取自 Mozilla SSL Configuration Generator 的 intermediate 档。四项加固的生效前提与回退风险差别很大,建议按下表逐项确认后再开:

加固项 关键指令/默认值 生效前提 回退与风险
TLS 1.3 SSLProtocol,默认 all OpenSSL ≥ 1.1.1 老客户端降级到 TLS 1.2 继续握手
HTTP/2 Protocols h2 http/1.1,默认 http/1.1 mod_http2 已加载,且 MPM 非 prefork 客户端自动回退 http/1.1
HSTS max-age=31536000; includeSubDomains 全部子域已启用 HTTPS 浏览器强制升级,子域 HTTP 直接失败
OCSP Stapling SSLUseStapling,默认 off 证书含 OCSP URL 据 Let’s Encrypt 2024 年公告,OCSP 将逐步退出,不宜作为唯一可用性指标

HTTP/2 打开后 Apache 起不来?多半是 MPM 不匹配,会直接报 AH10034: The mpm module (prefork.c) is not supported by mod_http2,改用 event 或 worker 即可。HSTS 预加载的取舍在于:max-age 至少 31536000 且带 includeSubDomains 才可能进入浏览器 preload 列表,而提交后撤销周期以月计,务必先确认没有只跑 HTTP 的子域。

证书链不完整时,Stapling 会静默失败并刷出 AH02217: ssl_stapling_init_cert: can't retrieve issuer certificate!。改完配置按下面的顺序验证与发布,graceful 只重读配置、不切断已建立连接:

# 1) 协议与套件实际协商结果
openssl s_client -connect example.com:443 -servername example.com -tls1_3 </dev/null | grep -E 'Protocol|Cipher'
# 2) 证书链是否完整(期望 ≥2 张证书)
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | grep -c 'BEGIN CERTIFICATE'
# 3) Stapling 是否生效
openssl s_client -connect example.com:443 -servername example.com -status </dev/null | grep -A2 'OCSP response'
# 4) 语法检查通过后平滑 reload
apachectl configtest && apachectl graceful

需要。Apache 2.4.8+ 推荐 SSLCertificateFile 直接指向 fullchain.pem(站点证书+中间证书),cert.pem 只有站点证书,缺中间链会导致部分客户端报 unable to get local issuer certificate;SSLCertificateChainFile 已弃用。

每个域名一个 ,各自配置 SSLCertificateFile 与 SSLCertificateKeyFile,依赖 SNI;Apache 2.4.6x + OpenSSL 1.1.1/3.x 支持。用 apachectl -S 检查默认 vhost,无 SNI 的老客户端只会拿到第一个证书。

不需要。apachectl configtest 通过后执行 apachectl graceful 或 systemctl reload apache2,平滑加载新证书,不断开现有连接;restart 会中断连接,多站点生产环境优先 graceful。

Nginx 用 ssl_certificate 指向 fullchain、ssl_certificate_key 指向私钥;Apache 用 SSLCertificateFile/SSLCertificateKeyFile,并在 2.4.8+ 把链并入 SSLCertificateFile。Nginx reload 是 nginx -s reload,Apache 是 apachectl graceful,两者都要先做语法检查。

检查 ServerName 是否在证书 SAN 中;用 openssl x509 -in fullchain.pem -noout -ext subjectAltName 查看。不一致就重签证书,或改 vhost 的 ServerName/ServerAlias,改完 apachectl configtest && apachectl graceful。

多半是中间链缺失或证书过期。用 openssl s_client -showcerts 看服务器发送了几张证书,正常应至少 2 张;再用 openssl x509 -noout -dates 检查有效期,并确认 fullchain.pem 顺序为 leaf → intermediate。

certbot renew –deploy-hook 'systemctl reload apache2',cron 每天跑一次;先用 certbot renew –dry-run 验证。acme.sh 则用 –reloadcmd 'systemctl reload apache2',并在续期后检查 apachectl configtest。


本文由 sslgood.com 安全团队基于生产环境实战经验撰写,关键结论均经过技术复核。

内容审核

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