1、下载源码包redis-2.8.21.tar.gz,并将其上传到指定目录/urs/src,然后对其进行解压:
[[email protected] src]# tar -xvf redis-2.8.21.tar.gz
进入解压后的目录,并执行下面命令,指定安装目录为/urs/local/redis:
[[email protected] src]# cd redis-2.8.21
[[email protected] redis-2.8.21]# make PREFIX=/usr/local/redis install
安装redis成功后,可以在/usr/local/redis看到一个bin的目录,里面包括了以下文件:
[[email protected] ~]# cd /usr/local/redis/bin/
[[email protected] bin]# ls
redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server
2、添加redis启动脚本到服务中:
在/etc/rc.d/init.d目录,创建一个文件,名为redis(说明:/etc/rc.d/init.d/目录下的脚本在系统启动的时候某些指定脚本将被执行),然后在文件中添加如下内容:
[[email protected] ~]# vim /etc/init.d/redis
#!/bin/bash
# Init file for redis
# chkconfig: - 80 12
# description: redis daemon
# processname: redis
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/bin"
CONFIG="/etc/redis/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
将解压后的redis目录下的配置文件redis.conf拷贝到/etc/redis/redis.conf (注意要与redis的启动脚本中的路径一致)
[[email protected] ~]# mkdir /etc/redis
[[email protected] ~]# cp /usr/src/redis-2.8.21/redis.conf /etc/redis/redis.conf
然后将redis添加到注册服务:
[[email protected] ~]# chkconfig --add redis
[[email protected] ~]# chkconfig redis on
[[email protected] ~]# chkconfig --list redis
redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
重启redis:
[[email protected] ~]# service redis restart
Stop Redis Server: [ OK ]
Starting Redis Server: [ OK ]
查看是否启动成功:
[[email protected] ~]# ps -ef | grep redis
root 2984 1 0 13:40 ? 00:00:00 /usr/local/bin/redis-server 0.0.0.0:6379
root 2989 2444 0 13:40 pts/1 00:00:00 grep redis
修改/etc/redis/redis.conf,设置redis进程为后台守护进程,并指定一个密码:
[[email protected] ~]# vim /etc/redis/6379.conf
daemonize yes //daemonize:是否以后台daemon方式运行
requirepass 20082009 //设置密码为20082009
设置完后,需要重启redis才能使设置生效:
[[email protected] ~]# service redis restart
修改环境变量文件,添加如下内容:
vim /etc/profile
#set redis path
export REDIS_HOME=/usr/local/redis
export PATH=${REDIS_HOME}/bin:${PATH}
通过source /etc/profile 使其立刻生效
3、调用redis-cli的命令进行简单操作(注意是否启动密码验证):
[[email protected] ~]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 20082009 //需要输入密码
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name lebron james
(error) ERR syntax error
127.0.0.1:6379> set name "lebron james" //有空格的字符串需要加“”
OK
127.0.0.1:6379> get name
"lebron james"
127.0.0.1:6379> set name lebronjames
OK
127.0.0.1:6379> get name
"lebronjames"
127.0.0.1:6379>
4、php中扩展redis:
下载php的一个扩展phpredis的源码包phpredis-2.2.4.tar.gz,将其上传到服务器指定位置,对其进行解压,然后进入解压后的目录:
tar -xvf phpredis-2.2.4.tar.gz
cd phpredis-2.2.4
用phpize生成configure配置文件
[[email protected] phpredis-2.2.4]# /usr/local/php/bin/phpize
然后执行如下命令:
[[email protected] phpredis-2.2.4]# ./configure --with-php-config=/usr/local/php/bin/php-config
[[email protected] phpredis-2.2.4]# make && make install
配置php支持phpredis,在php.ini文件添加如下内容:
vim /usr/local/php/lib/php.ini
extension= /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/redis.so
然后重启分别重启nginx,php-fpm和redis:
service nginx restart
service php-fpm restart
service redis restart
查看phpinfo(),显示如下,表明php扩展redis成功:
一个简单的php操作redis的例子:
<?php $redis = new Redis(); $redis->connect('127.0.0.1', '6379') or die ("Could not to connect to redis"); //或者具体主机的IP地址也行 $redis->set("test", “php handle redis"); var_dump($redis->get('test')); echo "<br/>"; $redis->del('test'); //删除赋值 var_dump($redis->get('test')); ?>
输出结果:
从结果中可以看出,redis并没有起到作用,后来分析分析,由于之前在redis.conf文件中配置了密码登录redis的限制,现在只需要把redis.conf里面的 requirepass 20082009 这句注释掉即可:#requirepass 20082009,然后重启redis:service redis restart
再次执行php文件,显示如下结果,表明php操作redis成功:
版权声明:本文为博主原创文章,未经博主允许不得转载。