Windows 10下Chocolatey安装的PHP 7.4加载Redis、MongoDB扩展报错如何解决
我在Windows 10电脑上通过Chocolatey下载安装了PHP运行环境。我尝试安装并启用PHP Redis扩展,流程为下载对应版本的扩展压缩包,将php_redis.dll扩展文件复制到默认ext文件夹,并按照如下方式修改了php.ini文件:
;;;;;;;;;;;;;;;;;;;;;; ; Dynamic Extensions ; ;;;;;;;;;;;;;;;;;;;;;; ; If you wish to have an extension loaded automatically, use the following ; syntax: ; ; extension=modulename ; ; For example: ; ; extension=mysqli ; ; When the extension library to load is not located in the default extension ; directory, You may specify an absolute path to the library file: ; ; extension=/path/to/extension/mysqli.so ; ; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and ; 'extension=php_<ext>.dll') is supported for legacy reasons and may be ; deprecated in a future PHP major version. So, when it is possible, please ; move to the new ('extension=<ext>) syntax. ; ; Notes for Windows environments : ; ; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+) ; extension folders as well as the separate PECL DLL download (PHP 5+). ; Be sure to appropriately set the extension_dir directive. ; extension=bz2 extension=curl extension=ffi extension=ftp extension=fileinfo extension=gd2 extension=gettext extension=gmp extension=intl extension=imap extension=ldap extension=mbstring extension=exif ; Must be after mbstring as it depends on it extension=mysqli ;extension=oci8_12c ; Use with Oracle Database 12c Instant Client extension=odbc extension=openssl ;extension=pdo_firebird extension=pdo_mysql ;extension=pdo_oci extension=pdo_odbc extension=pdo_pgsql extension=pdo_sqlite extension=pgsql extension=shmop ; The MIBS data available in the PHP distribution must be installed. ;extension=snmp extension=soap extension=sockets extension=sodium extension=sqlite3 extension=tidy extension=xmlrpc extension=xsl ; Added by Rumon: extension=mongodb extension=redis
我使用完全相同的流程安装了mongodb扩展。
现在,当我执行任意与上述扩展相关的操作时,系统都会报错提示无法找到对应的扩展。例如执行php --ini命令的输出如下:
$ php --ini PHP Warning: PHP Startup: Unable to load dynamic library 'mongodb' (tried: ext\mongodb (The specified module could not be found.), ext\php_mongodb.dll (The specified module could not be found.)) in Unknown on line 0 PHP Warning: PHP Startup: Unable to load dynamic library 'redis' (tried: ext\redis (The specified module could not be found.), ext\php_redis.dll (The specified module could not be found.)) in Unknown on line 0 Configuration File (php.ini) Path: Loaded Configuration File: C:\tools\php74\php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none)
我已提供扩展目录的截图辅助排查,请问该问题的诱因是什么?以及要如何解决该问题?
排查与解决步骤
按照优先级从高到低依次检查即可:
确认PHP与扩展的构建属性完全匹配
手动执行以下命令获取当前PHP的核心构建参数:php -i | findstr /C:"Architecture" /C:"Thread Safety" /C:"Compiler" /C:"PHP Version"你下载的扩展文件名包含
7.4-ts-vc15-x64标识,对应要求是:PHP版本为7.4、开启线程安全(TS)、编译器为VC15、架构为x64,任意一项不匹配都会导致扩展加载失败。如果参数不一致,需要重新下载和当前PHP完全匹配的扩展版本。修正
extension_dir配置
从报错信息可知当前加载的php.ini路径为C:\tools\php74\php.ini,打开该文件检查extension_dir配置项:- 确保该行没有被注释(前面没有分号)
- 不要使用相对路径,直接指定绝对路径:
extension_dir = "C:/tools/php74/ext"
确认你放置php_redis.dll、php_mongodb.dll的目录和该配置指向的目录完全一致。
安装对应版本的VC运行库
VC15编译的扩展依赖Visual C++ 2017 Redistributable(x64版本),如果系统没有安装该运行库,即使路径、版本完全匹配也会加载失败。安装完成后重启命令行重试即可。排除文件损坏问题
重新下载对应版本的Redis、MongoDB扩展压缩包,解压后直接提取根目录下的php_*.dll文件覆盖到ext目录(不要把解压出来的整个文件夹放进去),排除压缩包损坏、文件放错子目录的问题。临时测试扩展语法
虽然PHP 7.4支持extension=redis的简写语法,但可以临时改成旧的显式写法测试:extension=php_redis.dll extension=php_mongodb.dll修改后重启命令行执行
php -m,如果能看到redis、mongodb字样就说明加载成功。
内容的提问来源于stack exchange,提问作者msrumon

