Install PhpRedis & Redis-Server in CentOS/RedHat

Steps to install PhpRedis & Redis Server in CentOS

What is Redis?

Redis is a flexible, open-source, key value data store. It’s other NoSQL databases, such as Cassandra and MongoDB. Redis allows the user to store vast amounts of data without the limits of a relational database.It is written in ANSI C. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. So here you will find the steps to install phpRedis and Redis server in CentOS server.

Redis consists of two key parts, first being the server it self and second PhpRedis extension that allows PHP to communicate with Redis.

The procedure should apply to Fedora and Red Hat distributions as well. In case you feel like skipping these steps, feel free to use the GitHub for an even simpler installation and configuration of Redis as a service

Redis can be install using Linux repository “yum” and from source. We will cover both type of installation.

Redis Install by “yum” Repository –

Follow these blow steps to install Redis & Redis-Server in Linux using yum repository which will automatically install and setup redis on server which is very fast and easy to setup.

First, install the epel repo

sudo rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm  

Next, install the remi repo

sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm  

Now, you should be able to install redis using the yum package manager.

yum install redis -y  

You should now be able to start redis using the following command

redis-server

Install Redis Server in CentOS from Source

There is another way of installation of redis using source which is also suggested by redis community as redis has no dependency other than gcc compiler so it’s the best way to compile redis from source. As installation using package manager may sometime not installing latest version of redis in server.

First we have to download and install a compiler. This will help us install Redis from source:

yum install make gcc wget

Download and Compile Software

Begin the installation process by issuing the following sequence of commands to download the software and prepare it for use:

cd /opt/
mkdir /opt/redis
wget http://redis.googlecode.com/files/redis-2.2.2.tar.gz 
tar -zxvf /opt/redis-2.2.2.tar.gz
cd /opt/redis-2.2.2/
make
make install

This will download and compile the 2.2.2 version of Redis.  It is important to use the most up to date version of the software to avoid security flaws and bugs as well as to take advantage of the latest features. When you compile software manually, you are responsible for ensuring your system is running the most current version without the assistance of your system’s package management tools.

Now we will modify the default configuration of the Redis and change the location of the database. We would also change the log notices to a production acceptable level and the location of the log file.

#mkdir /etc/redis/var/lib/redis
#sed -e "s/^daemonize no$/daemonize yes/" -e "s/^dir \.\//dir \/var\/lib\/redis\//" -e "s/^loglevel debug$/loglevel notice/" -e "s/^logfile stdout$/logfile \/var\/log\/redis.log/" redis.conf  > /etc/redis/redis.conf

After executing above command your redis.conf file will look like as per below –

 

daemonize yes
pidfile /var/run/redis.pid
logfile /var/log/redis.log

port 6379
bind 127.0.0.1
timeout 300

loglevel notice

## Default configuration options
databases 16

save 900 1
save 300 10
save 60 10000

rdbcompression yes
dbfilename dump.rdb

dir /var/lib/redis
appendonly no

glueoutputbuf yes

Add the ‘redis’ init script

#vi /etc/init.d/redis-server

 

Create a file and add below code for start service in init script

#!/bin/bash
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/redis-server
DAEMON_ARGS=/etc/redis/redis.conf
NAME=redis-server
DESC=redis-server
PIDFILE=/var/run/redis.pid
 
test -x $DAEMON || exit 0
test -x $DAEMONBOOTSTRAP || exit 0
 
set -e
 
case "$1" in
start)
echo -n "Starting $DESC: "
touch $PIDFILE
chown redis:redis $PIDFILE
if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON --background -- $DAEMON_ARGS
then
echo "$NAME."
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry 10 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
then
echo "$NAME."
else
echo "failed"
fi
rm -f $PIDFILE
;;
 
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
 
exit 0

 

Give appropriate permission to the init script

chmod u+x  /etc/init.d/redis-server
To run the ‘redis’ server at startup we need to add it to the chkconfig list
#/sbin/chkconfig --add redis-server
#/sbin/chkconfig --level 345 redis-server on
Finally we are ready to start the Redis Server
# /etc/init.d/redis-server start

Check if Redis is working

External programs talk to Redis using a TCP socket and a Redis specific protocol. Redis provide a command line utility which used to send command to redis on backend. This program is implemented in different language. This program is called redis-cli.

The first thing to do in order to check if Redis is working properly is sending a PING command using redis-cli:

$ redis-cli ping
PONG

If you get PONG in return that means redis server is working fine on server.

Another way to run redis-cli is without arguments: the program will start in interactive mode, where you can type different commands and see their replies.

$ redis-cli
redis 127.0.0.1:6379> ping
PONG

Install Redis extension of PHP using Source

First we need to install “php5-dev” which  provides the dev library as well as the phpize command which is required for the compiling Source code.

sudo yum -y install php-devel

Next we will download phpredis from git repository. Install Git if you don’t have using following command.

sudo yum -y install git
git clone git://github.com/nicolasff/phpredis.git

Compile and install phpRedis by running the following commands.

cd phpredis
phpize
./configure
make
sudo -s make install

Next we need to add extension with php configuration so that php can enable this module using following command. This step need root access for updating php extension file.

sudo -s
echo "extension=redis.so">/etc/php5/conf.d/redis.ini
exit

To check Redis is working with php we make a simple php file as follow and run it.

vi phpredis.php

And wirte the following code in the phpredis.php file.

<?php 
$redis=new Redis() or die("Can not load Redis.");
$redis->connect('127.0.0.1'); 
$redis->set('Redis test_key', 1);

Now, run the phpredis.php file either using your browser or command line i am here to chose command line option as.

php phpredis.php

If this file runs without any error then check cache with redis using following command.

redis-cli
redis 127.0.0.1:6379>keys * 
1) "Redis test_key redis 127.0.0.1:6379

if the above result found that means Redis is working perfectly. If not that means you have missed some steps from above.

Conclusion

‘Redis’ also supports datatypes such as Transitions, Publish and Subscribe. ‘Redis ’ is considered more powerful than ‘Memcache’ . It would be smart to bring ‘Redis’ into practice and put ‘Memcache’ down for a while.

 

LinuxTweaks

Linuxtweaks Blog helping Server Admin to Manage their servers, Desktop users for making more friendly with linux. Tutorials , guides and tips for linux server maintenance. Here you can learn how to tweak linux servers with code and how to manage it properly.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *