Install Redis Server on Ubuntu

What is redis server?

Redis is an open source server used as a database and very popular as caching. If you have a website which runs slowly and you want to speed up the loading time you use redis as a caching mechanism.

You should always cache data which does not change often. Redis store this data fro time specified so that you do not have to get the same data over and over from mysql database.

First time you get the data from the database and store them on redis server and fetch this data from redis instead of getting them from mysql database.

How to install redis server on ubuntu?

To install redis server on your ubuntu server open the terminal window and run following commands:

# update apt package
sudo apt-get update
sudo apt-get install build-essential tcl

# change dir to /tmp
cd /tmp

# download redis stable tar.gz
curl -O http://download.redis.io/redis-stable.tar.gz

# unpack redis package
tar xzvf redis-stable.tar.gz

# build and install
cd redis-stable
make
make test
sudo make install​

How to configure a redis server?

To configure your redis server follow the steps below:

# create a redis directory in /etc
$ sudo mkdir /etc/redis

# copy configuration files
$ sudo cp /tmp/redis-stable/redis.conf /etc/redis

# open and edit redis.conf
$ sudo nano /etc/redis/redis.conf

# search for "supervised" in /etc/redis/redis.conf
# and replace that text with following
supervised systemd

# search for "dir" in /etc/redis/redis.conf
# and replace that text with following
dir /var/lib/redis​

how to keep redis server running?

To run redis server constantly on ubuntu you have to first create a redis service. To create a new service on ubuntu follow the steps below:

# create a redis service file
$ sudo nano /etc/systemd/system/redis.service

# paste following contents to open file
# and save the file once contents are pasted
[Unit]
Description=Redis Server
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target​

How to start/stop redis service?

To start/stop or restart a redis server on your ubuntu machine run following commands only when you have setup the redis service mentioned in previous step:

# to start redis service
$ sudo service redis start

# to stop redis service
$ sudo service redis stop

# to check redis service status
$ sudo service redis status​

Enable Redis to Start at Boot

Once you have created a redis service you might want to run following command so that even if your ubuntu server stops for any reason and restarted you will have redis server running.

sudo systemctl enable redis​

Create the Redis User, Group and Directories

To create redis user, group and directory follow the steps below:

# create redis user and group
$ sudo adduser --system --group --no-create-home redis

# make redis home dir
$ sudo mkdir /var/lib/redis

# update file permissions
$ sudo chown redis:redis /var/lib/redis

# prevent regular users
$ sudo chmod 770 /var/lib/redis​

How to test if redis server is installed correctly?

Once you followed above steps and installed redis correctly try following command to test if installation process went smoth and you get response back from redis:

# switch to redis cli mode
$ redis-cli

# send ping request
127.0.0.1:6379> ping

# you will see following output
PONG

# exit the server
127.0.0.1:6379> exit​