composer怎么解决SSL证书过期导致的下载失败_系统证书更新【方案】

15次阅读

应更新系统CA证书包,因过期导致ssl验证失败;ubuntu/debian运行sudo apt install –reinstall ca-certificates,centos/RHEL用yum update ca-certificates,windows替换cacert.pem,docker需重建镜像或容器内更新。

composer怎么解决SSL证书过期导致的下载失败_系统证书更新【方案】

composer 下载失败报 “SSL certificate problem” 怎么办

直接原因是系统或 php 的 CA 证书包过期,导致 composer installcomposer create-project 连不上 packagist.org(它强制 httpS)。不是 Composer 本身坏了,也不是你网络被墙——是信任链断了。

先确认是不是证书问题

运行这条命令看错误是否匹配:

php -r "print_r(openssl_get_cert_locations());"

重点看 default_cert_file 路径是否存在、是否可读;再手动检查该文件最后修改时间。常见过期路径有:

  • /etc/ssl/certs/ca-certificates.crt(Debian/Ubuntu)
  • /etc/pki/tls/certs/ca-bundle.crt(CentOS/RHEL)
  • C:xamppphpextrassslcacert.pem(Windows XAMPP)

如果文件存在但最后更新是 2022 年以前,基本就是它。

更新系统级 CA 证书(推荐优先做)

别跳过这步——很多 PHP 环境(尤其 CLI)直接复用系统证书。不更新会导致后续所有 https 请求(包括 curlfile_get_contents)都可能出问题。

  • Ubuntu/Debian:sudo apt update && sudo apt install --reinstall ca-certificates
  • CentOS/RHEL 7+:sudo yum update ca-certificatessudo dnf update ca-certificates
  • macos(Homebrew):brew reinstall ca-certificates,然后确保 openssl@3 已链接
  • Windows:升级 git for Windows 或手动下载 https://www.php.cn/link/5fe4dadcdb001d8566cd20e6d8a20251 替换 PHP 的 cacert.pem

更新后重启终端,再执行 php -r "print_r(openssl_get_cert_locations());" 确认 default_cert_file 时间已刷新。

临时绕过或补救(仅限调试/紧急)

不建议长期使用,但能快速验证是不是证书问题:

  • 临时禁用 SSL 验证(⚠️危险,仅本地测试):composer config -g secure-http false,再删掉 ~/.composer/auth.json 中的 github-oauth(如有),避免混用 HTTP/HTTPS
  • 手动指定新版证书路径(更安全):composer config -g cafile /etc/ssl/certs/ca-certificates.crt(路径按 openssl_get_cert_locations() 输出为准)
  • PHP INI 强制指定(适合容器或共享环境):在 php.ini 加一行 openssl.cafile=/etc/ssl/certs/ca-certificates.crt,然后 php --ini 确认生效

注意:composer config -g cafile 只影响 Composer 自身请求,不影响 PHP 其他 HTTPS 调用;而 openssl.cafile 是全局生效的。

最常被忽略的是:Docker 容器里即使宿主机证书更新了,容器内仍用旧镜像里的证书包——得重建镜像或进容器手动更新。另外,某些老旧 PHP 版本(如 7.2)自带的 OpenSSL 不支持新根证书,必须升 PHP 或打补丁。

text=ZqhQzanResources