一、配置依赖环境
yum install tcl
二、使用下列指令执行下载、解压、安装
tar xzf redis-3.2.0.tar.gz cd redis-3.2.0 make make install
三、安装成功后,拷贝配置文件到目录/etc,并打开文件进行编辑
cp redis.conf /etc
vi /etc/redis.conf
四、编辑配置将daemonize no改为daemonize yes,即将Redis配置为以守护进程的方式启动,且bind:改为0.0.0.0,即处理所有IP请求。保存配置后,可以使用下列方式启动Redis
/usr/local/bin/redis-server /etc/redis.conf
五、但是为了更加方便地启动,可以创建一个启动文件,然后将它加入 系统服务中
vi /etc/init.d/redis
将下列代码粘贴进去
#chkconfig: 2345 80 15 #description: Start and Stop redis PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/local/bin/redis-server REDIS_CLI=/usr/local/bin/redis-cli PIDFILE=/var/run/redis.pid CONF="/etc/redis.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $REDIS_CLI -p $REDISPORT SHUTDOWN while [ -x ${PIDFILE} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac
上面文件保存后,更改其执行权限,将其加入系统服务中,同时设置为自动启动
chmod +x /etc/init.d/redis
chkconfig --add redis
chkconfig redis on
使用下列指令查看一下,如果2、3、4、5项为开启状态,即表示配置成功
chkconfig --list redis
现在可以使用下列指令启动Redis服务
service redis start
启动后可使用下列指令在本地测试
(redis-cli -h 127.0.0.1-p 6379)
redis-cli -h 127.0.0.1-p 6379 >set foo bar OK >get foo "bar" >quit
六、开启端口
上面测试表示Redis已经正常运行,并开启了默认端口为6379。
如果系统开启了防火墙,可以使用下列指令开放6379端口:
vim /etc/sysconfig/iptables
插入一条配置:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
保存后,重启防火墙
service iptables restart