Atlantic.Net Blog

How to Install and Secure Redis on Ubuntu 18.04

Hitesh Jethva
by Atlantic.Net (64posts) under VPS Hosting
0 Comments

Redis, which stands for “remote dictionary server,” is a key-value store that stores data as key-value pairs. Redis is a NoSQL database, meaning it does not have tables, rows and columns like in MySQL and Oracle databases. It does not use INSERT, SELECT, UPDATE and DELETE. Instead of this, Redis uses data structures to store data. It uses five primary data structures including Strings, Lists, Sets, Sorted Sets, and Hashes, as well as three extra data structures including bitmaps, hyperloglogs and geospatial indexes. One of the most important features of Redis is that it is an in-memory database, meaning it keeps the data in memory, making it super-fast. However, Redis also has options to write the data to the disk.

In this tutorial, we will show you how to install and secure Redis on Ubuntu 18.04.

Prerequisites

  • A fresh Ubuntu 18.04 VPS on the Atlantic.net Cloud Platform.
  • A root password configured on your server.

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server.  Create a new server, choosing Ubuntu 18.04 as the operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 18.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Install Redis Server

By default, the Redis package is available in the Ubuntu 18.04 server. You can install it with the following command:

apt-get install redis-server php-redis -y

After installing the Redis server, you will need to configure the Redis server to use the init system for managing Redis as a service. You can configure it by editing redis.conf file:

nano /etc/redis/redis.conf

Find the following line:

supervised no

Replace it with the following line:

supervised systemd

Save and close the file. Then, restart the Redis service to apply the changes:

systemctl restart redis

You can also check the status of the Redis service with the following command:

systemctl status redis

You should get the following output:

Verify Redis Server

It is recommended to test whether the Redis server is functioning as expected or not.

You can test it using the redis-cli command-line utility:

redis-cli
127.0.0.1:6379> ping

If everything is fine, you should get the following output:

PONG

You can now exit from the Redis shell with the following command:

exit

You can also check the version of the Redis with the following command:

redis-cli -v

Output:

redis-cli 4.0.9

For more information about Redis, run the following command:

redis-cli info

You should see the following screen:

You can also verify the Redis listening port with the following command:

netstat -lnp | grep redis

You should see that Redis server is listening on localhost on port 6379:

Secure Redis with Password

By default, Redis can be accessed without any password. It is recommended to secure Redis with a password for security reasons.

First, generate a strong password with OpenSSL command as shown below:

openssl rand 60 | openssl base64 -A

You should see the following output (a password like this):

MGDCmmWh2Ot+aZDv7TBr54SY5PueoSAVhX41sApcOLWYBTlPrfe01OT/nqO0YWK/y0ROozsDi9Dw61jR

Now, paste your password in the Redis configuration file /etc/redis/redis.conf:

nano /etc/redis/redis.conf

Find the following line:

# requirepass foobared

Replace it with the following line:

requirepass MGDCmmWh2Ot+aZDv7TBr54SY5PueoSAVhX41sApcOLWYBTlPrfe01OT/nqO0YWK/y0ROozsDi9Dw61jR

Save and close the file. Then, restart the Redis service to apply the changes:

systemctl restart redis

Now, connect to the Redis command-line with the following command:

redis-cli

127.0.0.1:6379>

Now, run the following command to test whether the Redis password works:

127.0.0.1:6379> set test "How Are You"

You will get the following error because didn’t authenticate:

(error) NOAUTH Authentication required.

Now, authenticate Redis with the password as shown below:

127.0.0.1:6379> auth MGDCmmWh2Ot+aZDv7TBr54SY5PueoSAVhX41sApcOLWYBTlPrfe01OT/nqO0YWK/y0ROozsDi9Dw61jR

You should get the following output:

OK

Now, run the previous command again:

127.0.0.1:6379> set test "How Are You"

Output:

OK

Now, get the value of the test:

127.0.0.1:6379> get test

You should see the following output:

"How Are You"

After testing successfully, exit from the Redis command-line with the following command:

127.0.0.1:6379> exit

Rename Dangerous Commands

It is always a good idea to rename some dangerous commands including config, shutdown, flushdb, flushall and rename. These commands can destroy or wipe your data if run by mistake.

You can rename these commands by editing /etc/redis/redis.conf file:

nano /etc/redis/redis.conf

Add the following lines at the end of the file:

rename-command CONFIG ATLANTICCONFIG
rename-command FLUSHDB ATLANTICFLUSHDB
rename-command SHUTDOWN ATLANTICSHUTDOWN
rename-command FLUSHALL ATLANTICFLUSHALL

Save and close the file, then restart Redis service to implement the changes:

systemctl restart redis

To test the above commands, enter the Redis command line:

redis-cli

Now, authenticate Redis with the password as shown below:

127.0.0.1:6379> auth MGDCmmWh2Ot+aZDv7TBr54SY5PueoSAVhX41sApcOLWYBTlPrfe01OT/nqO0YWK/y0ROozsDi9Dw61jR

You should get the following output:

OK

Now, try to run the original CONFIG command as shown below:

127.0.0.1:6379> config get requirepass

You should get the following error:

(error) ERR unknown command 'config'

Now, try again with the renamed command:

127.0.0.1:6379> atlanticconfig get requirepass

You should see the following output:

1) "requirepass"
2) "MGDCmmWh2Ot+aZDv7TBr54SY5PueoSAVhX41sApcOLWYBTlPrfe01OT/nqO0YWK/y0ROozsDi9Dw61jR"

Conclusion

Congratulations! Your Redis server is now installed and secured. This method is compatible with Atlantic.Net VPS Hosting. For more information, you can visit the Redis documentation page.

Get A Free To Use Cloud VPS

Free Tier Includes:
G2.1GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G2.1GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources