linux下Redis安装

安装redis

cd && wget http://download.redis.io/releases/redis-3.0.5.tar.gz && tar -zxvf redis-3.0.5.tar.gz && cd redis-3.0.5 && make && cd src && make install PREFIX=/usr/local/redis && cd .. && mkdir /usr/local/redis/etc && cp redis.conf /usr/local/redis/etc/redis.conf

默认情况,Redis不是在后台运行,需要把redis放在后台运行:

vi /usr/local/redis/etc/redis.conf
- 将daemonize的值改为yes
- 如果redis需要设密码,改:requirepass 【密码】

启动Redis:最好把这句加到系统启动文件中

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf #启动
/usr/local/redis/bin/redis-cli shutdown #停止

前两项批处理:

sed -i 's/^\(daemonize\).*/\1 yes/' /usr/local/redis/etc/redis.conf
echo '/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf' >> /etc/rc.d/rc.local

到这里Redis安装完成,

连接客户端:

/usr/local/redis/bin/redis-cli 
127.0.0.1:6379> ping      
PONG    #已经成功地安装Redis在机器上
127.0.0.1:6379> keys *             #列出所有键名 
(empty list or set)           
127.0.0.1:6379>keys flushdb       #清空

安装PHP扩展:

相关步骤:

cd                                                                                       
wget https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz 
tar -zxvf 2.2.4.tar.gz
cd phpredis-2.2.4
phpize                                                                         
./configure --with-php-config=/usr/local/php/bin/php-config             
make
make install

PHP7:

cd && wget -c https://github.com/phpredis/phpredis/archive/php7.zip && unzip php7.zip && cd phpredis-php7 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install

PHP7以下:

cd && wget https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz && tar zxvf 2.2.4.tar.gz && cd phpredis-2.2.4 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install

安装完成之后,出现下面的内容表示安装完成,需要配置PHP.ini:

+———————————————————–+

| ! ERROR !
|

| The test-suite requires that proc_open() is available. |

| Please check if you disabled it in php.ini.
|

+———————————————————–+

7,配置php支持

vi /usr/local/php/etc/php.ini

extension="redis.so"                                             
session.save_handler = redis     #原值:files
session.save_path = "tcp://127.0.0.1:6379"      #原值:/tmp,且未启用 
session.auto_start = 0  
sed -i '/Module Settings/i ; redis & scwc\n;;;;;;;;;;;;;;;;;;;\nextension=redis.so\n;;;;;;;;;;;;;;;;;;;' 
    /usr/local/php/etc/php.ini

    sed -i 's/^\(session.save_handler\).*/\1=redis/' /usr/local/php/etc/php.ini


    sed -i 's/^;\(session.save_path\).*/\1="tcp:\/\/127.0.0.1:6379"/' 
    /usr/local/php/etc/php.ini 

    sed -i 's/^\(session.auto_start\).*/\1=0/' /usr/local/php/etc/php.ini                                                                                   ``` 如果不想修改php.ini可这样 ini_set("session.save_handler","redis"); ini_set("session.save_path","tcp://127.0.0.1:6379″); 如果配置文件/etc/redis.conf里设置了连接密码requirepass保存session的时候会报错,save_path这样写 tcp://127.0.0.1:6379?auth=authpwd 即可。 有网友提到相对于文件存储的session,redis或者memcache存储session值会有并发一致性的问题,这块并没有细测过。 8,重启,直接lnmp restart 如果Redis没有起作用,php -m查看是否加载成功。 9,测试PHP读写:

vi /home/wwwroot/default/redis.php

```
<?php session_start(); $_SESSION['redis'] = "test redis". time(); echo "Redis session_id:\t\t\t".session_id()."<br>"; echo "刚存入的值:\t\t\t".$_SESSION['redis']."<br>"; $redis = new redis(); $redis->connect('127.0.0.1', 6379); echo "上一次的存入值:\t\t\t". $redis->get("PHPREDIS_SESSION:".session_id()); 

打开:http://192.168.2.122/redis.php 显示以下内容表示一切正常。

session_id:2ieemhttqhjt3mt46fb2sqbkr4

value:test redis

Redis:redis|s:10:”test redis”;

/usr/local/redis/bin/redis-cli

127.0.0.1:6379> keys *

1) “PHPREDIS_SESSION:2ieemhttqhjt3mt46fb2sqbkr4”

127.0.0.1:6379>

完整批处理

cd && wget http://download.redis.io/releases/redis-3.0.5.tar.gz 
&& tar -zxvf redis-3.0.5.tar.gz && cd redis-3.0.5 && make && cd src && make 
install PREFIX=/usr/local/redis && cd .. && mkdir /usr/local/redis/etc && cp 
redis.conf /usr/local/redis/etc/redis.conf && cd /usr/local/src && wget 
https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz && tar zxvf 
2.2.4.tar.gz && cd phpredis-2.2.4 && /usr/local/php/bin/phpize && ./configure 
--with-php-config=/usr/local/php/bin/php-config && make && make install && make 
test
vi /usr/local/php/etc/php.ini 
extension=redis.so          #WIN中是: extension=php_redis.dll
session.save_handler = redis     #原值:files
session.save_path = “tcp://127.0.0.1:6379#原值:/tmp,且未启用

你可能感兴趣的:(redis,linux)