Windows 環境下 PHP 擴展安裝一直是開發者痛點:版本號(VC 編譯器、TS/NTS、位數)、依賴庫、加載順序稍有不匹配就報 Unable to load dynamic library。本文彙總官方下載站點、版本對應規則、常用擴展清單與安裝標準流程,助你一次成功。
| PHP 版本 | 站點 | 說明 |
|---|---|---|
| 5.2 / 5.3 | https://downloads.php.net/pierre/ | Pierre Joye 早期構建,含 php_oci8.dll、php_memcache.dll 等經典擴展 |
| 5.3 / 5.4 / 5.5 / 5.6 / 7.0 / 7.1 / 7.2 / 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3 | https://windows.php.net/downloads/pecl/releases/ | PECL 官方釋出版本庫,**首選站點**,每個擴展有獨立目錄與版本歷史 |
| 所有版本 | https://pecl.php.net/package/ | 擴展元數據頁,含 changelog、依賴、DLL 下載鏈接 |
下載頁面文件名格式:php_{ext}-{version}-{php_version}-{vc}-{ts|nts}-{x86|x64}.zip
| 字段 | 含義 | 如何確認 |
|---|---|---|
{php_version} | PHP 大版本 | php -v → PHP 8.2.12 → 下載 8.2 目錄 |
{vc} | Visual C++ 編譯器版本 | php -i | findstr "Compiler" → VC15/VS16 等 |
ts / nts | Thread Safe / Non-Thread Safe | php -i | findstr "Thread Safety" → enabled 選 ts,否則 nts |
x86 / x64 | 32 位 / 64 位 | php -i | findstr "Architecture" → x64 選 x64 |
示例:PHP 8.2.12 NTS x64 VC15 → 下載 php_redis-5.3.7-8.2-nts-vs16-x64.zip。
以下擴展在 https://windows.php.net/downloads/pecl/releases/{ext}/ 目錄下均有穩定版:
| 擴展 | 用途 | 依賴庫(需放入 PATH) |
|---|---|---|
redis | Redis 客戶端 | 無 |
memcached | Memcached 客戶端 | libmemcached.dll, libsasl.dll |
mongodb | MongoDB 官方驅動 | 無 |
amqp | RabbitMQ 客戶端 | rabbitmq.4.dll |
swoole | 高性能協程框架 | 無(含 OpenSSL) |
openswoole | Swoole 分支 | 無 |
xdebug | 調試/覆蓋率 | 無 |
imagick | ImageMagick 封裝 | ImageMagick 7.x DLLs |
gd | 圖形處理(內建) | 無 |
pdo_sqlsrv / sqlsrv | SQL Server 驅動 | ODBC Driver 17/18 |
pdo_oci / oci8 | Oracle 驅動 | Instant Client SDK |
ssh2 | SSH2 協議 | libssh2.dll |
yaml | YAML 解析 | libyaml.dll |
zip | ZIP 壓縮(內建) | 無 |
apcu | 用戶緩存 | 無 |
opcache | Opcode 緩存(內建) | 無 |
grpc | gRPC 客戶端 | grpc.dll |
protobuf | Protocol Buffers | 無 |
php -v、php -i | findstr "Thread Safety\|Compiler\|Architecture"https://windows.php.net/downloads/pecl/releases/redis/,選擇版本目錄(如 5.3.7),下載匹配的 zip。php_redis.dll 放入 PHP ext 目錄(C:\php\ext)。libmemcached.dll),放入 PHP 根目錄或系統 PATH 目錄。extension=redis(無需 .dll 後綴,PHP 7.2+ 自動識別)。systemctl restart php-fpm / IIS 回收應用程序池 / Apache 重啟。php -m | findstr redis 或 php -r "var_dump(extension_loaded('redis'));"| 錯誤信息 | 原因 | 修復 |
|---|---|---|
Unable to load dynamic library 'redis' | DLL 路徑錯、文件名錯、位數不匹配 | 確認 extension_dir、文件名、x86/x64 |
The specified module could not be found | 缺少依賴 DLL(如 libmemcached.dll) | 將依賴 DLL 放入 PHP 根目錄或 PATH |
Module compiled with module API=20210902, PHP compiled with module API=20220829 | PHP 小版本不匹配(如 8.1 載入 8.2 的 DLL) | 嚴格匹配 php_version 字段 |
Thread Safety mismatch | TS 載入 NTS 或反向 | 重新下載匹配 ts/nts 版本 |
VC runtime missing | 缺少 Visual C++ Redistributable | 安裝對應 VC 運行庫(VC15→VS2015-2022 Redist) |
若無法安裝 DLL(如共享主機、CI 環境),可用純 PHP 實現替代:
composer require symfony/polyfill-mbstring # 模擬 mbstring
composer require ramsey/uuid # 替代 uuid 擴展
composer require firebase/php-jwt # 替代 openssl 簽名功能
雖性能不如 C 擴展,但解決「無法安裝擴展」的部署受限問題。
Windows PHP 擴展安裝核心口訣:版本號四項全匹配、依賴庫進 PATH、php.ini 寫 extension 名、重啟驗證 php -m。收藏 https://windows.php.net/downloads/pecl/releases/ 這一個站點,覆蓋 99% 生產環境所需擴展。遇到疑難雜症,優先看 php -i 輸出的 Compiler/TS/Architecture,對照文件名逐項核對,必能定位根因。