跳到主要内容
Apache

JoySSL SSL 证书 Apache 安装部署实战:证书链、配置示例与排错验证

问题背景与根因:为什么 Apache 部署 JoySSL 证书会“浏览器正常、客户端报错”

JoySSL 是国产 CA,提供免费 DV 和付费证书,中文审核与本地化支持对国内用户友好。把 JoySSL 签发的 SSL 证书部署到 Apache 时,最常见的问题不是证书本身无效,而是证书链、私钥匹配、ServerName/SNI 和 Apache 版本差异。浏览器通常会自动补链或缓存中间证书,所以桌面浏览器显示小锁正常;但 curl、Java、微信小程序、Android 旧版本等客户端严格校验链,一旦缺少中间证书就会报 unable to get local issuer certificate 或 PKIX path building failed。

另一个根因是有效期规则变化。公开信任 TLS 证书单张最长有效期在 2020-09-01 至 2026-03-14 为 398 天;自 2026-03-15 起降为 200 天,2027-03-15 起 100 天,2029-03-15 起 47 天。Let’s Encrypt 默认仍为 90 天。JoySSL 证书以实际签发有效期为准,续期后必须重新部署并 reload Apache。Apache 2.4.8+ 推荐把站点证书与中间证书合并为 fullchain,2.4.8 以下才单独使用 SSLCertificateChainFile。理解这些差异,才能避免“配置看似正确、部分客户端却握手失败”。

部署前准备:证书文件、私钥与证书链核对

先从 JoySSL 控制台下载 Apache 格式证书,通常得到站点证书、中间证书和私钥。推荐把站点证书与中间证书合并为 example.com_fullchain.crt,顺序是站点证书在前、中间证书在后,不要放根证书。私钥保持 example.com.key。上传到服务器后统一放到 /etc/ssl/joyssl/,并收紧权限:

mkdir -p /etc/ssl/joyssl
install -m 644 example.com_fullchain.crt /etc/ssl/joyssl/example.com_fullchain.crt
install -m 600 example.com.key /etc/ssl/joyssl/example.com.key
chown root:root /etc/ssl/joyssl/example.com.key
openssl x509 -in /etc/ssl/joyssl/example.com_fullchain.crt -noout -subject -issuer -dates -ext subjectAltName
openssl x509 -in /etc/ssl/joyssl/example.com_fullchain.crt -noout -pubkey | openssl pkey -pubin -outform der | openssl md5
openssl pkey -in /etc/ssl/joyssl/example.com.key -pubout -outform der | openssl md5

两条公钥哈希必须一致;RSA 证书也可用 openssl x509 -noout -modulus -in ... | openssl md5 与 openssl rsa -noout -modulus -in ... | openssl md5 对比。若哈希不同,说明证书与私钥不匹配,不要继续配置。再用 openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt /etc/ssl/joyssl/example.com_fullchain.crt 检查链是否能落到系统根证书;输出 OK 才说明链完整。

Apache 完整实操步骤:启用模块、放置配置、语法检查与重载

Debian/Ubuntu 先启用 SSL、headers 模块和站点配置。把下面的 VirtualHost 写入 /etc/apache2/sites-available/example.com-ssl.conf,然后执行:

sudo a2enmod ssl headers
sudo a2ensite example.com-ssl
sudo apachectl configtest
sudo systemctl reload apache2
sudo ss -tlnp | grep :443

RHEL/CentOS/AlmaLinux 使用 httpd 与 mod_ssl,配置文件放到 /etc/httpd/conf.d/example.com-ssl.conf:

sudo dnf install -y mod_ssl
sudo httpd -t
sudo systemctl reload httpd
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload

configtest 或 httpd -t 必须输出 Syntax OK 再重载;若报 Syntax error on line ...,先修复配置,不要直接 restart。云服务器还要在安全组放行 443。多站点场景要确认每个 443 VirtualHost 都有独立 ServerName,否则 SNI 会命中错误证书。重载后不要只看浏览器,继续用 openssl 和 curl 验证链、到期时间与 HSTS。

配置示例:443 VirtualHost、HTTP 跳转、HSTS 与 TLS 参数

以下示例适用于 Apache 2.4.8+,SSLCertificateFile 直接指向 fullchain。若你的 Apache 低于 2.4.8,才取消 SSLCertificateChainFile 注释并指向单独中间链文件。TLS 1.3 需要 Apache 2.4.36+ 与 OpenSSL 1.1.1+;老环境可只保留 TLS 1.2。

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

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html
    SSLEngine on
    SSLCertificateFile /etc/ssl/joyssl/example.com_fullchain.crt
    SSLCertificateKeyFile /etc/ssl/joyssl/example.com.key
    # Apache 2.4.8 以下才需要:
    # SSLCertificateChainFile /etc/ssl/joyssl/example.com_chain.crt
    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.com_ssl_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_ssl_access.log combined
</VirtualHost>

HSTS 建议先短时间测试,再改为一年;确认所有子域都支持 HTTPS 后才加 includeSubDomains。不要在没有 HTTP 跳转、证书链未验证的情况下直接上 HSTS。若使用 CDN 或反向代理,源站与边缘节点都要部署 fullchain,否则客户端看到的链仍可能不完整。

对比表格:Apache 版本、证书文件与有效期规则

Apache 版本决定链文件写法,证书有效期决定续期频率。下表可直接对照排查。注意 SSLCertificateChainFile 在 2.4.8+ 已弃用,但部分旧配置仍能加载;新部署应统一用 fullchain,减少文件漏配。

场景 Apache 版本 证书文件配置 链文件配置
推荐新部署 2.4.8+ SSLCertificateFile 指向 fullchain 不需要 SSLCertificateChainFile
老环境 2.4.8 以下 SSLCertificateFile 指向站点证书 SSLCertificateChainFile 指向中间链
错误做法 任意 只配站点证书 缺少中间证书,Java/curl 易报错
时间/CA 公开信任证书最长有效期
2020-09-01 至 2026-03-14 398 天
2026-03-15 起 200 天
2027-03-15 起 100 天
2029-03-15 起 47 天
Let’s Encrypt 默认 90 天

JoySSL 证书以实际签发有效期为准。有效期缩短意味着自动化续期和 reload 更重要:只续期不重载,Apache 进程仍会使用旧证书;只重载不检查链,部分客户端仍会失败。

常见错误排查:真实报错、原因与修复

先看 Apache 错误日志:tail -n 50 /var/log/apache2/error.log 或 journalctl -u httpd -n 50。再按下面报错逐项处理。

  • curl: (60) SSL certificate problem: unable to get local issuer certificate:证书链不完整。检查是否误把站点证书当 fullchain;用 openssl s_client -showcerts 看是否返回中间证书,改为 fullchain 后 reload。
  • AH02565: Certificate and private key ... do not match:证书与私钥不匹配。用公钥哈希或 RSA modulus 对比,重新下载匹配的 key,权限设为 600。
  • AH01909: RSA certificate configured for ... does not include an ID which matches the server name:ServerName 或 SAN 不匹配。检查证书 subjectAltName,补 ServerAlias 或重新签发。
  • sslv3 alert handshake failure:协议或套件不兼容。启用 TLS 1.2/1.3,避免只开 TLS 1.0;老客户端需单独评估。
  • SSL_ERROR_RX_RECORD_TOO_LONG:443 端口返回了 HTTP 明文。检查 Listen 443、VirtualHost 是否加载、防火墙是否把 443 转到了 80。
  • java.security.cert.PKIX path building failed:Java truststore 缺根证书或服务端缺中间证书。先修 fullchain,再把对应根证书导入客户端 truststore。

若浏览器正常但小程序报错,优先查链,不要先怀疑证书被吊销。用 openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 对比服务端返回的证书数量,通常应为站点证书加至少一张中间证书。

效果验证:openssl、curl、HSTS 与证书链检查

重载后先做本机语法与监听检查,再从外部验证。以下命令可直接执行,Verify return code: 0 (ok) 表示链验证通过。

sudo apachectl configtest
sudo systemctl status apache2 --no-pager
curl -I https://example.com
curl -sI https://example.com | grep -i 'strict-transport-security'
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null | openssl x509 -noout -subject -issuer -dates
openssl s_client -connect example.com:443 -servername example.com </dev/null | grep 'Verify return code'
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null | grep -E 's:|i:'

期望结果:curl -I 返回 HTTP/2 200 或 HTTP/1.1 200;HSTS 头存在;notAfter 是新证书到期时间;Verify return code 为 0;s: 与 i: 能看到站点证书和中间证书。若 curl 报证书错误,而浏览器正常,继续用 openssl verify 检查 fullchain 是否能落到系统根证书。最后用不同网络和 Java/小程序客户端各测一次,避免只验证了本机缓存。

续期与自动化:reload、权限、SELinux 与到期监控

证书续期后,文件替换不等于生效。Apache 需要重新读取配置和证书,推荐先检查再平滑重载:

sudo apachectl configtest && sudo systemctl reload apache2
# RHEL/CentOS
sudo httpd -t && sudo systemctl reload httpd
sudo chmod 600 /etc/ssl/joyssl/example.com.key
sudo chown root:root /etc/ssl/joyssl/example.com.key
sudo restorecon -Rv /etc/ssl/joyssl

Let’s Encrypt 可用 certbot renew --deploy-hook 'systemctl reload apache2',其证书默认 90 天;JoySSL 证书按实际有效期续期。公开信任证书自 2026-03-15 起最长 200 天,续期窗口更短,建议到期前 30 天告警。监控命令:openssl x509 -in /etc/ssl/joyssl/example.com_fullchain.crt -noout -enddate,或从外部取:openssl s_client -connect example.com:443 -servername example.com </dev/null | openssl x509 -noout -enddate。若 reload 后仍显示旧日期,检查配置路径是否指向新文件、容器是否挂载新证书、负载均衡后端是否全部更新。

常见问题

Apache 2.4.8+ 把站点证书和中间证书合并为 fullchain,配置到 SSLCertificateFile;私钥配置到 SSLCertificateKeyFile。不要只配叶子证书,也不要把根证书放进 fullchain。旧版 Apache 才单独用 SSLCertificateChainFile 指向中间链。


不需要。2.4.8 起推荐把中间证书与站点证书合并进同一个 fullchain 文件,由 SSLCertificateFile 一次加载;SSLCertificateChainFile 已弃用。只有 2.4.8 以下老版本才必须单独配置链文件。


这是证书链不完整。用 openssl s_client -showcerts 检查是否返回中间证书;把 JoySSL 提供的中间证书与站点证书合并为 fullchain,重新加载 Apache。Java 客户端还需确认其 truststore 含对应根证书。


先确认配置指向的是新 fullchain 和 key,再执行 apachectl configtest 和 systemctl reload apache2(RHEL 为 httpd)。reload 不生效时 restart;多进程/容器要确认挂载文件已更新,并用 openssl s_client 复查到期时间。


398 天是 2020-09-01 至 2026-03-14 的旧上限;2026-03-15 起公开信任 TLS 证书单张最长 200 天,2027-03-15 起 100 天,2029-03-15 起 47 天。Let’s Encrypt 默认仍为 90 天。


私钥建议 chmod 600、chown root:root,仅 root 可读;不要放在 Web 根目录。SELinux 系统执行 restorecon -Rv /etc/ssl/joyssl。Apache 以 root 启动后降权读取,权限过松会带来泄露风险。

内容审核

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