Windows下安装Redis及php的redis拓展
封面图
•
2025-10-25
•
PHP
188
0
0
0
在 Windows 系统下安装 Redis 以及 PHP 的 Redis 扩展,可以按照以下步骤进行操作。由于 Redis 官方不支持 Windows,但可以通过 Microsoft 提供的移植版本(如 [MSOpenTech/redis](https://github.com/microsoftarchive/redis))来运行。 --- ## 一、安装 Redis(Windows 版本) ### 1. 下载 Redis for Windows 访问 [Redis GitHub 仓库](https://github.com/microsoftarchive/redis),选择适合的版本下载: - 推荐使用 [Redis-x64-3.2.100.zip](https://github.com/microsoftarchive/redis/releases/tag/v3.2.100) > 注意:某些版本可能不再维护,建议选择较新的版本。 ### 2. 解压到指定目录 例如:`C:\redis` ### 3. 配置 Redis(可选) 打开 `redis.windows.conf` 文件,根据需要修改配置(如绑定 IP、端口等)。 ### 4. 启动 Redis 服务 #### 方法一:使用命令行启动 进入 Redis 目录: ```bash cd C:\redis redis-server.exe redis.windows.conf ``` #### 方法二:注册为 Windows 服务(可选) 使用 [NSSM](https://nssm.cc/download) 工具将 Redis 注册为 Windows 服务。 --- ## 二、安装 PHP 的 Redis 扩展 ### 1. 获取 PHP 的 Redis 扩展 DLL 文件 #### 方法一:使用 PECL(推荐) 如果你已经安装了 PHP 并且有 `pecl` 命令,可以直接使用: ```bash pecl install redis ``` > 注意:这要求你有编译环境(如 Visual Studio),否则可能无法成功。 #### 方法二:手动下载 DLL 文件(适用于 Windows) 1. 访问 [PECL Redis 下载页面](https://pecl.php.net/package/redis) 2. 根据你的 PHP 版本和线程安全(TS/NTS)选择对应的 `.dll` 文件。 - 例如:`php_redis-5.3-vc14-ts.dll`(假设你用的是 PHP 5.3,VC14 编译器,线程安全) 3. 将下载的 `.dll` 文件复制到 PHP 的 `ext` 目录中: ``` C:\php\ext\php_redis.dll ``` ### 2. 修改 `php.ini` 文件 在 `php.ini` 中添加以下内容: ```ini extension=php_redis.dll ``` > 如果你使用的是多版本 PHP,请确保修改的是当前使用的 `php.ini`。 ### 3. 重启 Web 服务器(如 Apache 或 Nginx) 如果你使用的是 Apache,重启 Apache: ```bash httpd -k restart ``` 或者通过服务管理器重启 Apache/Nginx。 --- ## 三、验证 Redis 扩展是否安装成功 创建一个 `info.php` 文件,内容如下: ```php <?php phpinfo(); ?> ``` 在浏览器中访问该文件,查找 `redis` 模块是否存在。 --- ## 四、测试 Redis 连接(可选) 你可以编写一个简单的 PHP 脚本来测试 Redis 是否正常工作: ```php <?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to Redis server successful."; $redis->set("test", "Hello Redis"); echo "Value of 'test' is: " . $redis->get("test"); ?> ``` --- ## 五、常见问题排查 | 问题 | 解决方案 | |------|----------| | Redis 无法启动 | 检查 `redis.windows.conf` 配置,确保没有错误 | | PHP 报错 `extension=php_redis.dll` 不存在 | 确保 DLL 文件路径正确,且 PHP 版本匹配 | | Redis 无法连接 | 检查防火墙设置,确保 6379 端口开放 | --- ## 六、推荐工具 - **Redis Desktop Manager**:图形化管理 Redis 数据库([官网](https://github.com/uglide/RedisDesktopManager)) - **PHP Manager for IIS**:用于管理 PHP 扩展(如果使用 IIS) --- 如果你使用的是 WAMP、XAMPP 或其他集成环境,安装过程类似,只需找到对应 PHP 的 `php.ini` 和 `ext` 目录即可。 如需我提供特定 PHP 版本或 Redis 版本的详细安装步骤,请告诉我你的具体环境。
上一篇:
PHP中输出文件,怎么区别什么时候该用readfile() , fread(), file_get_contents(), fgets()?
下一篇:
redis删除单个key和多个key
标题录入,一次不能超过6条
返回顶部
留言
留言
评论