Redis安装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis安装相关的知识,希望对你有一定的参考价值。

Redis安装

1,安装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

批处理:
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

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

vi /usr/local/redis/etc/redis.conf
将daemonize的值改为yes

2.1 如果redis需要设密码,改:

requirepass 【密码】

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

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

前两项批处理:
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安装完成,

4,连接客户端:

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

127.0.0.1:6379> ping        ---这说明已经成功地安装Redis在机器上
PONG
127.0.0.1:6379> keys *                     --列出所有键名
(empty list or set)                            --若出现这个,表示安装正常。
127.0.0.1:6379>keys flushdb         --清空,好象不起作用

5,停止(只是说明如何停止,这里不必须停止):

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

6,安装php扩展:

相关步骤:
cd /usr/local/src                                                                                         #进入软件包存放目录
wget https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz           #下载安装包,或都其他方式下载并上传phpredis-2.2.4.tar.gz到/usr/local/src目录
tar zxvf 2.2.4.tar.gz                                                                                     #解压
cd phpredis-2.2.4                                                                                         #进入安装目录
/usr/local/php/bin/phpize                                                                         #用phpize生成configure配置文件
./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 && make test

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 && make test

安装完成之后,出现下面的内容表示安装完成,需要配置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"                                             #WIN中是: extension=php_redis.dll
session.save_handler = redis                                 #原值:files
session.save_path = “tcp://127.0.0.1:6379″         #原值:/tmp,且未启用
session.auto_start = 0                                             #原值: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/redis/etc/redis.conf        将daemonize的值改为yes

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,且未启用

vi /etc/rc.d/rc.local
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

以上是关于Redis安装的主要内容,如果未能解决你的问题,请参考以下文章

windows环境下redis启动加到服务中

为啥有的时候开机,redis没有自动起来

为啥有的时候开机,redis没有自动起来

Redis-Win系统下安装

Redis入门

硬盘独立安装ubuntu,系统启动时默认进Win7,如何将ubuntu启动选项加到Win7启动选项?