Install Redis & Redis-Server in Ubuntu 14.10/14.04/13.10

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.

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

Redis can be install using ubuntu repository “apt” and from source. We will cover both type of installation here.

Redis Install by “apt” Repository –

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

Step 1. Create a new file in the apt source  /etc/apt/sources.list.d/

sudo vi /etc/apt/sources.list.d/dotdeb.org.list

And write the following code in it.

# /etc/apt/sources.list.d/dotdeb.org.list
deb http://packages.dotdeb.org squeeze all
deb-src http://packages.dotdeb.org squeeze all

This repo will use to get the latest redis for ubuntu.Then you need to authenticate these repositories using their public key by running rhe following command.

sudo wget -q -O - http://www.dotdeb.org/dotdeb.gpg | sudo apt-key add -

Step 2.  Now update APT cache and install Redis by running the following commands.

sudo apt-get update
sudo apt-get -y install redis-server

Redis install using 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 with build essential  and tcl which is cross-platform interpreted scripting language. This will help us install Redis from source:

 sudo apt-get install build-essential tcl8.5

Next step is to download latest stable version of redis tarball file http://download.redis.io/redis-stable.tar.gz.

wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable

Proceed to with the make command:

make

You can try if your build works correctly by typing make test, this is an optional step

make test

Finish up by running make install, which installs the program system-wide.

sudo make install

Once the program has been installed, Redis comes with a built in script that sets up Redis to run as a background daemon.

To access the script move into the utils directory:

cd utils

From there, run the Ubuntu/Debian install script:

sudo ./install_server.sh

You can press enter to make it default setup or can change any path which you want.

By default this will install redis with redis_6379 setup which you can also change while executing above script.

Redis Server can  start and stop with these commands (the number depends on the port you set during the installation. 6379 is the default port setting):

sudo service redis_6379 start
sudo service redis_6379 stop

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

To set Redis to automatically start at boot, run:

sudo update-rc.d redis_6379 defaults

Install Redis as PHP Extension

Redis provide php extension to work with php. Here we will cover installation of Redis Extension of php from source compilation and using apt repository.

Following command will install and setup redis extension with php.

sudo apt-get -y install php5-redis

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 apt-get -y install php5-dev

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

sudo apt-get -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.

 

Balvinder Singh

Hello, I am Balvinder Singh - DevOps Engineer with 2.5+ year of working experience with different server environments. Tag Line:-Linux | AWS| WHM |Monitoring | Virtualization | Optimization | Performance | Security | Release & Deployment. I love helping companies / clients to deploy their code / applicateions to well managed, optimized, secure server and can go extra mile to satisfy.

You may also like...

Leave a Reply

Your email address will not be published.