Composer报错“SSL certificate problem: certificate has expired”的根本原因是其内置ca-bundle.crt过期,需通过composer config –global cafile指定外部最新证书路径(如~/.composer/ca-bundle.crt)来解决,Windows需注意路径格式与权限,CI/CD中推荐用COMPOSER_CAFILE环境变量动态设置。
Composer报错“SSL certificate problem: certificate has expired”
这是 Composer 在 HTTPS 请求时校验证书失败的典型错误,根本原因不是系统证书库问题,而是 Composer 自带的 ca-bundle.crt 文件过期了——它硬编码在 composer.phar 里,不随系统更新,也不自动轮换。
如何定位并替换本地 ca-bundle.crt
Composer 会优先使用自身内置证书包;只有当配置了 openssl.cafile 或设置了环境变量 COMPOSER_CAFILE,才会跳过内置包。所以不能只靠更新系统 CA(比如 update-ca-certificates),必须显式干预。
- 运行
composer config --global cafile查看当前是否已设置自定义证书路径;若输出为空,说明正使用内置包 - 下载最新 Mozilla CA 包:
curl -sS https://curl.se/ca/cacert.pem -o ~/.composer/ca-bundle.crt - 全局启用它:
composer config --global cafile ~/.composer/ca-bundle.crt - 验证是否生效:执行
composer diagnose,确认输出中 “CA file” 行指向你刚保存的路径
Windows 下需额外注意路径和权限
Windows 用户容易卡在路径解析或文件权限上。Composer 默认读取 %APPDATA%\Composer\ 目录,但 cafile 配置里的路径必须是正斜杠或双反斜杠,且不能含中文或空格。