How to install redis with master slave config in ubuntu ec2 instance

by jagbir on October 24, 2012

Redis is an open source, advanced key-value store. This guide provides you instructions/commands you can use to install and configure redis with master/slave config in two ec2 Ubuntu instances. Our intention is to use redis to store php sessions which needs to be shared among other instances.

1. Open ports in aws firewall:
Redis works with port 6379 which you need to open in aws firewall. You can do this by command line tools or Amazon Consle. IN case of Console, navigate to Security Groups, select security group in which instances are running and then select “inbound” tab to see/update rules.

Put port 6379 in port range text box and select the same security group (in which you are working right now) so that all instances in this security group can community using this port. Make sure to not open the port for world (by selecting in source), this would be really bad practice from security point of view.

If you need to monitor redis from some other host/nagios etc. You need to open port for them as well either by specifying their IP address or their security group.

2. Install using apt-get
You can setup Redis by installing using apt-get/aptitute or downloading manually. Both are easy ways but this installation through apt-get will be quick though it won’t fetch you latest/greatest stable build of Redis.

If you want to install latest build, then you should compile it and for that you can jump to step 3 right now.

A quick search for redis-server shows that the default Ubuntu repositories having very old version of Redis:

$ apt-cache show redis-server
Package: redis-server
Priority: optional
Section: universe/misc
Installed-Size: 511
Maintainer: Ubuntu Developers <>
Original-Maintainer: Chris Lamb <>
Architecture: amd64
Source: redis
Version: 2:2.2.12-1build1
Depends: libc6 (>= 2.7), adduser
Filename: pool/universe/r/redis/redis-server_2.2.12-1build1_amd64.deb

Here you can see redis-server_2.2.12 while on Redis site, latest stable version is 2.4.17 as of writing this article. Obviously we should not install the old 2.2.12 release.

We can pick another repository which should have latest or at least near to latest version. One of such repository from where you can get the package is dotdeb. Here in this repositories I can see version 2.4.16 which is very near to latest release.

Add following in /etc/apt/sources.list file:

$ sudo vim  /etc/apt/sources.list
	deb squeeze all
	deb-src squeeze all

Get and add dotdeb gnupg key after which run apt-get update and install redis-server:

$ wget; cat dotdeb.gpg | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install redis-server

3. Install using manual downloading it from redis site
This will enable you to get latest stable binaries of redis server. This process is neatly explained on Redis site itself here.

I’m here quickly putting commands with minor changes:

$ sudo apt-get install build-essential
$ cd /usr/src/
$ wget
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ sudo make
$ cd src 
$ sudo cp redis-server /usr/local/bin/
$ sudo cp redis-cli /usr/local/bin/
$ sudo mkdir /etc/redis /var/redis /var/log/redis
$ cd ../utils; sudo cp redis.conf.tpl /etc/redis/redis.conf; sudo cp redis_init_script /etc/init.d/redis
$ sudo update-rc.d redis defaults

4. Update config to make master/slave
After following above commands in both of the instances, you will have Redis installed and running in them. Making Redis work in master/slave config is relatively very easy, all you need to do is to tell one of server to get updates from other. On the instance, where you want to make Redis slave, login in it and update redis.conf file. Essential parameters you need to check/update are shown below:

$ sudo vim /etc/redis/redis.conf
daemonize yes
pidfile /var/run/redis/
port 6379
slaveof 10.x.x.x 6379

You can see the last line slaveof which specify from which server it should get updates using which port. It’s a good idea to specify internal IP of other instance for such internal communication for relatively better performance as well as save some costs.

You need to check/update config in both of the servers except on master, do not put that “slaveof” configuration line.

Start redis and check:

$ sudo /etc/init.d/redis start
$ redis-cli ping

Redis is installed, configured and running.

5. Install PHP client and update PHP config to use it
Our intention is to use Redis with PHP and for that you need to install Redis client for PHP. You can get more info about Redis clients here.

$ sudo apt-get install php5-redis
$ cat /etc/php5/conf.d/redis.ini
; configuration for php redis module
$ sudo service apache2 reload
$ sudo service apache2 reload
 * Reloading web server config apache2

You can see that PHP config is updated to use Redis extension. We reloaded Apache to apply updated PHP configs. You are good to go. Let me know if you face any issues or there’s any suggestions to improve the process.

Previous post:

Next post: