跳到主要内容
Apache

Apache mod_ssl 配置 SSL 证书实战:虚拟主机、证书链与排错

问题背景与根因:为什么 Apache 上 HTTPS 经常配置失败

Apache 通过 mod_ssl 提供 TLS 终止,配置本身不复杂,但生产环境故障往往不是“证书没买对”,而是模块、虚拟主机匹配、证书链和文件权限四类问题叠加。最常见根因是:只部署了站点证书,没有把中间证书合并进 fullchain,导致浏览器或 curl 报 unable to get local issuer certificate;SSLCertificateFile 与 SSLCertificateKeyFile 不匹配,Apache 启动时报 key values mismatch;443 虚拟主机没有正确匹配 ServerName/SNI,返回默认站点证书;私钥权限过宽或 SELinux 上下文错误,httpd 无法读取;以及沿用旧教程的 SSLCertificateChainFile,在 Apache 2.4.8+ 上产生弃用告警或重复链。

另一个过时点是证书有效期。CA/Browser Forum 自 2020-09-01 起要求公开信任 TLS 证书最长有效期 398 天,Let’s Encrypt 仍为 90 天。继续按“多年期证书”写教程会误导读者。Apache 2.4.8 起 SSLCertificateFile 可以同时承载站点证书和中间证书,SSLCertificateChainFile 已不推荐;TLS 1.3 需要 Apache 2.4.36+ 与 OpenSSL 1.1.1+。先确认版本,再决定配置写法。

排查顺序建议固定为:apachectl configtest 看语法 → apachectl -S 看虚拟主机和 SNI → openssl s_client 看链 → 查 error_log。不要一上来改 CipherSuite。下面从模块启用开始给出可直接执行的步骤。

前置准备:启用 mod_ssl 并检查 Apache 版本

Debian/Ubuntu 使用 a2enmod,RHEL/CentOS/Rocky 使用 mod_ssl 包。先安装并启用模块:

# Debian/Ubuntu
sudo apt update
sudo apt install -y apache2 openssl
sudo a2enmod ssl headers rewrite
sudo systemctl restart apache2
apache2ctl -M | grep -E 'ssl|headers|rewrite'

# RHEL/CentOS/Rocky/AlmaLinux
sudo dnf install -y httpd mod_ssl openssl
sudo systemctl enable --now httpd
httpd -M | grep -E 'ssl|headers|rewrite'

输出中应出现 ssl_module、headers_module、rewrite_module。若没有 ssl_module,说明包未安装或模块未加载。检查版本:

apache2 -v   # 或 httpd -v
openssl version
apache2ctl -S   # 或 httpd -S

Apache 2.4.8 是证书链配置分界线;2.4.36+ 且 OpenSSL 1.1.1+ 才支持 TLS 1.3。确认 443 未被占用:

sudo ss -lntp | grep ':443'
sudo ufw allow 'Apache Full'        # Ubuntu
sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reload

SELinux 开启时,证书放在 /etc/ssl 通常没问题,放在自定义目录需设置上下文:

getenforce
sudo semanage fcontext -a -t cert_t '/opt/certs(/.*)?'
sudo restorecon -Rv /opt/certs

私钥权限应为 600,属主 root。若 httpd 以非 root 用户读取,需要确保组权限和目录可执行权限。完成后再进入证书文件准备。

证书申请与文件规范:CSR、fullchain 与有效期

商业 CA 通常要求先生成 CSR。以下命令生成 2048 位 RSA 私钥和带 SAN 的 CSR,OpenSSL 1.1.1+ 支持 -addext:

openssl req -new -newkey rsa:2048 -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'

Let’s Encrypt 可直接用 certbot 签发并自动改 Apache 配置,证书有效期 90 天:

sudo certbot --apache -d example.com -d www.example.com
sudo certbot renew --dry-run
systemctl list-timers | grep certbot

拿到证书后统一命名:站点证书 example.com.pem,中间证书 intermediate.crt,私钥 example.com.key。Apache 2.4.8+ 推荐合并为 fullchain:

cat example.com.pem intermediate.crt > example.com-fullchain.pem
sudo install -m 644 example.com-fullchain.pem /etc/ssl/certs/
sudo install -m 600 example.com.key /etc/ssl/private/

顺序必须是“站点证书在前、中间证书在后”,不要放根证书。验证链和私钥匹配:

openssl x509 -noout -subject -issuer -dates -in example.com-fullchain.pem
openssl x509 -noout -modulus -in example.com.pem | openssl md5
openssl rsa  -noout -modulus -in example.com.key | openssl md5

两个 md5 必须一致。证书最长有效期按 398 天规划,Let’s Encrypt 90 天必须自动续期;不要把续期脚本只写成手动提醒。

完整实操:编写 443 虚拟主机与 80 跳转配置

下面以 Debian 路径为例,RHEL 把 sites-available 换成 /etc/httpd/conf.d/ssl.conf 即可。先写 80 跳转:

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

再写 443 虚拟主机。注意 SSLStaplingCache 是服务器级指令,应放在主配置或 conf.d 全局段,不能放在 VirtualHost 内:

# 全局段(如 /etc/apache2/conf-enabled/ssl-stapling.conf)
SSLStaplingCache 'shmcb:logs/ssl_stapling(32768)'

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

    SSLEngine on
    SSLCertificateFile      /etc/ssl/certs/example.com-fullchain.pem
    SSLCertificateKeyFile   /etc/ssl/private/example.com.key
    # Apache < 2.4.8 才需要下面一行
    # SSLCertificateChainFile /etc/ssl/certs/intermediate.crt

    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'
    SSLHonorCipherOrder off
    SSLUseStapling on

    Header always set Strict-Transport-Security 'max-age=31536000; includeSubDomains'
    Header always set X-Content-Type-Options 'nosniff'
    Header always set X-Frame-Options 'SAMEORIGIN'
    Header always set Referrer-Policy 'strict-origin-when-cross-origin'

    <Directory /var/www/html>
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

启用站点并检查:

sudo a2ensite example-ssl.conf
sudo apachectl configtest
sudo systemctl reload apache2

configtest 必须输出 Syntax OK。若先返回 AH00526,不要 reload,按下一节排查。HSTS 建议先用 max-age=300 测试,确认全站 HTTPS 后再改 31536000;includeSubDomains 要确保所有子域都有 HTTPS。

证书链与版本差异:SSLCertificateChainFile 还要不要

证书链问题占 Apache HTTPS 故障的多数。浏览器信任根证书,但服务器必须发送“站点证书 + 中间证书”,否则部分客户端无法构建到根的路径。Apache 2.4.8 之前,SSLCertificateFile 只放站点证书,中间证书必须用 SSLCertificateChainFile 指定;2.4.8 及以后,SSLCertificateFile 可以直接指向 fullchain,SSLCertificateChainFile 已废弃。混用两种写法可能把中间证书重复发送,也可能因为路径写错导致链缺失。

场景 SSLCertificateFile SSLCertificateChainFile 说明
Apache < 2.4.8 站点证书 中间证书 必须分开配置
Apache ≥ 2.4.8 fullchain(站点+中间) 不配置 推荐写法
Apache ≥ 2.4.8 仍用旧写法 站点证书 中间证书 可运行但已弃用
证书链顺序错误 中间证书在前 — 部分客户端报 authority invalid

检查链是否完整:

openssl crl2pkcs7 -nocrl -certfile /etc/ssl/certs/example.com-fullchain.pem | openssl pkcs7 -print_certs -noout
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null

输出应包含站点证书和至少一张中间证书,且 verify return code 为 0。若为 21 (unable to verify the first certificate),就是链不完整。Let’s Encrypt 的 fullchain.pem 已包含中间证书,直接用即可。商业证书要确认 CA 提供的是中间证书而不是根证书。

常见错误排查:从 AH02572 到证书链不完整

先看 apachectl configtest 和 error_log,再看客户端报错。以下为真实高频报错:

报错 根因 处理
AH00526: Syntax error … SSLCertificateFile: file does not exist or is empty 路径错、权限不足、SELinux 拒绝 ls -l 检查;私钥 600;restorecon
AH02572: Failed to configure at least one certificate and key for example.com:443 证书或私钥缺失、不匹配 用 modulus 对比;确认 VirtualHost 内两条指令
SSL_CTX_use_PrivateKey: key values mismatch 证书与私钥不是一对 重新配对,勿混用旧 key
ssl_error_rx_record_too_long 443 端口跑的是 HTTP,或 SSLEngine 未开 检查 Listen 443、VirtualHost、反代
NET::ERR_CERT_AUTHORITY_INVALID / curl: (60) unable to get local issuer certificate 中间证书缺失或顺序错 改用 fullchain,站点证书在前
AH02217: ssl_stapling_init_cert: can’t retrieve issuer certificate! OCSP Stapling 找不到签发者 补全链或暂时 SSLUseStapling off
AH02032: Hostname … provided via SNI and hostname … are different SNI 与 Host 头不一致,默认站点错配 对齐 ServerName,设置默认 443 站点

若 systemctl 只报 Job for httpd.service failed,执行:

sudo journalctl -xeu apache2 --no-pager | tail -50
sudo tail -100 /var/log/apache2/error.log
sudo apachectl -S

端口冲突用 ss -lntp 查。权限问题不要用 chmod 777,私钥应 600,证书 644。所有修改后先 configtest 再 reload,避免直接 restart 放大故障。

效果验证与安全加固:openssl、curl 与 SSL Labs

配置 reload 后从服务器本机和外部网络分别验证。先看证书主题、签发者、有效期:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates
curl -I https://example.com
curl -vI https://example.com 2>&1 | grep -E 'SSL certificate verify|subject:|issuer:'

验证 TLS 版本与链:

openssl s_client -connect example.com:443 -servername example.com -tls1_2 </dev/null
openssl s_client -connect example.com:443 -servername example.com -tls1_3 </dev/null
openssl s_client -connect example.com:443 -servername example.com -status </dev/null 2>&1 | grep -A2 'OCSP Response Status'

应看到 Verify return code: 0 (ok),TLS 1.2/1.3 握手成功,OCSP Response Status: successful。再检查 HSTS 和跳转:

curl -sI https://example.com | grep -i strict-transport-security
curl -sI http://example.com | grep -i -E 'HTTP/|location'

HSTS 应返回 max-age=31536000,80 端口应 301 到 https。最后用 SSL Labs 或 testssl.sh 评分:

docker run --rm -it drwetter/testssl.sh https://example.com

目标为 A/A+,无 TLS 1.0/1.1,无 RC4/3DES,证书链完整。若使用 CDN 或反向代理,确认后端到源站也启用 TLS,避免“前端 HTTPS、回源 HTTP”的混合架构。证书到期监控建议 30/14/7 天告警,Let’s Encrypt 续期后 reload Apache。

常见问题

不需要。2.4.8+ 将站点证书与中间证书按顺序合并到 fullchain.pem,直接指向 SSLCertificateFile 即可;继续配置 ChainFile 属于弃用写法,可能重复发送中间证书。


Let’s Encrypt 用 certbot renew 自动续期,建议 systemctl list-timers 确认定时器,续期后执行 systemctl reload apache2;商业证书按 398 天提前 30 天更换,不要等过期。


通常是证书链不完整或顺序错误。把站点证书和中间证书合并为 fullchain,站点证书在前,再用 openssl s_client -showcerts 验证;不要放根证书。


私钥建议 600、属主 root,证书 644,所在目录 755。SELinux 开启时用 restorecon 恢复 cert_t 上下文,不要用 chmod 777。


分别对证书和私钥取 modulus 的 md5:openssl x509 -noout -modulus -in cert.pem | openssl md5 与 openssl rsa -noout -modulus -in key.pem | openssl md5,两个值一致才匹配。

内容审核

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