php使用redis存储
一、Redis扩展模块
# wget https://codeload.github.com/phpredis/phpredis/zip/develop -O phpredis.zip
# unzip phpredis.zip
# cd phpredis-develop/
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make
# make install
二、配置php.ini添加redis.so
# vim /usr/local/php/etc/php.ini
加入:
extension = redis.so
检测是否配置成功:
# /usr/local/php/bin/php -m | grep redis
如果出现redis说明配置成功
三、Redis实现session共享
在lamp/lnmp环境下实现的通用配置
编辑php.ini添加下面两行
session.save_handler = "redis"
session.save_path = "tcp://191.168.1.203:6379"
或者编辑httpd.conf中对应的虚拟主机添加:
php_value session.save_handler "redis"
php_value session.save_path " tcp://191.168.1.203:6379"
或者php-fpm.conf对应的pool中添加
php_value[session.save_handler] = redis
php_value[session.save_path] = " tcp://191.168.1.203:6379"
四、Redis存储session测试
http://www.test.com/session.php
1462905233
1462905648
r0nrs2en0oor7nbsr1tkoqeo30
[root@server2 etc]# redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:r0nrs2en0oor7nbsr1tkoqeo30"
127.0.0.1:6379> get PHPREDIS_SESSION:r0nrs2en0oor7nbsr1tkoqeo30
"TEST|i:1462905233;TEST3|i:1462905648;"
五、php连接redis集群
如果想用php连接redis cluster, 需要使用predis扩展
安装方法类似phpredis, predis扩展地址https://github.com/nrk/predis