How to Install Redis Server on CentOS 8

Redis is a free, open-source in-memory key-value store that supports different kinds of data structures. You can use it as a database, cache, message broker, and queue. It is used for building high-performance and scalable web applications. It supports a wide range of programming languages such as, Java, PHP, C, Perl, Python, C#, Lua, Go, and many more. It is a cross-platform and works in most Unix-based operating systems including, Linux, *BSD, and OS X.

In this tutorial, we will explain how to install Redis on CentOS 8 server.

Prerequisites

  • A server running CentOS 8.
  • A root password is configured the server.

Install Redis Server

By default, Redis package is available in the CentOS 8 default repository. You can install it by running the following command:

dnf install redis -y

After installing Redis server, start the Redis service and enable it to start at system reboot with the following command:

systemctl start redis
systemctl enable redis

You can also verify the status of the Redis server with the following command:

systemctl status redis

You should get the following output:

   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           ??limit.conf
   Active: active (running) since Tue 2020-09-15 04:52:54 EDT; 6s ago
 Main PID: 3076 (redis-server)
    Tasks: 4 (limit: 12527)
   Memory: 6.6M
   CGroup: /system.slice/redis.service
           ??3076 /usr/bin/redis-server 127.0.0.1:6379

Sep 15 04:52:54 centos8 systemd[1]: Starting Redis persistent key-value database...
Sep 15 04:52:54 centos8 systemd[1]: Started Redis persistent key-value database.

By default, Redis listen on port 6379. You can check it with the following command:

ss -ant | grep 6379

You should get the following output:

LISTEN     0        128             127.0.0.1:6379              0.0.0.0:*       

Next, test the Redis connectivity with the following command:

redis-cli

You should see the following output:

127.0.0.1:6379>

Now, run the following command to check the connectivity:

127.0.0.1:6379> ping

You should see the following output:

PONG

Configure Redis Cache

Next, you will need to configure Redis as a caching server. You can do it by editing Redis configuration file:

nano /etc/redis.conf

Add the following lines at the end of the file:

maxmemory 128mb
maxmemory-policy allkeys-lru

Save and close the file when you are finished. Then, restart the Redis service with the following command:

systemctl restart redis

Secure Redis Server

By default, redis-cli allows you to run any command within Raedis shell. So it is a good idea to secure a Redis shell with the password. You can enable password authentication by editing /etc/redis.conf file:

nano /etc/redis.conf

Find the following line:

# requirepass foobared

And, replace it with the following line:

requirepass A&#9D3Fs4%#df

Save and close the file when you are finished. Then, restart Redis service to apply the configuration changes:

systemctl restart redis

Now, connect to the Redis with the following command:

redis-cli

Once connected, run the following command:

127.0.0.1:6379> INFO server

You will get the following error because you don't authenticate.

NOAUTH Authentication required.

Now, run the following command to authenticates with the password specified in the Redis configuration file:

127.0.0.1:6379> AUTH A&#9D3Fs4%#df

You should see the following output:

OK

Now, run the following command:

127.0.0.1:6379> INFO server

You should get the following output:

# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:28849dbea6f07cc8
redis_mode:standalone
os:Linux 4.18.0-193.14.2.el8_2.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.3.1
process_id:3176
run_id:2bf42acb9a0c0f251220c9bf5d66982b1c0b1d87
tcp_port:6379
uptime_in_seconds:41
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:6324712
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

When you are finished, you can proceed to the next step.

Rename Dangerous Command

It is also recommended to rename some dangerous commands inside Redis shell. One of the dangerous command is "config". It is used to retrieve the Redis password.

Let's see how to retrieve the Redis password with "config" command.

First, connect to the Redis with the following command:

redis-cli

Once connected, authenticate Redis with the following command:

127.0.0.1:6379> AUTH A&#9D3Fs4%#df

Next, retrieve the Redis password using the following command:

127.0.0.1:6379> config get requirepass

You should get the Redis password in the following output:

1) "requirepass"
2) "A&#9D3Fs4%#df"

You can rename the "config" command by editing the file /etc/redis.conf:

nano /etc/redis.conf

Find the following line:

# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52

And, replace it with the following line:

rename-command CONFIG hitredis

Save and close the file. Then, restart Redis service with the following command:

systemctl restart redis

Now, connect and authenticate the Redis with the following command:

redis-cli
127.0.0.1:6379> AUTH A&#9D3Fs4%#df

Now, retrieve the Redis password with the "config" command:

127.0.0.1:6379> config get requirepass

You should get the following error:

(error) ERR unknown command `config`, with args beginning with: `get`, `requirepass`, 

Now, run the renamed command as shown below:

127.0.0.1:6379> hitredis get requirepass

You should get your Redis password in the following output:

1) "requirepass"
2) "A&#9D3Fs4%#df"

Conclusion

Congratulations! you have successfully installed and secured Redis server on CentOS 8. You can now host your application configure it to use Redis cache. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)