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

Apache HTTPS 配置教程:Apache 2.4 + OpenSSL 从 CSR 到 TLS 1.3 全流程

apache https 配置教程 — Apache HTTPS 配置教程:Apache 2.4 + OpenSSL 从 CSR 到 TLS 1.3 全流程
核心结论

Apache 开启 HTTPS 只需 5 个核心指令:在 中写 SSLEngine on、SSLCertificateFile(指向含中间证书的 fullchain)、SSLCertificateKeyFile、SSLProtocol -all +TLSv1.2 +TLSv1.3、SSLCipherSuite,apachectl configtest 输出 Syntax OK 后 systemctl reload httpd 即生效。 Apache 2.4.8 起 SSLCertificateChainFile 已废弃,中间证书必须用 cat server.crt intermediate.crt > fullchain.pem 拼进 SSLCertificateFile,否则 Java 8、Android 7.1 以下客户端会报 unable to get local issuer certificate。 TLS 1.3 的前提是 OpenSSL 1.1.1 以上 + Apache 2.4.36 以上,用 openssl version 与 httpd -v 核对;开启后 SSL Labs 通常直接给 A,再补 Strict-Transport-Security max-age=63072000 可达 A+。 Let's Encrypt 免费 DV 证书 90 天有效期,certbot --apache -d example.com -d www.example.com 一条命令完成签发与 vhost 改写,续期靠 certbot.timer,务必加 --deploy-hook “systemctl reload httpd”。 验证三步:openssl s_client -connect example.com:443 -servername example.com -status 看 issuer 与 OCSP,curl -sI 看 HSTS 与 301 跳转,curl -v --tlsv1.1 应握手失败并返回 alert protocol version。

为什么 Apache 明明配了 SSL 还是打不开 HTTPS?先看 6 个根本原因

很多人按着 apache https 配置教程一步步改完 ssl.conf,systemctl restart 也没报错,浏览器却依然返回”无法访问此网站”或 ERR_CONNECTION_REFUSED。原因几乎都能归到这 6 类,按顺序排查最快。

1. mod_ssl 到底加载了吗?

Apache 2.4 的全部 TLS 能力都来自 mod_ssl,模块缺失时 SSLEngine on 只会被当成未知指令。先验证:

# 有输出(如 ssl_module (shared))说明已加载;无输出即模块缺失
httpd -M | grep ssl_module        # Debian/Ubuntu 用 apache2ctl -M

# RHEL / CentOS 8/9 / Rocky:装包会自动生成 /etc/httpd/conf.d/ssl.conf
dnf install mod_ssl

# Debian / Ubuntu:模块以软链接形式启用
a2enmod ssl && systemctl restart apache2

2. 443 端口真的在监听吗?

ss -lntp | grep :443
# 为空时先确认 vhost 被 Include 进来了(输出里应能看到 *:443 的 vhost)
httpd -S

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

云主机还要单独检查安全组入方向规则——本机 ss 有监听但外网连不上,九成是安全组没放行 443。

3. SSLProtocol 的默认值其实不安全

Apache 2.4 中 SSLProtocol 的默认值是 all -SSLv3,仍包含 TLSv1.0 与 TLSv1.1;而 Chrome 98+、Firefox 78+ 已彻底移除对这两个版本的支持,握手会直接失败。必须显式声明:

# 需要 OpenSSL 1.1.1+ 才能启用 TLSv1.3
SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLCipherSuite HIGH:!aNULL:!MD5   # 可参考 Mozilla SSL Configuration Generator 的 intermediate 配置

4. 证书链是否完整?

只部署 leaf 证书、缺中间 CA 时,Java 8、Android 7.1 以下及部分 curl 版本会抛 unable to get local issuer certificate。据 Apache 2.4 官方文档,SSLCertificateChainFile 自 2.4.8 起已废弃,正确做法是把中间证书直接拼接在 SSLCertificateFile 指向的文件末尾:

cat your_domain.crt intermediate_ca.crt > fullchain.pem
# 验证链是否完整,输出应以 0 (ok) 结尾
openssl verify -untrusted intermediate_ca.crt fullchain.pem

5. 私钥和证书是配对的吗?

手工复制文件时拿错 key 极常见,Apache 会启动失败并抛 AH02572: Failed to configure at least one certificate and key。两个 MD5 必须一致:

openssl x509 -noout -modulus -in cert.pem | openssl md5
openssl rsa  -noout -modulus -in key.pem  | openssl md5

6. 反向代理下为什么样式全丢?

Apache 做前置代理时若未告知后端真实协议,应用仍会生成 http:// 资源地址,Chrome 控制台报 Mixed Content。在 vhost 中补一行,或加 CSP 兜底:

RequestHeader set X-Forwarded-Proto "https"
Header always set Content-Security-Policy "upgrade-insecure-requests"
症状 一条命令定位 结论
启动报 unknown directive “SSLEngine” httpd -M | grep ssl_module mod_ssl 未安装/未启用
ERR_CONNECTION_REFUSED ss -lntp | grep :443 vhost 未生效或防火墙/安全组拦截
ERR_SSL_PROTOCOL_ERROR openssl s_client -connect 域名:443 -tls1_2 协议版本或加密套件不合规
部分客户端报 issuer 错误 openssl verify -untrusted ca.crt fullchain.pem 中间证书缺失
AH02572 两次 -modulus 比对 MD5 私钥与证书不匹配
控制台 Mixed Content 查看响应头是否含 X-Forwarded-Proto 代理未传递原始协议

排查完这 6 项,绝大多数”配了 SSL 却打不开”的场景都能闭环;剩下的问题通常才轮到 CSR 生成、OCSP Stapling 与 HSTS 这些进阶配置。

Apache HTTPS 配置的完整实操步骤(Apache 2.4.62 + OpenSSL 3.0 实测)

第 1 步:先核对版本,别在旧协议栈上折腾 TLS 1.3。

# 三行确认版本、OpenSSL、模块是否到位
httpd -v | head -1
# 实测输出:Server version: Apache/2.4.62 (Unix)
openssl version
# 实测输出:OpenSSL 3.0.13 —— 必须 ≥1.1.1,否则 TLS 1.3(RFC 8446)无从谈起
httpd -M | grep -E "ssl_module|http2_module"
# 期望:ssl_module (shared) / http2_module (shared)

# 改配置前做带日期的备份,回滚只需一条 cp
cp -a /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.bak.$(date +%F)

Apache 2.4.36 是首个支持 TLS 1.3 的分支,前提是编译时链接 OpenSSL 1.1.1+;2.4.62 发布于 2024 年 7 月,是当前 2.4.x 稳定线。Debian/Ubuntu 体系把 httpd -v 换成 apache2 -v、httpd -M 换成 apache2ctl -M,配置目录是 /etc/apache2/。据 Apache mod_ssl 官方文档,模块清单里没有 ssl_module 时,任何 HTTPS 配置都不会生效,先装 mod_ssl(RHEL 系 dnf install mod_ssl)再往下走。

第 2 步:生成私钥与 CSR,SAN 必须一次写全。

# 路线 A:RSA 2048,兼容性最广;-addext 需要 OpenSSL 1.1.1+
openssl req -new -newkey rsa:2048 -sha256 -nodes 
  -keyout /etc/pki/tls/private/example.com.key 
  -out /etc/pki/tls/certs/example.com.csr 
  -subj "/C=CN/O=Example/CN=example.com" 
  -addext "subjectAltName=DNS:example.com,DNS:www.example.com"

# 路线 B:ECDSA P-256,先出私钥再签 CSR(ecparam 不能与 -newkey 混用)
openssl ecparam -genkey -name prime256v1 -out /etc/pki/tls/private/example.com.key
openssl req -new -key /etc/pki/tls/private/example.com.key -sha256 
  -out /etc/pki/tls/certs/example.com.csr 
  -subj "/CN=example.com" 
  -addext "subjectAltName=DNS:example.com,DNS:www.example.com"

# 自检:SAN 是否覆盖全部对外域名
openssl req -in /etc/pki/tls/certs/example.com.csr -noout -text | grep -A1 "Subject Alternative Name"

按 RFC 5280 与 CA/Browser Forum 基线要求,现代浏览器只认 subjectAltName,CN 已完全不被采信——只填 CN 的 CSR 签出来的证书会直接触发 NET::ERR_CERT_COMMON_NAME_INVALID。

对比项 RSA 2048 ECDSA P-256
握手体积 较大,证书链约 1.5–2 KB 小一个量级,移动端首包更快
兼容性 覆盖全部客户端 不支持 IE11 / Android 4.x
生成命令 -newkey rsa:2048 ecparam -genkey -name prime256v1
签名算法 sha256WithRSAEncryption ecdsa-with-SHA256

第 3 步:拿证书。免费路线用 Let’s Encrypt,需 certbot ≥2.0:

certbot --apache -d example.com -d www.example.com
# 会自动改写 vhost 的 SSLCertificateFile/KeyFile,并补上 80→443 跳转

据 Let’s Encrypt 文档,其证书有效期 90 天,certbot renew --dry-run 应纳入上线检查项。商业证书则把 CSR 全文提交 CA:DV 通常 10 分钟内签发,OV/EV 需 1–3 个工作日人工核验企业资质。

第 4 步:合并证书链并修正权限,顺序错了就是链不完整。

# 服务器证书在前、中间证书在后,顺序颠倒会导致部分客户端报 chain incomplete
cat example.com.crt intermediate.crt > /etc/pki/tls/certs/example.com-fullchain.pem
grep -c "BEGIN CERTIFICATE" /etc/pki/tls/certs/example.com-fullchain.pem
# 期望输出:2(漏了中间证书就会是 1)

chmod 600 /etc/pki/tls/private/example.com.key
chown root:root /etc/pki/tls/private/example.com.key
restorecon -Rv /etc/pki/tls   # SELinux 环境下必做,否则 httpd 读不到私钥

第 5 步:写 443 虚拟主机。本节这份 apache https 配置教程以 RHEL 路径为例,Debian 系把文件放到 /etc/apache2/sites-available/example.com-ssl.conf 再 a2ensite 即可:

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

    SSLEngine on
    SSLCertificateFile    /etc/pki/tls/certs/example.com-fullchain.pem
    SSLCertificateKeyFile /etc/pki/tls/private/example.com.key

    # 2.4.62 的 mod_ssl 默认 SSLProtocol 为 "all -SSLv3",这里显式收紧
    SSLProtocol           -all +TLSv1.2 +TLSv1.3
    SSLCipherSuite        ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305
    SSLHonorCipherOrder   off   # TLS 1.3 下该指令无效,选序由客户端决定

    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</VirtualHost>

Header 指令依赖 mod_headers;HSTS 默认没有,必须显式下发。加密套件可参考 Mozilla SSL Configuration Generator 的 intermediate 档位,比手写更稳妥。

第 6 步:语法检查、热加载、补 80→443 跳转。

apachectl configtest        # 等价 httpd -t,必须回显 Syntax OK 才能 reload
systemctl reload httpd      # 热加载,不打断在线连接

# 80 端口 vhost 内:域名硬编码,别用 %{HTTP_HOST} 拼接(开放重定向风险)
Redirect permanent / https://example.com/

验证 TLS 1.3 是否真正生效:

openssl s_client -connect example.com:443 -servername example.com -tls1_3 </dev/null 2>/dev/null | grep -E "Protocol|Cipher"
# 期望:Protocol: TLSv1.3 / Cipher: TLS_AES_128_GCM_SHA256

至此这份 apache https 配置教程的主链路走完;下一步通常是把 HSTS 的 max-age 从 300 秒逐步抬到 31536000、加入 OCSP Stapling(SSLUseStapling on + SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"),以及用 sslscan 复核是否残留 TLS 1.0/1.1。

关键配置片段与命令示例:可直接抄的 ssl.conf 写法

问:照着网上教程抄下来的 ssl.conf,apachectl configtest 直接报 AH00526: Syntax error on line …,或者站点能开但 OCSP 装订始终不生效,问题出在哪?

答:绝大多数情况是把指令放错了层级,或者漏了模块。下面这段 443 vhost 以 Apache 2.4.37+(RHEL/CentOS 8)配合 OpenSSL 1.1.1 为基准,加密套件串直接取自 Mozilla SSL Configuration Generator 的 Intermediate 方案,可原样复制:

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

    SSLEngine on
    # fullchain 必须是「站点证书 + 中间证书」的串联,缺中间证书会导致部分安卓/旧客户端握手失败
    SSLCertificateFile    /etc/pki/tls/certs/example.com-fullchain.pem
    SSLCertificateKeyFile /etc/pki/tls/private/example.com.key

    # Apache 2.4.23+ 默认 SSLProtocol 为 all -SSLv3,这里显式收敛到 1.2/1.3
    SSLProtocol           -all +TLSv1.2 +TLSv1.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   off   # 默认即 off;TLS 1.3 下该指令本就不起作用,客户端优先更利于移动端
    SSLSessionTickets     off   # 2.4.11+ 引入,默认 on;关掉可避免 ticket 密钥长期不轮换削弱前向保密
    Protocols             h2 http/1.1   # 需已加载 mod_http2,否则 configtest 报错
</VirtualHost>

问:OCSP 装订为什么一定要写在 vhost 外面?因为 SSLStaplingCache 属于 server config 级指令,写在 <VirtualHost> 内会直接报 AH00526;而 SSLUseStapling 虽可写在 vhost 内,但缓存缺失时装订不会生效。据 RFC 6960 与 Apache 2.4 文档,推荐写法是:

# 放在 httpd.conf 或 ssl.conf 的全局区(ServerRoot 下 logs/ 目录需可写)
SSLUseStapling                  on
SSLStaplingCache                "shmcb:logs/stapling-cache(150000)"
SSLStaplingResponderTimeout     5      # 默认 10 秒,缩短可避免首字节延迟
SSLStaplingReturnResponderErrors off   # 默认 on,关闭后不把错误响应回传给客户端

150000 字节约可缓存 1 万条 OCSP 响应,中小站点绰绰有余;若日活很高可按 1 万条≈150KB 线性放大。

问:HSTS 怎么加才不会被自己锁死?需 mod_headers(Debian/Ubuntu 执行 a2enmod headers 后 reload)。据 RFC 6797,max-age=63072000 即 2 年,是提交 hstspreload.org 的门槛之一,务必确认全站(含所有子域)HTTPS 可用后再加 includeSubDomains:

Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
Header always set X-Content-Type-Options "nosniff"
Header always set Content-Security-Policy "upgrade-insecure-requests"

问:80 端口跳转用 Redirect 还是 RewriteRule?

写法 状态码 开销 注意事项
Redirect permanent / https://example.com/ 301 无需 mod_rewrite,少一次规则匹配 整目录跳转,需为 ACME 单独放行
RewriteRule ^(.*)$ https://example.com$1 [R=301,L] 301 每次请求走一遍 rewrite 引擎 可写条件判断,灵活但更慢
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    # 放行 ACME HTTP-01 验证目录,否则 certbot renew 必然失败
    <Location "/.well-known/acme-challenge/">
        Require all granted
    </Location>
    Redirect permanent / https://example.com/
</VirtualHost>

问:改完之后按什么顺序验证?

apachectl configtest                 # 必须输出 Syntax OK
systemctl reload httpd               # 热加载,Debian 系为 apache2
ss -lntp | grep :443                 # 确认 443 处于 LISTEN
tail -f /var/log/httpd/ssl_error_log # Debian 系为 /var/log/apache2/ssl_error.log

问:证书签发和续期命令怎么写?Let’s Encrypt 官方文档推荐用 deploy-hook 而非重启,续期后自动热加载:

certbot --apache -d example.com -d www.example.com 
  --deploy-hook "systemctl reload httpd"

# 演练续期链路,不消耗速率限制
certbot renew --dry-run

只有输出 Congratulations, all simulated renewals succeeded 才算真正跑通;若报 Failed to renew certificate … with error: urn:ietf:params:acme:error:unauthorized,先回头检查上面对 .well-known/acme-challenge/ 的放行是否被 301 抢先匹配。

参数对比与方案选型:证书类型、密钥算法、TLS 版本怎么选

选型时真正需要回答的只有三个问题:证书买哪一档、私钥用哪种算法、协议开到哪个版本。这三项决定后续所有 Apache HTTPS 配置教程里 SSLCertificateFile 与 SSLProtocol 的写法,先定下来能少返工。

证书类型怎么选?按 2025 年国内市场常见区间(价格仅供参考,不同 CA 差异较大):

方案 有效期 / 审核 参考价格 适用场景
Let’s Encrypt DV 90 天,自动续期 免费 个人站、内部系统、CI 环境
商业 DV 单域名 1 年 ¥100–¥600/年 中小企业官网
OV 单域名 1 年,需企业资质 ¥800–¥3000/年 电商、需展示组织信息
EV 单域名 1 年,扩展验证 ¥2000–¥8000/年 金融;Chrome 77+ 已不再显示绿色地址栏
通配符 DV 90 天或 1 年 免费 / ¥800–¥2500/年 *.example.com 多子域场景

据 Let’s Encrypt 官方文档,其证书有效期固定 90 天,官方推荐用 ACME 客户端在剩余 30 天时自动续期——这也是 DV 免费档能长期稳定跑在生产上的原因,续期失败往往比证书本身更致命。

RSA 还是 ECDSA?两者的差异不只是”快慢”:

维度 RSA 2048 ECDSA P-256
密钥长度 / 签名长度 2048 bit / 256 字节 256 bit / 64 字节
等效安全强度 约 112 bit 约 128 bit
握手耗时 基线 通常低 30%–50%
兼容性下限 Windows XP、Java 6 均可 需 Windows Vista+、Android 4.4+、Java 7+
证书体积 约 1.2 KB 约 0.6 KB

生成 P-256 私钥与 CSR 的标准命令如下(OpenSSL 1.1.1 与 3.x 通用):

# 1) 生成 P-256 私钥(等价于 prime256v1 / secp256r1)
openssl ecparam -name prime256v1 -genkey -noout -out ec.key
# 2) 用私钥生成 CSR,SAN 建议在配置文件中写,-subj 仅作示例
openssl req -new -key ec.key -out ec.csr -subj "/CN=www.example.com"
# 3) 签发后核对证书里的公钥算法,应为 id-ecPublicKey + prime256v1
openssl x509 -in fullchain.pem -noout -text | grep -E "Public Key Algorithm|NIST CURVE|Signature Algorithm"

对外公众站点优先 RSA 2048,客户端可控(内部 API、App 后端)时换 ECDSA;想两头兼顾,见下文双证书方案。

TLS 1.2 还是 1.3?TLS 1.3(RFC 8446)砍掉了静态 RSA 密钥交换与 CBC 模式,握手从 2-RTT 降到 1-RTT,会话恢复可 0-RTT。代价是环境门槛:Apache ≥ 2.4.36 且 OpenSSL ≥ 1.1.1 才能稳定工作,低于此版本写 +TLSv1.3 会直接启动失败:

AH00526: Syntax error on line 42 of /etc/apache2/sites-enabled/ssl.conf:
SSLProtocol: Illegal protocol 'TLSv1.3'

另一个易踩的坑:TLS 1.3 的密码套件不受 SSLCipherSuite 控制,改它只会影响 1.2 及以下。需要裁剪 1.3 套件时用 SSLConfCmd Ciphersuites ... 透传给 OpenSSL。

项 TLS 1.2(RFC 5246) TLS 1.3(RFC 8446)
握手往返 2-RTT,恢复 1-RTT 1-RTT,恢复 0-RTT
密钥交换 RSA / ECDHE 均可 仅 (EC)DHE,无静态 RSA
对称模式 CBC + GCM + ChaCha20 仅 AEAD,CBC 被移除
套件配置方式 SSLCipherSuite 生效 需 SSLConfCmd Ciphersuites

证书链与双证书怎么写?SSLCertificateChainFile 自 Apache 2.4.8 起废弃,但大量老教程仍在用;它与 fullchain.pem 混写会把中间证书发两遍,每次握手多传 1–2 KB,个别旧版 Android 会直接握手失败。当前推荐合并式单文件,迁到 Nginx、HAProxy、CDN 时同一份文件可直接复用。Apache 2.4.8+ 还允许在同一个 <VirtualHost *:443> 内写多组证书,由服务端按 ClientHello 的签名算法能力自动挑:

# 适用:Apache 2.4.36+ / OpenSSL 1.1.1+(Debian、RHEL 8+ 默认组合)
<VirtualHost *:443>
    ServerName www.example.com
    DocumentRoot /var/www/html

    # 第一组:ECDSA P-256,支持时优先命中
    SSLCertificateFile    /etc/letsencrypt/live/example.com/ecdsa/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/ecdsa/privkey.pem
    # 第二组:RSA 2048 兜底,服务端自动回退
    SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    # 关键:不要再写 SSLCertificateChainFile,fullchain 已含中间证书
    SSLProtocol         -all +TLSv1.2 +TLSv1.3
    SSLCipherSuite      ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305
    SSLHonorCipherOrder off   # 默认即 off,交给客户端更利于移动端走 ChaCha20
</VirtualHost>

改完执行 apachectl configtest 后 reload,再用下面两条命令核对实际协商结果与证书链完整性:

# 看协商到的协议与套件,可把 -tls1_3 换成 -tls1_2 做对照
openssl s_client -connect www.example.com:443 -servername www.example.com -tls1_3 </dev/null 2>/dev/null | grep -E "Protocol|Cipher"
# 检查链是否完整,输出应以 "Verify return code: 0 (ok)" 结束
openssl s_client -connect www.example.com:443 -servername www.example.com -showcerts </dev/null 2>/dev/null | grep "Verify return code"

结论性建议(可执行的默认值):日均 PV 10 万以下、无合规硬性要求的站点,直接上「Let’s Encrypt DV + RSA 2048 + TLS 1.2/1.3 + fullchain 单文件」,这是本 Apache HTTPS 配置教程后续所有示例的基线。有 PCI DSS 4.0 要求的支付链路必须禁用 TLS 1.0/1.1 并保留 12 个月日志,此时 SSLProtocol -all +TLSv1.2 +TLSv1.3 是硬性下限而非优化项;OV 证书的组织信息核验在部分行业招标中同样是硬门槛,不能仅以”浏览器不显示差别”为由省掉。Mozilla SSL Configuration Generator 可按 Apache 与 OpenSSL 版本直接生成对应的中间配置档,比自己拼套件列表更稳。

常见错误与排查:6 条真实报错信息对照处理

六条报错全部来自 Apache 2.4(mod_ssl 2.4.x)+ OpenSSL 1.1.1/3.x 的真实日志,日志位置在 RHEL 系为 /var/log/httpd/ssl_error_log,Debian 系为 /var/log/apache2/ssl_error_log,也可用 journalctl -u httpd -n 50 直接看。先查表定位,再按下面命令逐条处理。

报错关键字 根本原因 一行定位命令
AH00526 SSLCertificateFile does not exist 装了 mod_ssl 未替换默认自签证书路径 ls -l /etc/pki/tls/certs/localhost.crt
AH02572 Failed to configure at least one certificate and key 缺 SSLEngine on,或证书指令写在 vhost 之外 httpd -S
AH02217 ssl_stapling_init_cert 证书链缺中间证书 openssl s_client -connect … -status
AH01909 does NOT include an ID which matches the server name ServerName 不在证书 SAN 中 / 默认 vhost 不匹配 httpd -S
AH00072 make_sock: could not bind to address [::]:443 端口被 nginx 或残留 httpd 占用 ss -lntp | grep :443
sslv3 alert handshake failure 协议版本或密钥算法不匹配 openssl s_client -tls1_1

1. AH00526:证书文件不存在或为空。典型是 yum install mod_ssl 后没换掉 /etc/pki/tls/certs/localhost.crt。确认文件非 0 字节,SELinux 环境还要修上下文,否则 httpd 读不到文件仍会报同样的错:

# 确认存在且大小 > 0;本地证书默认应由 mod_ssl 生成
ls -l /etc/pki/tls/certs/localhost.crt /etc/pki/tls/private/localhost.key
# SELinux 上下文修正(RHEL/CentOS 系)
restorecon -Rv /etc/pki/tls
# 指向你自己的证书后先做语法检查,避免 reload 后整站 503
apachectl configtest   # 期望输出 Syntax OK

2. AH02572:至少需要一个证书和私钥。99% 是 <VirtualHost *:443> 里漏了 SSLEngine on(默认值 off,mod_ssl 文档明确说明),或 SSLCertificateFile 落在了 </VirtualHost> 之后导致不被继承:

# 打印生效的 vhost 与 SSL 指令归属,看证书是否挂在正确的块里
httpd -S
grep -n -E "VirtualHost|SSLEngine|SSLCertificate(File|KeyFile)" /etc/httpd/conf.d/ssl.conf

3. AH02217:取不到签发者证书。OCSP 装订需要完整链(叶子 + 中间证书),只放域名证书必然失败。补齐 fullchain 后复验,紧急恢复可临时 SSLUseStapling off(默认即 off,开启时必须同时配置 SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"),代价是丢失装订带来的握手加速:

openssl s_client -connect example.com:443 -servername example.com -status </dev/null | grep -A2 "OCSP response"

4. AH01909:证书 SAN 与 ServerName 不符。浏览器不报错但日志刷屏,说明存在 SNI 未命中后回落到第一个 443 vhost 的情况。httpd -S 的输出顺序即默认站点顺序,务必让默认 vhost 的证书覆盖所有未匹配域名,或用 openssl x509 -in cert.pem -noout -text | grep DNS 核对 SAN。

5. AH00072:443 已被占用。常见是同时装了 nginx。注意 IPv4/IPv6 是两个 socket,要分别确认:

ss -lntp | grep -E ':443b'      # 或 lsof -iTCP:443 -sTCP:LISTEN
systemctl stop nginx
systemctl restart httpd && systemctl status httpd --no-pager

6. sslv3 alert handshake failure。服务端按 Mozilla 推荐配置了 SSLProtocol -all +TLSv1.2 +TLSv1.3(TLS 1.3 需 Apache 2.4.36+ 与 OpenSSL 1.1.1+),而客户端只会 TLS 1.0/1.1,或客户端不支持 ECDSA 证书。用降级握手复现,再回 ssl_error_log 对照客户端 IP:

openssl s_client -connect example.com:443 -tls1_1 -servername example.com
# 若返回 handshake failure,即为协议被禁用;确认客户端能力后再决定是否放宽 SSLProtocol

效果验证:怎么确认 Apache HTTPS 配置真的生效了

改完 ssl.conf 重启了服务,浏览器地址栏也出现了小锁,这就算配好了吗?不一定——小锁只代表”能握手”,不代表证书链完整、旧协议已禁用、跳转没写错。下面按”服务端 → 证书 → 协议 → 响应头 → OCSP → 第三方”六层逐项验证,全部通过才算 HTTPS 真正生效。

第一层:配置有没有被真正加载。三条命令缺一不可,任何一条不过,后面的验证都是白做。

# 1) 语法检查:期望输出 "Syntax OK"(RHEL/CentOS 用 httpd -t)
apachectl configtest

# 2) 服务状态:期望 active (running),失败时看最后 20 行日志定位
systemctl status httpd          # Debian/Ubuntu 服务名为 apache2
journalctl -u httpd -n 20 --no-pager

# 3) 端口监听:需 root 才能看到进程名,期望出现 httpd 或 apache2
ss -lntp | grep ':443'

如果 configtest 报 AH00526: Syntax error,说明配置根本没被加载,此时浏览器能访问多半是旧进程还在跑旧配置——务必确认 systemctl reload httpd 真的执行成功。

第二层:证书链与有效期。据 Let’s Encrypt 文档,服务器必须下发”叶子证书 + 中间证书”,缺中间证书时桌面浏览器可能正常(有缓存),但 Android、Java 客户端会直接报 unable to get local issuer certificate。

openssl s_client -connect example.com:443 -servername example.com -showcerts /dev/null 
  | openssl x509 -noout -subject -issuer -dates
# issuer 应为中间 CA(如 R3、E1),不是根 CA;notAfter 距今应 > 30 天
# subject 的 CN 与 SAN 必须包含实际访问域名

第三层:协议版本。Apache 2.4 的 SSLProtocol 默认值是 all -SSLv3,TLS 1.0/1.1 仍然开着,必须显式写 SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1。验证方式:

# 期望失败,报 error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version
curl -v --tlsv1.1 --tls-max 1.1 https://example.com

# 期望成功,输出含 SSL connection using TLSv1.3
# 注意:需 curl 7.52+ 与 OpenSSL 1.1.1+,Apache 2.4.36+ 才默认支持 TLS 1.3
curl -v --tlsv1.3 https://example.com 2>&1 | grep -i "SSL connection using"

第四层:响应头与跳转。HSTS 的 max-age=63072000(两年)是常见起点,一旦下发且带 includeSubDomains,回退成本极高,建议先用 300 秒试跑。

curl -sI https://example.com | grep -iE 'strict-transport-security|content-security-policy'
curl -sI http://example.com  | grep -iE '^HTTP/|^location'   # 期望 301 + Location: https://example.com/

第五层:OCSP 装订。SSLUseStapling on 能省掉客户端一次 CA 查询,但 Apache 需要出站 80 端口访问 CA 的 Responder。

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

若返回 no response sent,九成是出站 80 被防火墙拦截,而非配置写错。

检查项 关键命令/参数 通过标准 常见失败原因
配置加载 apachectl configtest Syntax OK 模块未 LoadModule
证书链 s_client -showcerts issuer 为中间 CA 漏配 SSLCertificateChainFile
协议 curl --tlsv1.1 握手失败 SSLProtocol 未收紧
跳转 curl -sI http:// 301 → https RewriteRule 顺序错
综合评分 SSL Labs / testssl.sh A 或 A+ 弱套件、缺 HSTS

第六层:第三方交叉验证。Qualys SSL Labs 的评分目标是 A 或 A+,重点看 Certificate 一栏是否显示链完整、Protocols 是否只剩 TLS 1.2/1.3。上线前建议本地再跑一遍 testssl.sh --severity HIGH example.com,它覆盖 100 多项检查(Heartbleed、ROBOT、降级攻击等),适合放进回归清单;但注意它会真实发起大量握手,生产环境请避开业务高峰。

上线之后:证书续期、多域名 SNI 与日常巡检怎么做

certbot 明明续期成功了,为什么浏览器里还是旧证书?这是上线后最高频的故障。Let’s Encrypt 证书有效期 90 天,据 Let’s Encrypt 官方文档,客户端应在剩余 30 天时续期;而 certbot 续期只把新证书写进 /etc/letsencrypt/live/ 下的文件,Apache 是在启动时把证书读入内存的,不 reload 就永远用旧的那一份。所以 --deploy-hook 不是可选项,签发那一刻就要带上。

# 1) 确认定时器已注册,并查看下次触发时间(certbot 包会自动安装 certbot.timer)
systemctl list-timers --all | grep -i certbot
# 典型输出:NEXT 列有具体时间即正常,若 UNIT 不存在需 systemctl enable --now certbot.timer

# 2) 用 --dry-run 演练续期(走 staging 环境,不消耗正式速率限制)
#    RHEL/CentOS 系服务名是 httpd,Debian/Ubuntu 系换成 apache2
sudo certbot renew --dry-run --deploy-hook "systemctl reload httpd"

# 3) 已签发的证书可把 hook 固化进续期配置,避免以后忘带
sudo sed -i '/^[renewalparams]/a renew_hook = systemctl reload httpd' 
  /etc/letsencrypt/renewal/example.com.conf
sudo certbot renew --dry-run   # 再次验证 hook 生效

怎么在告警系统里提前发现证书要过期?不要依赖 certbot 自身的日志,直接在外部探活,这才是端到端验证。openssl x509 -checkend 的秒数参数决定窗口,2592000 即 30 天;返回码 0 表示窗口内不会过期,1 表示即将过期或握手失败,可直接被 Zabbix/Prometheus textfile collector 消费。

#!/bin/bash
# /usr/local/bin/check_cert.sh  —— 退出码 0=正常,1=30 天内到期或连接失败
set -o pipefail
openssl s_client -connect example.com:443 -servername example.com /dev/null 
  | openssl x509 -noout -checkend 2592000
# 正常输出 "Certificate will not expire";异常输出 "Certificate will expire"
echo "exit=$?"

一台服务器挂多个域名,SNI 该怎么配?每个域名一个独立的 <VirtualHost *:443>,各自指定 SSLCertificateFile(用 fullchain.pem)与 SSLCertificateKeyFile,ServerName 必须与证书 SAN 中的条目逐字一致,否则客户端会报 certificate subject name does not match。据 RFC 6066,SNI 扩展由客户端在 ClientHello 中携带;不支持 SNI 的老客户端(如部分 Java 6、Windows XP 上的 IE)会落到配置文件中第一个 443 vhost,因此把主域名放在最前面,并用 httpd -S 复核实际加载顺序。

# /etc/httpd/conf.d/ssl-vhosts.conf —— 主域名必须写在最前,它是默认站点
<VirtualHost *:443>
    ServerName example.com                 # 必须与证书 SAN 完全一致
    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    # 显式收紧协议:httpd 2.4 的 SSLProtocol 默认 all,会带上 TLSv1.0/1.1
    SSLProtocol           -all +TLSv1.2 +TLSv1.3
    # 按协议版本与套件统计客户端分布,为是否关停 TLS 1.2 提供数据
    CustomLog logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x %r %b"
</VirtualHost>
# 语法检查后按配置加载顺序列出所有 vhost,确认第一个 *:443 是主域名
sudo apachectl configtest && sudo httpd -S | grep -A2 ':443'

有了 %{SSL_PROTOCOL}x 日志,就能用 awk '{print $3}' logs/ssl_request_log | sort | uniq -c 看还有多少客户端停留在 TLSv1.2,而不是凭感觉关协议。

巡检项 判定命令 健康判据 异常处置
续期定时器 systemctl list-timers | grep certbot NEXT 有具体时间 systemctl enable --now certbot.timer
证书剩余期 openssl x509 -checkend 2592000 退出码 0 查 /var/log/letsencrypt/letsencrypt.log
协议协商 openssl s_client -tls1_2 -connect ... 默认协商出 TLSv1.3 检查是否被 conf.d 覆盖 SSLProtocol
vhost 顺序 httpd -S 首个 443 是主域名 调整 Include 文件名前缀

版本要不要跟着升?要。Apache 2.4.60 修复了 CVE-2024-38472(Windows 平台 mod_rewrite 的 UNC 路径 SSRF)及一批 mod_proxy 相关问题,2.4.62 是 2024 年 7 月发布的稳定版本,据 Apache HTTP Server 官方安全公告,这些修复都只在对应版本及以上生效。日常跟随发行版安全更新即可,但每季度应用 testssl.sh --severity MEDIUM https://example.com 跑一次回归,确认协议与套件策略没有被某次 conf.d 变更悄悄放宽——配置漂移比漏洞更常见。

在 里至少 5 个:SSLEngine on、SSLCertificateFile(指向含中间证书的 fullchain 文件)、SSLCertificateKeyFile、SSLProtocol -all +TLSv1.2 +TLSv1.3、SSLCipherSuite。改完 apachectl configtest 通过后 systemctl reload httpd 即生效,不需要重启整台服务器。

Nginx 用 ssl_certificate 一条指令同时加载证书与链,Apache 2.4.8 之后也是把链合并进 SSLCertificateFile,但历史教程里的 SSLCertificateChainFile 仍大量存在,容易配重复。协议与套件方面 Nginx 用 ssl_protocols/ssl_ciphers,Apache 用 SSLProtocol/SSLCipherSuite,语义基本一一对应;OCSP 装订 Nginx 只需 ssl_stapling on,Apache 用 SSLUseStapling on 且必须额外配 SSLStaplingCache。

不需要。据 Apache 2.4 官方文档,SSLCertificateChainFile 自 2.4.8 起已废弃,中间证书直接拼接在 SSLCertificateFile 指向的文件里即可,顺序是服务器证书在前、中间证书在后。可以用 grep -c “BEGIN CERTIFICATE” fullchain.pem 校验,正常应输出 2。

多数情况是 https:// 打到了只监听 80 的端口,或 443 vhost 里漏写 SSLEngine on。先在服务器上执行 openssl s_client -connect 127.0.0.1:443 -servername example.com,能正常拿到证书说明服务端没问题,问题在 DNS 解析、CDN 回源或防火墙;拿不到就去看 ssl_error_log 里的 AH 编号对照处理。

用 SNI:每个域名一个独立 ,各自配 SSLCertificateFile 与 SSLCertificateKeyFile,ServerName 必须与证书 SAN 一致。注意第一个 443 vhost 是默认站点,不支持 SNI 的老客户端会落到它上面,建议把主域名放最前并用 httpd -S 复核。Apache 2.4.8+ 还支持在同一 vhost 内同时配置 RSA 与 ECDSA 双证书。

配置写法完全一样,差别在签发方式与有效期:Let's Encrypt 走 ACME 协议、90 天有效期,certbot –apache 一条命令自动改写 vhost 并处理续期;商业 DV/OV 证书是把 CSR 提交 CA,人工下载后手动放到 SSLCertificateFile。付费证书的价值主要在 OV/EV 的组织信息核验、赔付条款和中文技术支持,而不是加密强度。

改证书、协议、套件、响应头用 systemctl reload httpd,热加载不会断开现有连接;改 Listen 端口、新增 LoadModule、调整 MPM 参数必须 systemctl restart httpd,因为 reload 不会重新绑定监听端口。无论哪种,执行前都要先跑 apachectl configtest,避免带着语法错误重启导致整站不可用。


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

内容审核

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