跳到主要内容
Apache

Apache mod_ssl 配置 SSL 证书完整教程:2026 有效期新规与排错实战

问题背景与根因:为什么 Apache 上了证书仍报错

很多站点在 Apache 上启用 mod_ssl 后,浏览器仍提示 NET::ERR_CERT_AUTHORITY_INVALID,curl 返回 curl: (60) SSL certificate problem: unable to get local issuer certificate,或 Apache 直接启动失败并报 AH00526。表面是“证书没生效”,根因通常不是证书文件不存在,而是链、私钥、SNI、端口四类问题。第一,只把站点证书放进 SSLCertificateFile,没有把中间证书按“站点证书→中间证书”顺序合并,客户端无法补全信任链。第二,私钥与证书不匹配,Apache 日志出现 SSL_CTX_use_PrivateKey: key values mismatch。第三,多个域名共用 443 时 ServerName 与 SNI 不一致,触发 AH02032。第四,443 被 HTTP 服务或其他进程占用,浏览器报 SSL_ERROR_RX_RECORD_TOO_LONG。排查先跑 sudo apachectl -S 看虚拟主机映射,再跑 sudo apachectl configtest;用 openssl x509 -in /etc/ssl/certs/example.com.fullchain.pem -noout -subject -issuer -dates -ext subjectAltName 检查 CN/SAN 与有效期;用 openssl pkey -in /etc/ssl/private/example.com.key -pubout | openssl sha256 与 openssl x509 -in /etc/ssl/certs/example.com.fullchain.pem -pubkey -noout | openssl sha256 比对公钥哈希。还要注意 2026-03-15 起公共 TLS 证书最长有效期 200 天,旧文里的 398 天、825 天配置必须按新规更新,否则续期策略会失效。只有把根因定位到链、私钥、SNI、端口或有效期,后续配置才有意义。

2026 年公共 TLS 证书有效期新规与选型

CA/Browser Forum 已通过 SC-081:公共 TLS 证书最长有效期自 2026-03-15 起为 200 天,2027-03-15 起为 100 天,2029-03-15 起为 47 天。旧教程中的 398 天、825 天说法已经过时,不能继续作为签发或续期依据。Let’s Encrypt 免费证书固定 90 天,仍低于 200 天上限,适合用 ACME 自动续期;商业 DV 证书也必须遵守新上限,OV/EV 只是组织验证强度不同。EV 证书不再承诺在浏览器地址栏展示企业名称,浏览器早已取消该 UI,采购时不要把“地址栏显示公司名”写进验收标准。选型建议:个人站点和 API 用 Let’s Encrypt DV + certbot 自动续期;金融、政企对外门户可选 OV/EV 满足合规审计,但仍按 200/100/47 天节奏续期。检查现有证书:openssl x509 -in /etc/ssl/certs/example.com.fullchain.pem -noout -dates -subject -issuer;用 openssl x509 -in /etc/ssl/certs/example.com.fullchain.pem -noout -checkend 2592000 判断 30 天内是否到期。续期后必须重载 Apache:systemctl reload apache2 或 systemctl reload httpd。不要用“一年一签”的旧流程,建议把续期检查放进监控,提前 30 天告警。

完整实操:启用 mod_ssl、生成 CSR 与签发证书

先确认版本与模块。Debian/Ubuntu 执行:sudo apt update && sudo apt install -y apache2 openssl;sudo a2enmod ssl headers rewrite;sudo systemctl restart apache2;sudo apache2ctl -M | grep -E ‘ssl|headers|rewrite’。RHEL/CentOS/Rocky 执行:sudo dnf install -y mod_ssl openssl;sudo systemctl restart httpd;sudo httpd -M | grep ssl。生成私钥与 CSR(RSA 2048 位,兼容性最好):sudo openssl req -new -newkey rsa:2048 -sha256 -nodes -keyout /etc/ssl/private/example.com.key -out /etc/ssl/certs/example.com.csr -subj ‘/CN=example.com’ -addext ‘subjectAltName=DNS:example.com,DNS:www.example.com’。检查 CSR:openssl req -in /etc/ssl/certs/example.com.csr -noout -text | grep -A1 ‘Subject Alternative Name’。把 CSR 交给 CA 签发;若用 Let’s Encrypt,直接执行:sudo certbot certonly –webroot -w /var/www/html -d example.com -d www.example.com –deploy-hook ‘systemctl reload apache2’。签发后合并链,顺序必须是站点证书在前、中间证书在后,根证书不要放:sudo cat /etc/letsencrypt/live/example.com/cert.pem /etc/letsencrypt/live/example.com/chain.pem | sudo tee /etc/ssl/certs/example.com.fullchain.pem。设置权限:sudo chmod 600 /etc/ssl/private/example.com.key;sudo chown root:root /etc/ssl/private/example.com.key;SELinux 系统执行 sudo restorecon -Rv /etc/ssl。最后用 openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/example.com.fullchain.pem 验证链。

配置示例:Apache 2.4 虚拟主机与证书链写法

Apache 2.4.8+ 推荐把站点证书与中间证书合并进 SSLCertificateFile,SSLCertificateChainFile 已弃用,不再需要单独写。示例:
<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
Protocols h2 http/1.1
SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
SSLHonorCipherOrder off
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
ErrorLog ${APACHE_LOG_DIR}/example_ssl_error.log
CustomLog ${APACHE_LOG_DIR}/example_ssl_access.log combined
</VirtualHost>
如果仍使用分离写法,则保留 SSLCertificateFile 指向站点证书,SSLCertificateChainFile 指向中间证书;但新部署不建议。HSTS 第一次上线先用 max-age=300 观察,确认全站 HTTPS 后再改 31536000;includeSubDomains 要求所有子域都支持 HTTPS,preload 不要随意加。配置写好后执行 sudo apachectl configtest,输出 Syntax OK 再 sudo systemctl reload apache2。用 sudo apachectl -S 确认 443 虚拟主机绑定到正确 ServerName。若使用 certbot 的 apache 插件,它会自动生成 ssl 虚拟主机,但证书链和 HSTS 仍要人工复核。

HTTP 强制跳转 HTTPS、HSTS 与 OCSP Stapling

80 端口虚拟主机做 301 跳转,推荐用 Redirect 保持简单:<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
需要保留路径时用 Rewrite:RewriteEngine On;RewriteCond %{HTTPS} !=on;RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]。HSTS 响应头只在 HTTPS 虚拟主机里发:Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"。OCSP Stapling 可减少客户端额外查询,全局配置:SSLUseStapling on;SSLStaplingCache "shmcb:logs/ssl_stapling(32768)";SSLStaplingResponderTimeout 5;SSLStaplingReturnResponderErrors off。注意 Let’s Encrypt 新证书可能不再提供 OCSP URL,若 openssl x509 -in /etc/ssl/certs/example.com.fullchain.pem -noout -ocsp_uri 输出为空,则 Stapling 不会生效,这不影响 HTTPS。验证:echo | openssl s_client -connect example.com:443 -servername example.com -status 2>/dev/null | grep -A3 ‘OCSP Response Status’。HSTS 验证:curl -sI https://example.com | grep -i strict-transport-security。跳转验证:curl -I http://example.com 应返回 301 且 Location 为 https://example.com/。修改后 apachectl configtest 与 reload 不能省。

方案对比:证书链写法、证书类型与续期方式

下面表格把 Apache 配置与证书选型放在一起对比。链写法:单文件 SSLCertificateFile 适用于 Apache 2.4.8+,文件顺序为站点证书、中间证书,根证书不放;SSLCertificateChainFile 是旧写法,2.4.8+ 已弃用,混用容易导致链重复或顺序错误。证书类型:DV 只验证域名控制权,签发快;OV 验证组织身份;EV 验证更严,但浏览器不再承诺地址栏展示企业名,不要为“地址栏公司名”买 EV。有效期:Let’s Encrypt 90 天;公共 TLS 证书上限 2026-03-15 起 200 天,2027-03-15 起 100 天,2029-03-15 起 47 天;旧文 398 天/825 天必须废弃。续期:ACME 自动续期适合 90 天与未来更短周期;商业证书也要设置到期前 30 天告警。表格:

项目 推荐做法 不推荐/风险
证书链 fullchain.pem 单文件,站点证书在前 只配站点证书、根证书混入、顺序颠倒
证书类型 一般站点 DV;合规场景 OV/EV 把 EV 地址栏展示当承诺
有效期 90 天 ACME 或按 200/100/47 天续期 继续按 398/825 天排期
续期 certbot renew + deploy-hook reload 手工一年一换

表格后按业务选型即可。

常见错误排查:真实报错与修复命令

1)AH00526: Syntax error on line … SSLCertificateFile: file ‘/etc/ssl/example.com.pem’ does not exist or is empty。修复:ls -l 路径;确认 fullchain.pem 非空;sudo apachectl configtest。2)SSL_CTX_use_PrivateKey: key values mismatch。修复:比对公钥哈希,openssl pkey -in /etc/ssl/private/example.com.key -pubout | openssl sha256 与 openssl x509 -in /etc/ssl/certs/example.com.fullchain.pem -pubkey -noout | openssl sha256 必须一致;不一致重新签发。3)curl: (60) SSL certificate problem: unable to get local issuer certificate。修复:补中间证书,cat cert.pem chain.pem > fullchain.pem;不要放根证书。4)浏览器 SSL_ERROR_RX_RECORD_TOO_LONG。修复:443 端口被 HTTP 占用,ss -lntp | grep ‘:443’,检查 VirtualHost 是否 SSLEngine on,防火墙是否把 443 转到 80。5)AH02032: Hostname … provided via SNI and hostname … provided via HTTP are different。修复:ServerName 与证书 SAN 一致,多域名用 ServerAlias,确保 SNI 虚拟主机顺序正确。6)AH01909: RSA certificate configured for … does NOT include an ID which matches the server name。修复:证书 SAN 加入该域名,或调整 ServerName。7)Permission denied: … key。修复:chmod 600 key,chown root:root,SELinux 执行 restorecon -Rv /etc/ssl。每次改完都执行 apachectl configtest 和 systemctl reload。

效果验证:openssl、curl 与浏览器检查清单

配置重载后按顺序验证。第一步语法与映射:sudo apachectl configtest 应输出 Syntax OK;sudo apachectl -S 检查 443 虚拟主机、ServerName、证书路径。第二步端口:sudo ss -lntp | grep ‘:443’ 应看到 apache2/httpd 监听。第三步证书链:openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName,确认 CN/SAN 含 example.com 和 www.example.com,NotAfter 符合新有效期。第四步链完整性:openssl s_client -connect example.com:443 -servername example.com -verify_return_error </dev/null 应返回 Verify return code: 0 (ok)。第五步 HTTP 跳转:curl -I http://example.com 返回 301,Location: https://example.com/。第六步 HSTS:curl -sI https://example.com | grep -i strict-transport-security。第七步 TLS 版本:openssl s_client -connect example.com:443 -tls1_1 应失败,-tls1_2 和 -tls1_3 应成功。第八步浏览器:开发者工具 Security 面板看证书链、有效期、TLS 1.3;再用 SSL Labs 跑 A 级。若 curl 报证书错误而浏览器正常,检查系统 CA 包 ca-certificates 是否更新。最后把 certbot renew –dry-run 加入验收,确保自动续期可用。

常见问题

不需要。2.4.8+ 推荐把站点证书和中间证书按顺序合并到 SSLCertificateFile 指向的 fullchain.pem,SSLCertificateChainFile 已弃用。只有旧版本或旧配置才保留分离写法,且不要同时重复配置,避免链顺序错乱。

站点证书在前,中间证书在后,根证书不要放。可用 cat cert.pem chain.pem > fullchain.pem。顺序颠倒会导致 unable to get local issuer certificate;根证书混入会增大握手包且部分客户端不认。用 openssl verify 验证。

不冲突。90 天低于公共 TLS 证书最长有效期;2026-03-15 起上限 200 天,2027-03-15 起 100 天,2029-03-15 起 47 天。Let’s Encrypt 仍按 90 天签发,建议 certbot renew –dry-run 和自动 reload。

不要直接。先 max-age=300 验证全站 HTTPS,再逐步提高;includeSubDomains 要求所有子域支持 HTTPS;preload 一旦提交撤销慢。配置 Header always set Strict-Transport-Security 后,用 curl -sI 检查。

通常是 HTTPS 端口收到明文 HTTP。检查 443 是否被 HTTP VirtualHost 占用,ss -lntp | grep ‘:443’;确认 443 虚拟主机有 SSLEngine on;检查防火墙/负载均衡是否把 443 转发到后端 80。修正后 apachectl configtest 并 reload。

不能承诺。主流浏览器已取消 EV 地址栏企业名展示,EV 只代表更严格的组织验证,可能在证书详情中看到组织信息。采购时不要以地址栏展示为验收条件,按合规需求选择 OV/EV。

内容审核

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