在网上找了许多教程, 在Windows下都无法安装 PHP redis 扩展,自己搞了两个小时才终于找到下载的地址,现归纳总结如下。
PHP的(Thread Safe与Non Thread Safe)
在安装xdebug到时候你会有有TS和NTS版本的选择,在以前还有VC6和VC9的版本。如果你没有根据你目前的服务器的状况选择对应的版本的话,那么xdebug是安装不成功的。
一、如何选择 php5.3 的 VC9 版本和 VC6 版本
VC6 版本是使用 Visual Studio 6 编译器编译的,如果你的 PHP 是用 Apache 来架设的,那你就选择 VC6 版本。
VC9 版本是使用 Visual Studio 2008 编译器编译的,如果你的 PHP 是用 IIS 来架设的,那你就选择 VC9 版本。
二、如何选择 PHP5.3 的 Thread Safe 和 Non Thread Safe 版本
先从字面意思上理解,Thread Safe 是线程安全,执行时会进行线程(Thread)安全检查,以防止有新要求就启动新线程的 CGI 执行方式而耗尽系统资源。Non Thread Safe 是非线程安全,在执行时不进行线程(Thread)安全检查。
再来看 PHP 的两种执行方式:ISAPI 和 FastCGI。
ISAPI 执行方式是以 DLL 动态库的形式使用,可以在被用户请求后执行,在处理完一个用户请求后不会马上消失,所以需要进行线程安全检查,这样来提高程序的执行效率,所以如果是以 ISAPI 来执行 PHP,建议选择 Thread Safe 版本;
而 FastCGI 执行方式是以单一线程来执行操作,所以不需要进行线程的安全检查,除去线程安全检查的防护反而可以提高执行效率,所以,如果是以 FastCGI 来执行 PHP,建议选择 Non Thread Safe 版本。
这样就可以根据自己的使用情况选择所需的php版本下载了。
注:ISAPI 和 FastCGI无需在php中设置,是webserver的操作。
通过phpinfo();查看当前php是什么版本,Thread Safety,这个参数是查看是否是线程安全。
我的PHP是5.2的运行在apache上,通过phpinfo发现thread safety是enabled的,所以我选择了VC6非线程安全的版本安装。在windows下的php.ini增加以下配置即可
[XDebug]
zend_extension_ts="D:/Program Files/php-5.2.13-Win32/ext/php_xdebug-2.2.3-5.2-vc9.dll"
;是否开启自动跟踪
xdebug.auto_trace = On
;是否开启异常跟踪
xdebug.show_exception_trace = On
;是否开启远程调试自动启动
xdebug.remote_autostart = On
;是否开启远程调试
xdebug.remote_enable = On
;允许调试的客户端IP
xdebug.remote_host=127.0.0.1
;远程调试的端口(默认9000)
xdebug.remote_port=9000
;调试插件dbgp
xdebug.remote_handler=dbgp
;是否收集变量
xdebug.collect_vars = On
;是否收集返回值
xdebug.collect_return = On
;是否收集参数
xdebug.collect_params = On
;跟踪输出路径
xdebug.trace_output_dir="d:\xdebug\trace_output_dir"
;是否开启调试内容
xdebug.profiler_enable=On
;调试输出路径
xdebug.profiler_output_dir="d:\xdebug\profiler_output_dir"
1.查看自己的PHP版本
echo phpinfo();
PHP 版本信息:
PHP logo
PHP Version 5.3.29
Compiler MSVC9 (Visual C++ 2008)
Architecture x86
Zend Extension Build API220090626,NTS,VC9
PHP Extension Build API20090626,NTS,VC9
2.根据PHP版本号,编译器版本号和CPU架构,
这里的PHP版本为5.6,X86,VC11 编译的,所以,选下面的扩展版本:
选择php_redis-2.2.7-5.3-nts-vc9-x86.zip和php_igbinary-1.2.1-5.3-nts-vc9-x86.zip
下载地址:
可以根据这两个链接来查找对应的版本:
redis : https://windows.php.net/downloads/pecl/releases/redis/2.2.7/
igbinary: https://windows.php.net/downloads/pecl/releases/igbinary/1.2.1/
3.解压缩后,将php_redis.dll和php_igbinary.dll拷贝至php的ext目录下
4.修改php.ini,在该文件中加入:
; php_redis
extension=php_igbinary.dll
extension=php_redis.dll
注意:extension=php_igbinary.dll一定要放在extension=php_redis.dll的前面,否则此扩展不会生效
连接到 redis 服务
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //查看服务是否运行 echo "Server is running: " . $redis->ping(); ?>
执行脚本,输出结果为:
Connection to server sucessfully Server is running: PONG
Redis PHP String(字符串) 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //设置 redis 字符串数据 $redis->set("tutorial-name", "Redis tutorial"); // 获取存储的数据并输出 echo "Stored string in redis:: " . $redis->get("tutorial-name"); ?>
执行脚本,输出结果为:
Connection to server sucessfully Stored string in redis:: Redis tutorial
Redis PHP List(列表) 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //存储数据到列表中 $redis->lpush("tutorial-list", "Redis"); $redis->lpush("tutorial-list", "Mongodb"); $redis->lpush("tutorial-list", "Mysql"); // 获取存储的数据并输出 $arList = $redis->lrange("tutorial-list", 0 ,5); echo "Stored string in redis"; print_r($arList); ?>
执行脚本,输出结果为:
Connection to server sucessfully Stored string in redis Redis Mongodb Mysql
Redis PHP Keys 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; // 获取数据并输出 $arList = $redis->keys("*"); echo "Stored keys in redis:: "; print_r($arList); ?>
执行脚本,输出结果为:
Connection to server sucessfully Stored string in redis:: tutorial-name tutorial-list