Home / How To / How to install Redis Server on CentOS 8

How to install Redis Server on CentOS 8



Redis is a free, open source memory key store that supports various 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 a wide range of programming languages ​​such as Java, PHP, C, Perl, Python, C #, Lua, Go and many more. It is a cross-platform 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 the CentOS 8 server.

Conditions

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

Install Redis Server

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

dnf install redis -y

After installing the Redis server, start the Redis service and allow it to restart at system startup 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 listens to port 6379. You can check this 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:*       

Then test the Redis connection 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 connection:

127.0.0.1:6379> ping

You should see the following output:

PONG

Configure Redis Cache

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

nano /etc/redis.conf

Add the following lines to 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 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 the /etc/redis.conf file:

nano /etc/redis.conf

Find the following line:

# requirepass foobared

And replace it with the following line:

requirepass A	D3Fs4%#df

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

systemctl restart redis

Now connect to Redis with the following command:

redis-cli

When connected, run the following command:

127.0.0.1:6379> INFO server

You get the following error because you do not authenticate.

NOAUTH Authentication required.

Now run the following command for authentication with the password specified in the Redis configuration file:

127.0.0.1:6379> AUTH A	D3Fs4%#df

You should see the following output:

OK

Now run the following command:Advertisement

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 done, you can move on to the next step.

Rename dangerous command

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

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

First connect to Redis with the following command:

redis-cli

Once connected, authenticate Redis with the following command:

127.0.0.1:6379> AUTH A	D3Fs4%#df

Then retrieve the Redis password with 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	D3Fs4%#df"

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

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 with the following command:

systemctl restart redis

Connect now and authenticate Redis with the following command:

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

Now retrieve the Redis password using the “config” command:

127.0.0.1:6379> config get requirepass

You get the following error:

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

Now run the renamed command shown below:

127.0.0.1:6379> hitredis get requirepass

You should get your Redis password in the following output:

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

Conclusion

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


Source link