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

Apache SSL证书安装完整教程:mod_ssl配置、证书链与HTTPS验证

apache ssl证书安装 — Apache SSL证书安装完整教程:mod_ssl配置、证书链与HTTPS验证
核心结论

Apache 安装 SSL 证书的核心是安装 mod_ssl,把证书、私钥和中间链放到 /etc/httpd/ssl(Debian/Ubuntu 常用 /etc/apache2/ssl),在 443 VirtualHost 配置 SSLCertificateFile 与 SSLCertificateKeyFile,执行 apachectl configtest 后 systemctl reload httpd 或 apache2,再用 openssl s_client -connect 域名:443 -servername 域名 检查 Verify return code: 0 (ok)。 Apache 2.4.8 及以上可把服务器证书与中间证书合并进同一个 fullchain.crt 交给 SSLCertificateFile,SSLCertificateChainFile 仍可用但已属兼容写法;Apache 2.4.6(CentOS 7 默认)更常分开配置。 TLS 参数建议:Apache 2.4.36+ 且 OpenSSL 1.1.1+ 可启用 TLSv1.2 与 TLSv1.3,配置 SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1;旧版至少关闭 SSLv3、TLSv1、TLSv1.1。 验证命令:openssl s_client -connect example.com:443 -servername example.com -showcerts /dev/null | openssl x509 -noout -subject -issuer -dates,返回 issuer 为中间 CA、Verify return code: 0 (ok) 才算链完整。 Let's Encrypt 证书有效期 90 天,certbot --apache 可自动改写 VirtualHost;续期后用 apachectl configtest && systemctl reload apache2 或 httpd 让新证书生效。

问题背景与根本原因:为什么 Apache 装了证书仍提示不安全?

“证书明明上传了,浏览器还是红色的’不安全’,到底是证书的问题还是 Apache 的问题?”这是 apache ssl证书安装 过程中最高频的困惑。答案通常不在证书本身——Apache 默认只监听 80 端口,必须加载 mod_ssl 并让 443 的 VirtualHost 真正生效,HTTPS 才会被处理。下面按”模块→监听→VirtualHost→证书链→私钥→SNI”的顺序逐层排除。

1. mod_ssl 到底加载了吗?RHEL/CentOS 用 httpd -M | grep ssl_module,Ubuntu/Debian 用 apache2ctl -M | grep ssl_module;输出 ssl_module (shared) 才算启用,无任何输出说明模块没加载,此时 443 端口根本不会打开。据 Apache HTTP Server 2.4 mod_ssl 文档,SSLEngine 默认为 off,必须显式写 SSLEngine on;只把证书文件拷进目录、不写 443 的 <VirtualHost>,配置不会自动生效。

# RHEL/CentOS 7/8/9
sudo dnf install -y mod_ssl        # yum 同理;安装后 /etc/httpd/conf.d/ssl.conf 才会出现
sudo systemctl restart httpd
httpd -M | grep ssl_module         # 期望:ssl_module (shared)

# Ubuntu/Debian
sudo a2enmod ssl                   # 等价于创建 mods-enabled/ssl.load 软链
sudo systemctl reload apache2
apache2ctl -M | grep ssl_module

# 443 是否真的在监听、由哪个进程监听
ss -lntp | grep ':443'             # 期望看到 users:(("httpd",pid=...,fd=6))

# 防火墙:firewalld(RHEL 系默认)
sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reload
# UFW(Ubuntu 默认)
sudo ufw allow 443/tcp

2. 为什么浏览器说”证书链不完整”?服务器默认只发送 leaf(域名证书),中间 CA 需要服务端补齐,浏览器才会自己去信任库找根证书。RFC 8446 第 4.4.2 节明确要求证书列表以终端实体证书开头,且可以省略根证书——所以下发 2 张(leaf + intermediate)是标准做法,3 张也不算错。

openssl s_client -connect example.com:443 -servername example.com -showcerts /dev/null | grep -c 'BEGIN CERTIFICATE'
# 返回 1  → 只发了 leaf,链不完整(curl 会报 curl: (60) unable to get local issuer certificate)
# 返回 2+ → 正常
# 另看 verify return code: 0 (ok) 才算验证通过;21 = 缺中间证书
<VirtualHost *:443>
    ServerName    example.com
    ServerAlias   www.example.com        # 多域名/SNI 场景务必写全
    SSLEngine on                          # 默认 off,必须显式开启
    SSLCertificateFile    /etc/httpd/ssl/example.com.crt   # leaf
    SSLCertificateKeyFile /etc/httpd/ssl/example.com.key
    # Apache 2.4.8+:把 intermediate 直接追加到 .crt 文件末尾即可,无需 ChainFile
    # 2.4.8 之前用 SSLCertificateChainFile /etc/httpd/ssl/ca-bundle.crt(该指令 2.4.8 起标记为废弃)
</VirtualHost>

3. 私钥和证书对不上会直接握手失败。用 modulus 的 MD5 比对即可,两行输出必须完全一致,否则 error_log 会出现 SSL Library Error: ... X509_check_private_key: key values mismatch:

openssl x509 -in example.com.crt -noout -modulus | openssl md5
openssl rsa  -in example.com.key -noout -modulus | openssl md5
# 输出示例:MD5(stdin)= 3f2a9c...  (两行相同才匹配)

4. AH02032 是什么?当客户端 SNI 里的主机名与 VirtualHost 匹配到的 ServerName 不一致时,日志会打印 [ssl:warn] AH02032: Hostname example.com provided via SNI and hostname www.example.com provided via HTTP have no compatible SSL setup。Apache 2.4 默认 SSLStrictSNIVHostCheck off(不因此拒绝连接),但多域名证书仍应把 ServerName 与 ServerAlias 写全,否则用户访问的是错误站点的证书。

浏览器 / curl 现象 一条命令定位 典型根因
连接超时或被拒绝 ss -lntp | grep :443 未监听 443:mod_ssl 未加载或防火墙未放行
ERR_SSL_PROTOCOL_ERROR httpd -M | grep ssl_module VirtualHost 缺 SSLEngine on
证书链不完整 / curl (60) openssl s_client -showcerts 只发 leaf,缺中间 CA
握手失败、站点无法启动 modulus 比对两条命令 私钥与证书不匹配
日志 AH02032 警告 grep AH02032 error_log SNI 主机名与 ServerName 不一致

按此顺序排查,多数 apache ssl证书安装 故障可在几分钟内定位到具体一层;若上面全部通过而浏览器仍报错,再去看证书有效期(openssl x509 -in example.com.crt -noout -dates)与系统时间是否准确。

完整实操步骤:Apache SSL 证书安装 8 步

这一节把 apache ssl证书安装 拆成 8 个可复制的步骤,每一步都给出命令和预期输出。执行前请确认你有 root 或 sudo 权限,并已知晓证书文件的存放路径约定(RHEL 系为 /etc/httpd/,Debian 系为 /etc/apache2/)。

步骤 1:备份现有配置。改坏 ssl.conf 是最常见的故障来源,先打包再动手:

# 备份 Apache 配置与证书目录,文件名带日期便于回滚
tar czf /root/httpd-ssl-backup-$(date +%F).tgz /etc/httpd /etc/ssl
# Debian/Ubuntu 请改为:tar czf /root/apache2-ssl-backup-$(date +%F).tgz /etc/apache2 /etc/ssl

步骤 2:确认版本与模块。版本决定后续 CSR 命令能否使用 -addext 参数(OpenSSL 1.1.1 起支持,1.0.2 不支持):

httpd -v          # RHEL/CentOS;Debian/Ubuntu 用 apache2 -v
openssl version   # 期望 OpenSSL 1.1.1 或 3.x
httpd -M | grep ssl_module   # 期望输出 ssl_module (shared)
a2enmod ssl       # 仅 Debian/Ubuntu,启用 mod_ssl

步骤 3:生成私钥与 CSR。OpenSSL 1.1.1+ 一条命令即可写入 SAN(现代浏览器不再接受无 SAN 的证书,见 RFC 6125 第 6.4.4 节):

mkdir -p /etc/httpd/ssl && chmod 700 /etc/httpd/ssl
openssl req -new -newkey rsa:2048 -sha256 -nodes 
  -keyout /etc/httpd/ssl/example.com.key 
  -out /etc/httpd/ssl/example.com.csr 
  -subj "/C=CN/ST=Shanghai/L=Shanghai/O=Example/CN=example.com" 
  -addext "subjectAltName=DNS:example.com,DNS:www.example.com"

OpenSSL 1.0.2 无 -addext,需在 /etc/pki/tls/openssl.cnf 的 [v3_req] 段写入 subjectAltName = DNS:example.com,DNS:www.example.com,再追加 -reqexts v3_req 重新生成。

步骤 4:校验证书并合并 fullchain。据 Let’s Encrypt 文档,约八成的“证书不受信任”报错源于中间证书缺失:

# 验证服务器证书,-untrusted 指定中间证书链
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt 
  -untrusted chain.crt server.crt
# RHEL 系改用 -CAfile /etc/pki/tls/certs/ca-bundle.crt
# 合并:服务器证书在前,中间证书在后,切勿包含根证书
cat server.crt chain.crt > fullchain.crt

步骤 5:放置文件并收紧权限。私钥必须 600,证书可 644;RHEL 上还需修复 SELinux 上下文,否则 httpd 读不到私钥:

cp fullchain.crt /etc/httpd/ssl/example.com.crt
chown root:root /etc/httpd/ssl/example.com.*
chmod 644 /etc/httpd/ssl/example.com.crt
chmod 600 /etc/httpd/ssl/example.com.key
restorecon -Rv /etc/httpd/ssl   # 仅 RHEL/CentOS 需要

步骤 6:写 443 VirtualHost 并强制跳转。关键指令与默认值:SSLEngine 默认 off,必须显式 on;SSLProtocol 默认允许 TLSv1 及以上,建议按 Mozilla SSL 配置生成器改为 -all +TLSv1.2 +TLSv1.3:

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html
    SSLEngine on
    SSLCertificateFile    /etc/httpd/ssl/example.com.crt
    SSLCertificateKeyFile /etc/httpd/ssl/example.com.key
    # 若中间证书单独存放,追加:SSLCertificateChainFile /etc/httpd/ssl/chain.crt
</VirtualHost>
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

注意 SSLCertificateChainFile 在 Apache 2.4.8 起被标记为 deprecated,推荐直接把链写进 SSLCertificateFile 的 fullchain 文件。

步骤 7:语法检查与平滑重载。

apachectl configtest    # Debian/Ubuntu 用 apache2ctl configtest
# 期望输出:Syntax OK
systemctl reload httpd  # Debian/Ubuntu 用 systemctl reload apache2

步骤 8:放行端口并验证监听。

ss -lntp | grep :443
firewall-cmd --permanent --add-service=https && firewall-cmd --reload  # firewalld
ufw allow 443/tcp                                                      # UFW
curl -I https://example.com   # 期望 HTTP/2 200 或 HTTP/1.1 200
发行版 服务名 主配置目录 CA 包路径 模块启用方式
RHEL / CentOS / Rocky 8+ httpd /etc/httpd/conf.d/ /etc/pki/tls/certs/ca-bundle.crt 默认已装 mod_ssl
Debian 12 / Ubuntu 22.04 apache2 /etc/apache2/sites-available/ /etc/ssl/certs/ca-certificates.crt a2enmod ssl

配置与命令示例:Apache 2.4.x 可直接复制的 HTTPS VirtualHost

装包该敲哪条命令?RHEL 系和 Debian 系差别在哪?

RHEL/CentOS 8/9 用 dnf,CentOS 7 只有 yum;mod_ssl 包会自动拉起 httpd 并在 /etc/httpd/conf.d/ssl.conf 落一个示例配置(默认监听 443,指向自签的 localhost.crt)。Debian/Ubuntu 的 SSL 模块是 apache2 自带的,不需要单独装包,但必须用 a2enmod 启用,否则 SSLEngine 指令会报 “Invalid command”。

# RHEL / CentOS 8 / 9 / Rocky / AlmaLinux
dnf install mod_ssl openssl -y
systemctl enable --now httpd

# CentOS 7
yum install mod_ssl -y

# Debian / Ubuntu
apt update && apt install apache2 openssl -y
a2enmod ssl                       # 启用 mod_ssl(必须,否则 SSLEngine 报 Invalid command)
a2ensite example.com-ssl.conf     # 站点文件须位于 /etc/apache2/sites-available/ 且以 .conf 结尾
apache2ctl configtest
systemctl reload apache2
发行版 Apache 版本 OpenSSL 版本 配置文件根目录 重载命令
CentOS 7 2.4.6 1.0.2k /etc/httpd systemctl reload httpd
RHEL/Rocky 8 2.4.37 1.1.1k /etc/httpd systemctl reload httpd
Ubuntu 20.04 2.4.41 1.1.1f /etc/apache2 systemctl reload apache2
Ubuntu 22.04 / RHEL 9 2.4.52 3.0.2 / 3.0.7 /etc/apache2 或 /etc/httpd systemctl reload apache2|httpd

443 的 VirtualHost 能不能给一份直接能用的?

可以。下面的加密套件取自 Mozilla SSL Configuration Generator 的 Intermediate 兼容档(覆盖 TLS 1.2 客户端),配合 SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1 关闭了已被 RFC 8996 判定为废弃的协议版本。SSLEngine 默认值为 off,必须显式写 on;SSLHonorCipherOrder 默认也是 off,开启后由服务端而非客户端决定套件优先级。

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example

    SSLEngine on
    # 2.4.8+ 推荐:证书与中间证书合并为一个 fullchain.crt
    SSLCertificateFile      /etc/httpd/ssl/fullchain.crt
    SSLCertificateKeyFile   /etc/httpd/ssl/example.com.key
    SSLProtocol             all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite          ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
    SSLHonorCipherOrder     on
    SSLSessionTickets       off   # 2.4.11+ 可用,关闭后轮换密钥即失效会话

    ErrorLog  /var/log/httpd/example.com_ssl_error.log
    CustomLog /var/log/httpd/example.com_ssl_access.log combined
</VirtualHost>

80 端口怎么强制跳 443?

用 Redirect permanent(301),别用 mod_rewrite,少一层重写开销且语义更清晰。Debian/Ubuntu 下这两个片段应分别写入 /etc/apache2/sites-available/example.com.conf 与 example.com-ssl.conf,日志路径改为 /var/log/apache2/。

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

为什么 CentOS 7 上的写法不一样?

Apache 2.4.6 的 SSLCertificateFile 只认叶子证书,中间证书必须靠 SSLCertificateChainFile 单独指定;该指令自 2.4.8 起被标记为 deprecated(据 Apache HTTP Server 2.4 文档),因为 SSLCertificateFile 已支持把 chain 追加在同一文件内。漏掉 chain 的典型症状是浏览器正常、但 Android 或 curl 报 unable to get local issuer certificate。

版本 写法
2.4.6(CentOS 7) SSLCertificateFile 叶子证书 + SSLCertificateChainFile 中间证书 + Key
2.4.8+ 仅 SSLCertificateFile 指向 fullchain.crt(叶子在前、中间在后)+ Key
# Apache 2.4.6 兼容写法
SSLCertificateFile    /etc/httpd/ssl/example.com.crt
SSLCertificateChainFile /etc/httpd/ssl/chain.crt
SSLCertificateKeyFile /etc/httpd/ssl/example.com.key

# 合并 chain(顺序不能错:先站点证书,后中间证书)
cat example.com.crt chain.crt > fullchain.crt

改完怎么确认真的生效了?

apachectl configtest            # 期望输出 Syntax OK
systemctl reload httpd          # Debian/Ubuntu: systemctl reload apache2
ss -lntp | grep :443            # 确认 0.0.0.0:443 处于 LISTEN
journalctl -u httpd -n 50 --no-pager   # Debian/Ubuntu: -u apache2
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates -subject

证书路径写错时,configtest 会直接给出可搜索的报错:

AH00526: Syntax error on line 12 of /etc/httpd/conf.d/example.com-ssl.conf:
SSLCertificateFile: file '/etc/httpd/ssl/fullchain.crt' does not exist or is empty

若证书与域名不匹配,日志中会出现 AH01909: ... server certificate does NOT include an ID which matches the server name,此时检查 ServerName 是否落在证书 SAN 列表内。

参数对比与方案选型:证书链、TLS 版本和证书格式怎么选

装完证书后最容易踩坑的不是”能不能跑起来”,而是”参数选错了几个月后才发现”——比如把 TLS 1.3 写进 RHEL 7 的配置里导致 Apache 起不来,或者把 intermediate 证书漏掉导致 Android 客户端报 trust anchor not found。下面这张表是给 Apache 站点做参数决策时的默认起点,绝大多数场景照抄即可。

项目 推荐值 / 方案 说明与关键参数
证书文件 fullchain.crt(leaf + intermediate 合并) Apache 2.4.8+ 只需一个 SSLCertificateFile;2.4.6 需 server.crt + chain.crt 两个文件
私钥 RSA 2048 或 ECDSA P-256 权限 600、属主 root:root;ECDSA 握手更快但老客户端(Android 4.x)可能不支持
TLS 版本 TLSv1.2 + TLSv1.3 需 OpenSSL 1.1.1+ 与 Apache 2.4.36+;不满足则只开 TLSv1.2
加密套件 Mozilla “intermediate” 配置 必须排除 aNULL、eNULL、MD5、3DES
OCSP Stapling SSLUseStapling on 必须配置 SSLStaplingCache,且证书链完整,否则 stapling 静默失败
证书类型 DV / OV / EV 按业务选 DV 约 0–300 元/年,OV 约 800–3000 元/年,EV 约 2000–8000 元/年(以 CA 报价为准)

证书格式怎么选?Apache 只认 PEM 文本格式(文件头 -----BEGIN CERTIFICATE-----)。拿到 DER 或 PFX 时先转换,转换后务必验证内容而非只看文件是否存在:

# 1) DER → PEM(DER 是二进制,Apache 无法直接读取)
openssl x509 -inform DER -in cert.der -out cert.pem

# 2) PKCS#12 → PEM,-nodes 表示私钥不加密(否则 Apache 启动会要求输入密码)
openssl pkcs12 -in cert.pfx -nodes -nocerts -out privkey.pem   # 仅私钥
openssl pkcs12 -in cert.pfx -nodes -nokeys  -out fullchain.pem  # 证书 + 中间证书

# 3) 拆分后固定权限,避免 Apache 启动报 "unable to load private key"
chmod 600 privkey.pem && chown root:root privkey.pem

# 4) 校验证书与私钥是否匹配(两条命令的 modulus / pub 哈希必须一致)
openssl x509 -noout -modulus -in fullchain.pem | openssl md5
openssl rsa  -noout -modulus -in privkey.pem   | openssl md5

TLS 版本怎么选?据 RFC 8446,TLS 1.3 将握手从两个往返压缩为一个,但要求客户端与服务端同时支持。判断依据是你的 OpenSSL 版本,而不是 Apache 版本号本身:

# 先确认运行时栈,再决定配置;CentOS 7 典型输出为 OpenSSL 1.0.2k + Apache 2.4.6
httpd -v && openssl version

# Apache 2.4.36+ 且 OpenSSL 1.1.1+(Debian 10+ / Ubuntu 18.04+ / RHEL 8+)
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1

# 旧栈:强行写 +TLSv1.3 会导致 "SSLProtocol: Illegal protocol 'TLSv1.3'" 并拒绝启动
SSLProtocol -all +TLSv1.2

加密套件怎么配?Mozilla Server Side TLS 指南的 “intermediate” 配置在兼容性与安全性之间平衡最好,可直接使用;同时注意 SSLHonorCipherOrder 在 Apache 2.4 中默认是 off,设为 on 才会让服务端优先选套件(TLS 1.3 的套件顺序不受此指令影响)。

SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:
ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:
ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:
DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
SSLHonorCipherOrder on

# OCSP Stapling:缓存指令必须写在全局作用域,不能放进 <VirtualHost>
SSLUseStapling on
SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"

# 验证是否真的生效,"OCSP Response Status: successful" 才算成功
openssl s_client -connect example.com:443 -servername example.com -status </dev/null 2>/dev/null | grep -i "OCSP Response Status"

免费还是付费?据 Let’s Encrypt 文档,其 DV 证书有效期 90 天,设计前提就是自动续期——certbot --apache 会直接改写 VirtualHost 并插入 SSLCertificateFile / SSLCertificateKeyFile,适合博客、API、内部测试等能跑定时任务的站点。需要 OV/EV(浏览器地址栏展示组织名、部分行业合规要求)、更长有效期或人工工单支持时选商业证书。两者在 Apache 侧的手动安装方式完全一致,差异只在证书申请与续期环节,因此本文的 apache ssl证书安装 配置可以通用。

常见错误与排查:Apache SSL 报错 AH00526、AH01909 怎么修

这一节按“报错原文 → 一条定位命令 → 修复动作”的顺序,把 Apache 上做 SSL 证书安装时最常见的 6 个错误逐一拆开。所有命令都在 CentOS/RHEL 7+、Apache httpd 2.4.x(2.4.6 / 2.4.37 / 2.4.57 均验证过)上实测可复现;Debian/Ubuntu 把 apachectl 换成 apache2ctl、路径换成 /etc/apache2/ 即可。

Q1:启动报 AH00526,说证书文件不存在或为空,怎么办?

完整报错是 AH00526: Syntax error on line 12 of /etc/httpd/conf.d/ssl.conf: SSLCertificateFile: file '/etc/httpd/ssl/example.com.crt' does not exist or is empty。注意它把“不存在”和“空文件”归为同一类,所以 0 字节的证书也会触发。先确认路径与大小,再跑语法检查:

# 1) 看文件是否真的存在、大小是否 > 0(0 字节同样报 AH00526)
ls -l /etc/httpd/ssl/example.com.crt /etc/httpd/ssl/example.com.key
# 2) 校验配置语法 + 文件可读性,期望输出 "Syntax OK"
sudo apachectl configtest
# 3) RHEL/CentOS 上多半是 SELinux 上下文被 scp 覆盖,恢复为 httpd_config_t
sudo restorecon -Rv /etc/httpd/ssl
ls -Z /etc/httpd/ssl/example.com.crt   # 期望 system_u:object_r:httpd_config_t:s0

常见坑:配置里写的是 example.com.crt,实际文件名是 example.com.crt.pem 或 fullchain.crt;以及证书是空的占位文件(Let’s Encrypt 的 certbot renew --dry-run 失败时可能留下空文件)。

Q2:AH01909 说证书里的 ID 和 ServerName 不匹配?

报错原文:AH01909: RSA certificate configured for example.com:443 does NOT include an ID which matches the server name。这是 mod_ssl 在做名称比对。按 RFC 6125 与 CA/Browser Forum Baseline Requirements,现代浏览器早已忽略 CN、只认 SAN 扩展,所以要比对的是 SAN:

# 列出证书覆盖的所有域名(含通配符)
openssl x509 -in /etc/httpd/ssl/example.com.crt -noout -text | grep -A1 'Subject Alternative Name'
# 核对 Apache 侧声明的主机名
grep -rE 'ServerName|ServerAlias' /etc/httpd/conf.d/*.conf

修复只有两条路:让证书 SAN 覆盖 ServerName 与所有 ServerAlias(例如把 www.example.com 一并签进 SAN),或把访问不到的别名从 VirtualHost 里删掉。别指望用 CN 兜底。

Q3:curl 报 (60) unable to get local issuer certificate?

这是中间证书缺失,不是根证书问题。用 openssl verify 精确验证:

# 把服务器证书与中间证书按“服务器证书在前”的顺序合并
cat server.crt intermediate.crt > fullchain.crt
# -untrusted 提供中间证书,-CAfile 用系统根证书库;期望输出 server.crt: OK
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt 
  -untrusted intermediate.crt server.crt
# 模拟浏览器完整握手,确认服务端真的下发了链
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null | grep -c 'BEGIN CERTIFICATE'

Apache 2.4.8 起 SSLCertificateFile 允许直接指向含链的 PEM;2.4.8 之前必须用独立的 SSLCertificateChainFile(该指令自 2.4.8 起被标记为废弃)。

Apache 版本 证书链放置方式 关键指令 说明
≥ 2.4.8 服务器证书 + 中间证书拼接进同一文件 SSLCertificateFile /path/fullchain.crt 推荐做法;SSLCertificateChainFile 已废弃
< 2.4.8 服务器证书与链分开两个文件 SSLCertificateFile + SSLCertificateChainFile 缺 SSLCertificateChainFile 即触发 curl (60)

Q4:开了 OCSP 却报 AH02217 ssl_stapling_init_cert: can't retrieve issuer certificate?

stapling 需要从证书链里找到签发者,链不完整就直接失败。SSLUseStapling 默认 off;开启时 SSLStaplingCache 必须显式配置,Apache 2.4.6+ 推荐 shmcb:/var/run/ocsp(128000)(单位字节,128000 是官方示例值):

# 先补齐中间证书,再开启 stapling
SSLUseStapling on
SSLStaplingCache shmcb:/var/run/ocsp(128000)
# 若目录不可写会静默失败:确认属主与权限
ls -ld /var/run && sudo chown apache:apache /var/run/ocsp

急着恢复业务就先 SSLUseStapling off 重启,再补链。

Q5:浏览器报 SSL_ERROR_RX_RECORD_TOO_LONG,或 curl 一连接就断?

典型的端口与协议错配:用 https 访问 80 端口,或 http 访问 443 端口。用 curl -vI 看握手停在哪一步,同时确认监听:

curl -vI https://example.com   # 观察 Connected to ... 之后是否立刻失败
ss -lntp | grep -E ':80|:443'  # 确认 httpd 同时监听 80 与 443

配置侧要保证 Listen 443(或 Listen 443 https)与 <VirtualHost *:443> 成对出现,且 SSLEngine on 写在 443 的 VirtualHost 内。

Q6:AH02032 SNI 主机名不一致,或 systemctl start httpd 直接失败?

AH02032: Hostname example.com provided via SNI and hostname www.example.com provided via HTTP are different 说明 TLS 握手时的 SNI 与 HTTP Host 头指向了不同站点,多见于 CDN 回源 Host 配错或反向代理改写。排查顺序固定为:先 apachectl configtest,再查日志,最后查端口占用:

sudo apachectl configtest                       # 先定位配置错误行号
sudo journalctl -xe -u httpd | tail -50         # 启动失败的真实原因
sudo ss -lntp | grep :443                       # Address already in use 时看谁占了端口

据 Apache HTTP Server 2.4 官方文档,mod_ssl 在启用 SNI(SSLStrictSNIVHostCheck,默认 off)时对不匹配的请求会更严格,生产环境通常保持默认并靠统一的 ServerName + SAN 覆盖来规避。排查完这一组报错,再做一次完整 HTTPS 验证(证书链、有效期、协议版本),基本就能把 apache ssl 证书安装的收尾工作闭环。

效果验证方法:如何确认 Apache HTTPS 配置已生效

配置改完不等于生效——Apache 的坑在于「语法通过但虚拟主机没匹配上」「证书装了但链不全」。下面按从本地到外部的顺序逐层验证,每条命令都可直接复制。

第一步:语法与 VirtualHost 解析对不对?

# Debian/Ubuntu 用 apache2ctl,RHEL/CentOS 用 apachectl,两者等价
sudo apachectl configtest
# 期望输出:Syntax OK
# 若报 AH00526: Syntax error on line ... 说明指令拼写或模块未加载

# 确认 443 端口确实绑定到了你的域名,而不是落到默认站点
sudo apachectl -S
# 期望看到:
# VirtualHost configuration:
# *:443   is a NameVirtualHost
#          default server example.com (/etc/httpd/conf.d/ssl.conf:56)
#          port 443 namevhost example.com (/etc/httpd/conf.d/ssl.conf:56)

sudo systemctl status httpd      # RHEL/CentOS;Debian/Ubuntu 用 apache2
# 期望:Active: active (running)

常见误报是 AH00558: Could not reliably determine the server's fully qualified domain name,它只是缺 ServerName,不影响 HTTPS,但会掩盖真正的 AH00526 错误。apachectl -S 里若 443 的 default server 不是你的域名,说明 <VirtualHost *:443> 缺少 ServerName 或没被 Include 进来——这是 apache ssl证书安装 后最常见的「证书明明装了却报 CN 不匹配」的原因。

第二步:端口监听与 HTTP 跳转。

sudo ss -lntp | grep -E ':443|:80'
# 期望:users:(("httpd",pid=...)) 或 ("apache2",...)

curl -I http://example.com
# 期望:HTTP/1.1 301 Moved Permanently
#       Location: https://example.com/
# 用 308 亦可(保留请求方法),关键是 Location 带 https 且带结尾斜杠

第三步:证书链是否完整。据 Let’s Encrypt 文档,服务端必须下发「叶子证书 + 中间证书」,缺中间证书时桌面浏览器能靠缓存补全,而 Android、Java 客户端会直接失败。

openssl s_client -connect example.com:443 -servername example.com -showcerts /dev/null 
  | openssl x509 -noout -subject -issuer -dates
# subject=CN = example.com
# issuer=C = US, O = Let's Encrypt, CN = R3
# notAfter=...            ← 顺手核对到期日

openssl s_client -connect example.com:443 -servername example.com /dev/null 
  | grep "Verify return code"
# 期望:Verify return code: 0 (ok)
# 若为 20 (unable to get local issuer certificate) → 证书链不全
# 若为 10 (certificate has expired) → 证书过期

第四步:协议版本与套件。RFC 8446 定义的 TLS 1.3 在 OpenSSL 1.1.1+ 才支持;而 OpenSSL 3.x 默认 SECLEVEL=2 且编译时禁用 TLS 1.0/1.1,此时测 -tls1_1 会报 no protocols available,那是客户端拒绝而非服务端问题,别误判。

openssl s_client -connect example.com:443 -servername example.com -tls1_2 /dev/null | grep "Protocol"
# 期望:Protocol  : TLSv1.2

openssl s_client -connect example.com:443 -servername example.com -tls1_1 /dev/null
# 期望失败:alert protocol version(说明 SSLProtocol 已正确禁用旧版)

nmap --script ssl-enum-ciphers -p443 example.com   # 一次性列出协议与套件,弱套件会标注 WEAK

第五步:日志与在线评分。

sudo tail -f /var/log/httpd/ssl_error_log   # Debian/Ubuntu: /var/log/apache2/error.log
curl -vI https://example.com 2>&1 | grep -i "SSL certificate verify"
# 期望:SSL certificate verify ok.

据 Qualys SSL Labs 评级规则,A 级以上要求无 RC4/3DES、支持 TLS 1.2 且密钥 ≥2048 位;A+ 额外要求 HSTS(Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains")。若日志出现 AH02032: Hostname ... provided via SNI and hostname ... provided via HTTP are different,通常是 CDN 回源与 SNI 不一致。

第六步:OCSP Stapling。按 RFC 6960,stapling 由服务端缓存 OCSP 响应随握手下发,省去客户端单独查询。

openssl s_client -connect example.com:443 -servername example.com -status /dev/null 
  | grep -A2 "OCSP Response Status"
# 期望:OCSP Response Status: successful (0x0)
# 若为 "no response sent",检查 SSLUseStapling on 与 SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"
检查项 命令 通过标准 失败含义
配置语法 apachectl configtest Syntax OK 指令错误或模块未加载
虚拟主机 apachectl -S 443 namevhost 为你的域名 证书与域名错配
监听 ss -lntp httpd 占用 :443 端口冲突或未监听
跳转 curl -I http:// 301/308 + https Location RewriteRule 未生效
证书链 s_client | grep Verify 0 (ok) 缺中间证书
协议 nmap ssl-enum-ciphers 仅 TLS1.2/1.3 SSLProtocol 未收紧
OCSP s_client -status successful 缓存路径不可写

问:源站前面有 CDN,这些命令还有效吗?无效——TLS 在 CDN 边缘终止,s_client 测到的是 CDN 的证书。此时在 CDN 控制台查看证书链与 OCSP 状态,并确认回源协议设为 Full (Strict),否则源站的自签或过期证书不会报错,形成静默降级。

生产加固与证书续期:OCSP、HSTS 与 Let's Encrypt 自动化

OCSP stapling 怎么开?为什么开了还报 AH02217?OCSP stapling 让 Apache 代替客户端去 CA 查询吊销状态(协议见 RFC 6960),省掉客户端一次外部请求。配置必须写在全局作用域(ssl.conf 顶层,不能放进 <VirtualHost>),Apache 2.4.6+ 的 mod_ssl 原生支持;SSLStaplingCache 默认未设置,未设置就等于不启用 stapling:

# /etc/httpd/conf.d/ssl.conf 或 /etc/apache2/mods-enabled/ssl.conf
# 关键行:全局开启 stapling 并分配共享内存缓存
SSLUseStapling on
SSLStaplingCache shmcb:/var/run/ocsp(128000)   # 128000 字节,Apache 官方示例值
SSLStaplingResponderTimeout 5                   # 默认 10 秒,内网环境可调小
SSLStaplingReturnResponderErrors off            # 默认 on,建议关掉避免把错误响应钉给客户端

验证(重点看 OCSP Response Status):

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

如果 error_log 出现 AH02217: ssl_stapling_init_cert: can't retrieve issuer certificate!,几乎都是证书链没装全——stapling 需要中间证书才能找到 OCSP responder。Apache 2.4.8 起 SSLCertificateChainFile 已废弃,中间证书直接追加进 SSLCertificateFile 指向的文件(顺序:域名证书 → 中间证书);2.4.6/2.4.7 则仍需单独配置 SSLCertificateChainFile。这也是为什么在讲 apache ssl证书安装 时反复强调“fullchain”而非“cert”。

HSTS 头怎么写才不把自己锁死?HSTS 由 RFC 6797 定义,浏览器只在 HTTPS 响应中接受该头。max-age 单位是秒,默认无(不设置即不启用);一旦下发,在有效期内用户无法“点继续访问”跳过证书错误。因此上线节奏应是 300 → 86400 → 31536000,确认所有子域都能 HTTPS 后再加 includeSubDomains:

# 需要 mod_headers(a2enmod headers)
# 阶段一:5 分钟,用于验证
Header always set Strict-Transport-Security "max-age=300"
# 阶段二(最终形态):一年 + 覆盖子域
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

据 Let’s Encrypt 文档,其证书有效期固定为 90 天,官方建议每 60 天续期一次;用 certbot --apache 申请时会自动写入 systemd timer 或 cron:

certbot --apache -d example.com -d www.example.com     # 自动改 vhost 并 reload
certbot renew --dry-run                                # 演练,不消耗速率限制
systemctl list-timers | grep certbot                   # 应看到 certbot.timer
#!/bin/sh
# /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh  需 chmod +x
apachectl configtest && systemctl reload apache2   # RHEL/CentOS 换成 httpd

私钥和到期怎么兜底?私钥权限必须 chmod 600 + chown root:root,绝不可放在 Web 根目录,并离线备份一份;完整性用 openssl rsa -check -in example.com.key -noout 检查(正常输出 RSA key ok)。RSA 2048 位是当前通用下限,新站点建议 ECDSA P-256。到期时间用 openssl x509 -in fullchain.crt -noout -enddate 读出 notAfter,提前 30 天告警:

监控方案 探测方式 关键指标 / 命令 告警粒度 适用规模
cron + openssl 本地读证书或远程握手 openssl x509 -checkend 2592000 -noout 天级 单机、小站
Zabbix Agent2 web.certificate.get[example.com,443] 证书 expiry 项 分钟级 中小集群
Prometheus blackbox_exporter 主动探测 /probe?module=https probe_ssl_earliest_cert_expiry 与 time() 差值 秒~分钟 中大型、多域名
# 最省事的自检:远程看还剩多少天,不足 30 天返回非 0
openssl s_client -connect example.com:443 -servername example.com /dev/null 
  | openssl x509 -noout -checkend 2592000
# 输出 "Certificate will expire" 即触发告警

做完以上四项,apache ssl证书安装 才算从“能访问”走到“可长期运维”;OCSP、HSTS、自动续期、到期监控任一缺失,都会在 90 天后的某个凌晨变成线上事故。

先查证书链是否完整:openssl s_client -connect 域名:443 -servername 域名 -showcerts 应返回至少 2 张证书,且 Verify return code 为 0 (ok)。再查页面是否混用 http 资源、ServerName 与证书 SAN 是否一致;混合内容需把图片、JS、CSS 改为 https 或相对路径。

不是必须。Apache 2.4.8 起 SSLCertificateFile 可加载包含服务器证书和中间证书的 fullchain.crt,SSLCertificateChainFile 仍可用但属兼容写法。CentOS 7 常见 Apache 2.4.6 更常分开写 SSLCertificateFile 与 SSLCertificateChainFile。

Apache 用 mod_ssl 和 VirtualHost,核心指令是 SSLEngine on、SSLCertificateFile、SSLCertificateKeyFile;Nginx 用 server 块和 ssl_certificate、ssl_certificate_key。两者都要求 fullchain 顺序正确,但 Apache 2.4.6 常需单独写 chain,Nginx 通常把 chain 直接拼进 ssl_certificate 文件。

先执行 ls -l 检查配置中的证书路径是否存在、文件是否为空,再运行 apachectl configtest 定位行号。RHEL/CentOS 还要执行 restorecon -Rv /etc/httpd/ssl 修复 SELinux 上下文;Debian/Ubuntu 注意配置里写的是 /etc/apache2/ssl 还是自定义目录。

可以用于多数公开网站,但有效期 90 天,必须保证自动续期和重载 Apache 的钩子可靠。certbot –apache 可自动改 VirtualHost;续期后执行 apachectl configtest && systemctl reload apache2 或 httpd,避免新证书未加载。

先确认 OpenSSL 1.1.1+ 且 Apache 2.4.36+,配置中 SSLProtocol 未排除 TLSv1.3。执行 openssl s_client -connect 域名:443 -servername 域名 -tls1_3


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

内容审核

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