X

Easy way to install and secure Redis on Linux Ubuntu 20.04

Dung Do Tien Apr 20 2021 479
In this article, I’ll guide you on how to install and secure Redis Server on Ubuntu Linux 18.04 or 20.04. It is very simple to install with some steps to finish and test it.

Setup Environment

You need to have a VPS or Virtual machine installed in Ubuntu version 18.04 or 20.04 with configuration as a minimum for 1GB of Ram and 1 core CPU. Please notice this server is only used for testing so it needs fewer resources if you run in your product. Maybe your server needs more resources, it depends on your application traffic.

Step 1: Install Redis Server

Firstly, we need SSH and connect to the server that wants to install Redis. The server maybe is VPS, hosting, personal computer…  I am using a VPS install Ubuntu 20.04.

If you only want to learn Redis and have no VPS to practice, you can use the free service of Google cloud, they are free for 3 months. I think it's enough to learn.

Okay, after connected we need to update all packages by using the command below:

sudo apt update
sudo apt upgrade

While updating, maybe some packages will require you to confirm the size of the update package. Please type Y and press Enter to accept and wait for the installation to be completed.

And then we will install the Redis server by running the command below:

sudo apt install redis-server

Redis also requires you to confirm before the install is complete. Type Y and press Enter to complete the installation.

Step 2: Access and execute some action in Redis 

1. How to check the status of Redis? It’s running or stopped?

To check the status of Redis is activated or stopped  we can run the command below:

systemctl status redis-server

If your Redis is running, you can see the green status as the image below:

Okay, we can access the Redis server and set, get some keys by using command redis-cli.
Type redis-cli and press Enter you will access inside of Redis. Now you can do anything in here:

The default port of Redis is 6379 and IP 127.0.0.1 is localhost.

Please type Ping and press Enter, you will see Redis return Pong message

contact_quizdeveloper@instance-3:~$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

Okay, now we will try to set a key to Redis by using command set key_name “value” see command below:

127.0.0.1:6379> set test "hello redis"
OK
127.0.0.1:6379> 

After pressing the Enter keyboard you will see the Redis return message OK.
And now to get value by key we can use the command get key_name See command below:

127.0.0.1:6379> get test
"hello redis"
127.0.0.1:6379> 

2. How do I start and stop a Redis server in Ubuntu Linux?

To restart Redis we can use the command below:

sudo systemctl restart redis.service

To stop Redis we can use the command below:

sudo systemctl stop redis.service

Step 3: Basic secure in Redis

1. How to change the default port’s Redis to another port? 

Go to the '/etc/redis' directory and edit the configuration file 'redis.conf' by using the vim editor.

cd /etc/redis/
vim redis.conf

If you see you have no permission you can run sudo su before running the command above.
Use the Down arrow keyboard to scroll this file. And find the line port 6379 and change it.

To save and quit vim editor, press ESC and type :wq! and press Enter

If you still not use Vim editor you can read the articles below:
Save and exit a file from Vim editor in Ubuntu Linux

And then, you need to restart Redis server to accept any change:

sudo systemctl restart redis

To check that this change has gone into effect, run the following netstat command:

sudo netstat -lnp | grep redis

You will see the result as below:

tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5199/redis-server 1 
tcp6       0      0 ::1:6379                :::*                    LISTEN      5199/redis-server 1 

Note: The netstat command may not be available on your system by default. If this is the case, you can install it (along with a number of other handy networking tools) with the following command:

sudo apt install net-tools

2. How to set a password for Redis Server in Ubuntu Linux?

To set the password of Redis, you also open redis.config file and add more line code anywhere of this file

requirepass your_password_here

Save and quit the file, don't forget to restart the Redis server.
To restart Redis run the command:

sudo systemctl restart redis.service

To test that the password works, open up the Redis client:

redis-cli

The following shows a sequence of commands used to test whether the Redis password works. The first command tries to set a key to a value before authentication:

set test2 10

That won’t work because you didn’t authenticate, so Redis returns an error:

Output
(error) NOAUTH Authentication required.

The next command authenticates with the password specified in the Redis configuration file:

auth your_password_here

Redis acknowledges:

Output
OK

Step 3: Open port for access from anywhere

Sometimes you want to open Redis for access from anywhere in the world. You can open redis.conf file and change bind option from bind 127.0.0.1 ::1 to bind 0.0.0.0. Save & quit the file. Now you can access it by publishing the IP and port you set for Redis.

bind 127.0.0.1 ::1
To
bind 0.0.0.0

Warning: I suggest never publish Redis on the global internet, You may be attacked by some hacker and don't forget that never store private into Redis and many as you know Redis using TCP protocol so it works better on LAN internet.

In conclusion, now Redis is very popular and It also indispensable in web programming to help make a website become better about performance so in this article note an easy way to install Redis on Ubuntu 20.04 you also install for Ubuntu 18.04 with the same command.