How to install Redis server on CentOS 8

How to install Redis server on CentOS 8

Redis is a free and open source in-memory key-value store that supports different types of data structures. You can use it as a database, cache, message broker and queue. It is used to build high-performance and scalable web applications. It supports multiple programming languages, such as Java, PHP, C, Perl, Python, C#, Lua, Go, etc. It is a cross-platform and can be used in most Unix-based operating systems, including Linux, *BSD and OSX.

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

prerequisites

  • Server running CentOS 8.
  • The root password is configured for the server.

Install Redis server

By default, the 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 the Redis server, start the Redis service and use the following command to enable it to start when the system reboots:

systemctl start redissystemctl enable redis

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

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 listens on port 6379. You can use the following command to check:

ss -ant | grep 6379

You should get the following output:

LISTEN     0        128             127.0.0.1:6379              0.0.0.0:*       

Next, use the following command to test Redis connectivity:

redis-cli

You should see the following output:

127.0.0.1:6379>

Now, run the following command to check connectivity:

127.0.0.1:6379> ping

You should see the following output:

PONG

Configure Redis cache

Next, you need to configure Redis as a cache server. You can do this by editing the Redis configuration file:

nano /etc/redis.conf

Add the following line at the end of the file:

maxmemory 128mb
maxmemory-policy allkeys-lru

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

systemctl restart redis

Secure Redis server

By default, redis-cli allows you to run any command in Raedis Shell. Therefore, it is a good idea to password protect the Redis shell. You can enable password authentication by editing the /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 done. Then, restart the Redis service to apply the configuration changes:

systemctl restart redis

Now, use the following command to connect to Redis:

redis-cli

After connecting, run the following command:

127.0.0.1:6379> INFO server

Since authentication is not performed, the following error will occur.

NOAUTH Authentication required.

Now, run the following command to authenticate 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

Once completed, you can proceed to the next step.

Rename dangerous commands

It is also recommended to rename some dangerous commands in the Redis shell. One of the dangerous commands is “config”. Used to get the Redis password.

Let’s see how to retrieve the Redis password using the “config” command.

First, connect to Redis using the following command:

redis-cli

After connecting, use the following command to authenticate Redis:

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

Next, use the following command to retrieve the Redis password:

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 the Redis service using the following command:

systemctl restart redis

Now, use the following command to connect and verify Redis:

redis-cli127.0.0.1:6379> AUTH A&#9D3Fs4%#df

Now, use the “config” command to retrieve the Redis password:

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 follows:

127.0.0.1:6379> hitredis get requirepass

You should get the Redis password in the following output:

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

in conclusion

Congratulations! You have successfully installed and protected the Redis server on CentOS 8. You can now host the application and configure it to use Redis cache. If you have any questions, please feel free to ask me.

Sidebar