Windows系统下Composer openssl扩展缺失错误如何解决?
composer install的SSL/TLS及扩展加载问题 问题场景
运行composer install时出现错误:
"The openssl extension is required for SSL/TLS protection but is not available. If you can not enable the openssl extension, you can disable this error, at your own risk, by setting the 'disable-tls' option to true."
尝试过取消php.ini中;extension=openssl的注释、重启Apache,但无效;执行php --ini显示未加载任何配置文件:
Configuration File (php.ini) Path: Loaded Configuration File: (none) Scan for additional .ini files in: (none) Additional .ini files parsed: (none)
设置composer config -g disable-tls true后,又出现新错误:
"You are running Composer with SSL/TLS protection disabled. Composer is operating significantly slower than normal because you do not have the PHP curl extension enabled. No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information."
解决方案
1. 让命令行PHP加载正确的php.ini
- 找到PHP安装目录(比如
D:\php),将目录下的php.ini-development或php.ini-production重命名为php.ini - 将PHP安装目录添加到系统环境变量PATH(右键此电脑→属性→高级系统设置→环境变量→系统变量→PATH→编辑→添加PHP路径)
- 重启命令行窗口(必须重启,否则环境变量不生效),执行
php --ini,确认Loaded Configuration File显示正确的php.ini路径
2. 启用openssl和curl扩展
- 打开上述正确路径的php.ini文件:
- 找到
;extension=openssl(部分版本是;extension=php_openssl.dll),去掉开头的分号; - 找到
;extension=curl(部分版本是;extension=php_curl.dll),同样去掉分号 - 检查
extension_dir配置,确保指向PHP的ext文件夹,比如extension_dir = "ext"(相对路径)或extension_dir = "D:\php\ext"(绝对路径)
- 找到
- 确认PHP的ext文件夹中存在
php_openssl.dll和php_curl.dll文件,若缺失需重新下载对应版本的PHP包
3. 验证扩展是否生效
执行php -m,查看输出列表中是否包含openssl和curl,确认扩展已启用
4. 恢复Composer的TLS安全设置
执行命令:
composer config -g disable-tls false
5. 重新运行Composer
执行composer install,此时应该能正常加载依赖
内容的提问来源于stack exchange,提问作者Aya Hosny

