How to install Redis on a Centos 6.4 Server

From Brian Nelson Ramblings
Revision as of 14:47, 14 March 2014 by Brian (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

How to install redis on a Centos Server

yum install redis php-pecl-redis
service redis start
chkconfig redis on

Now verify its set to start at boot

chkconfig --list redis
redis           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Now that was pretty simple, but lets say you want to add a second instance to your server on a different port

Add a second instance of Redis

cp redis.conf redis2.conf && cp /etc/init.d/redis /etc/init.d/redis2

Change

vim /etc/init.d/redis2
pidfile="/var/run/redis/redis.pid" to pidfile="/var/run/redis/redis2.pid"
REDIS_CONFIG="/etc/redis.conf" to REDIS_CONFIG="/etc/redis2.conf"

Change

vim /etc/redis2.conf
port 6379 to port 6380
unixsocket /tmp/redis.sock to unixsocket /tmp/redis2.sock
service redis2 start

Let also make this instance start up on boot

chkconfig redis2 on

Now verify its set to start at boot

chkconfig --list redis2
redis2          0:off   1:off   2:on    3:on    4:on    5:on    6:off

To install a web interface for redis

cd /downloads/
git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git
cd phpRedisAdmin/includes
cp config.sample.inc.php config.inc.php

Make sure the setting are correct

vim config.inc.php

Lets add the RedisAdmin configuration file to Apache

vim /etc/httpd/conf.d/redisadmin.conf

Now add the following

#
#  Web Interface for RedisAdmin
# 

<Directory "/downloads/phpRedisAdmin/">
  Order Deny,Allow
  Deny from all
  Allow from 127.0.0.1
  Allow from <your ipaddress>
</Directory>

Alias /redisAdmin /downloads/phpRedisAdmin
Alias /redisadmin /downloads/phpRedisAdmin

View your RedisAdmin Section

http://example.tld/redisadmin

Create a Bash Script to make sure Redis is Running

vim /scripts/redis-check.sh

Add the following for the above installation

#!/bin/bash
PS=$(which ps)
GREP=$(which grep)
WHEN=$(date +"%Y-%m-%d-%H:%M:%S")
   if  ! $PS aux | $GREP "redis.conf" | $GREP -v grep 2>&1 > /dev/null; then
       /etc/init.d/redis restart
       echo 'Restarted Redis @' $WHEN
   fi
#Check Second instance
   if  ! $PS aux | $GREP "redis2.conf" | $GREP -v grep 2>&1 > /dev/null; then
       /etc/init.d/redis2 restart
       echo 'Restarted Redis2 @' $WHEN
   fi

Make the Script executable

 chmod +x /scripts/redis-check.sh

Add your script to your cron to run every 3 minutes or so

vim /var/spool/cron/root
*/3 * * * * /bin/bash /script/redis-check.sh >> /var/log/redis-check.log