Installing Redis to an AWS EC2 Machine

Felipe Rohde
3 min readMay 25, 2018

--

Redis is too expensive at some providers, a cheaper solution is setup your own EC2 machine at AWS with this simple steps. This tutorial is straight to the point, if you have some issues, just comment.

  1. Start an EC2 through wizard at AWS Console, choose a recent AWS image. The instance size can be t2.nano or c3.large, as your needs.
  2. While it starts, on left panel, click "Elastic IPS" and allocate a new one, as soon machine had started, attach it. It will be your public access.
  3. At left panel too, open "Security Groups", choose de SG related with your machine, and at "Inbound" click "Add Rule", at "Port Range" type 6379, and source (for tests), choose your IP. Afterwards, in production, constrain to a security group.
  4. Once started, connect it via SSH with key pairs.
ssh -i "key.pem" ec2-user@ec2-{you-elastic-ip}.compute-1.amazonaws.com

5. Update yum packages and install.

sudo yum -y update
sudo yum -y install gcc make

6. Download and install Redis, change to the latest version.

cd /usr/local/src
sudo wget http://download.redis.io/releases/redis-4.0.9.tar.gz
sudo tar xzf redis-4.0.9.tar.gz
sudo rm redis-4.0.9.tar.gz

7. Recompile Redis.

cd redis-4.0.9
sudo make distclean
sudo make

8. Install TCL and test Redis.

sudo yum install -y tcl
sudo make test

9. Make base directories and copy config files to them.

sudo mkdir /etc/redis
sudo chown ec2-user:ec2-user /etc/redis
sudo cp src/redis-server src/redis-cli /usr/local/bin
sudo cp redis.conf /etc/redis/redis.conf

10. Using nano, or your preferred file editor, change the following lines (unig nano you can find lines using ctrl+w):

sudo nano /etc/redis/redis.confbind 127.0.0.1 -> #bind 127.0.0.1
dir ./ -> dir /etc/redis
daemonize no -> daemonize yes
protected-mode yes -> protected-mode no
pidfile /var/run/redis.pid -> pidfile /etc/redis/redis.pid
logfile '' -> logfile /etc/redis/redis_log

If you have some doubts, see my config at https://gist.githubusercontent.com/feliperohdee/d04126b0b727e2a0ef5eee04542794df/raw/7464540e95854703ed5797d999bd4d0419ca4198/redis.conf

Sometimes, is important to set some maxmemory policies too, for instance, I've been using with AWS t2.micro with 1GB RAM.

maxmemory 700mb
maxmemory-policy noeviction

At this way, when memory reaches 700Mb, Redis starts responding with error messages on write operations, instead of evict old keys using LRU alghoritm. For cases where data persistence isn't a question, you can set it with volatile-lru and Redis take care to eliminate old records for you.

(You will been using redis without any protection, at production, you might ensure to constrain the access for a security group. Or use protected-mode enabled and bind an ip, or a range of. But this is not the focus of this tutorial.)

11. Download a boot script and add it to "init.d" folder, turn it executable, and allow system to auto start it.

sudo wget https://gist.githubusercontent.com/feliperohdee/d04126b0b727e2a0ef5eee04542794df/raw/4531d1809639fe00bef81985fe076f6a004471be/redis-serversudo mv redis-server /etc/init.d
sudo chmod 755 /etc/init.d/redis-server
sudo chkconfig --add redis-server
sudo chkconfig --level 345 redis-server on

12. Download and enable a script which disables a linux memory management resource, which could make redis slow.

wget https://gist.githubusercontent.com/feliperohdee/d04126b0b727e2a0ef5eee04542794df/raw/5c540f2165eb8beb3c62360507ed3dbe6ab71f38/disable-transparent-hugepagessudo mv disable-transparent-hugepages /etc/init.d
sudo chmod 755 /etc/init.d/disable-transparent-hugepages
sudo chkconfig --add disable-transparent-hugepages

13. Append this file to fix a low-memory issue on backups, and disable memory swap (which could cause Redis’ process to be blocked by the I/O operation of the disk).

sudo nano /etc/sysctl.conf# Add
vm.overcommit_memory = 1
vm.swappiness = 0

14. Finally, start the service, and test it.

sudo service redis-server start
redis-cli ping
#PONG

Just that.

--

--

Felipe Rohde

A good Javascript and Golang developer. Tech lead at EasyAuth and owner of Simple Image Cloud.