redis安装使用教程

redis-2.2.7.tgzphpredis-2.2.4.tar.gz

一:安装redis

1.下载redis并安装

$wget http://redis.googlecode.com/files/redis-2.2.10.tar.gz

$tar zvxf redis-2.2.10.tar.gz

$cd redis-2.2.10

$make

$sudo cp redis.conf /etc/

$sudo cp redis-benchmark redis-cli redis-server /usr/bin/

2.运行redis服务器

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

可使用以下命令检测redis是否启动

$ps -x | grep redis

1411 pts/0 S+ 0:00 /usr/bin/redis-server /etc/redis.conf

说明已启动

3.运行客户端程序后即可启用

$redis-cli

 

如果想在php中使用redis,还需要安装phpredis扩展

二:安装phpredis

1、安装phpredis

下载:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz

上传phpredis-2.2.4.tar.gz到/usr/local/src目录

$rz

cd /usr/local/src #进入软件包存放目录

tar zxvf phpredis-2.2.4.tar.gz #解压

cd phpredis-2.2.4 #进入安装目录

/usr/local/php/bin/phpize #用phpize生成configure配置文件(如果提示找不到phpize可能是没有安装phpize,请看附录1)

./configure --with-php-config=/usr/local/php/bin/php-config  #配置

make  #编译

make install  #安装

安装完成之后,出现下面的安装路径

/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

2、配置php.ini支持

vim /usr/local/php/etc/php.ini  #编辑配置文件,在最后一行添加以下内容添加

extension="redis.so"

:wq! #保存退出

3  重启服务器

sudo service httpd restart

(nginx:sudo service nginx restart)

4.phpinfo中看到redis则说明phpredis扩展已经安装成功

三.php中调用redis
1.当在phpinfo中看到以上内容后,我们就可以编写几个小的程序试试redis的功能:
     1.php
 <?php
$redis = new redis();
$redis->connect("127.0.0.1",6379);
$weibo = array(
                'uid' => 1,
                'content' => "我操了你大爷",
                'timestamp' => time()
        );
$redis->lpush('weibo',$weibo);
$redis->close();
?>
    2.php
<?php
$redis = new redis();
$redis->connect("127.0.0.1",6379);
$data = $redis->get("weibo");
var_dump($data);
$redis->close();
?>
2.使用redis消息队列发布微博,异步发布
    1.php
<?php
$redis = new redis();
$redis->connect("127.0.0.1",6379);
$weibo = array(
                'uid' => 1,
                'content' => "我操了你大爷",
                'timestamp' => time()
        );
$redis->lpush('weibo',json_encode($weibo));
$redis->close();
?>
    2.php
<?php
$redis = new redis();
$redis->connect("127.0.0.1",6379);
 
while(TURE){
        if($redis->lsize('weibo') > 0){
                $info = $redis->rpop('weibo');
                $info = json_decode($info,ture);
                mysql_query();    //将信息插入数据库,这里并没有写入详情代码
        } else
                sleep(1);
}
?>
 
 
附录1:安装phpize
php有很多扩展功能,我们在初次安装的时候并没有安装某些扩展,可能在使用的过程中,又需要用到这些扩展。php提供了phpize就是供我们安装需要的扩展的工具。
大部分机子都是没有安装phpize,我们可通过yum instal php-devel 进行安装(ubuntu 是 apt-get install php-devel)。
 
 
 
 
原文地址:https://www.cnblogs.com/xyxxs/p/4731132.html