Manual Installation of Redis 6.0 on CentOS 7

Publish: 2020-10-29 | Modify: 2020-10-30

Redis is completely open source and follows the BSD license. It is a high-performance key-value database that is currently very popular and widely used. This article records the method of manually installing the Redis service on CentOS 7.

Redis

Installing Redis

First, execute the following command to install a bunch of dependencies:

# Install dependencies
yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make
# Upgrade gcc
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

At the time of writing this article, the latest stable version of Redis is 6.0. The version may change over time, so please visit the Redis official website https://redis.io/download to download the latest version.

# Download Redis
wget https://download.redis.io/releases/redis-6.0.9.tar.gz
# Unpack Redis
tar xzf redis-6.0.9.tar.gz
# Enter the Redis directory
cd redis-6.0.9
# Compile
make

After successful compilation, the Redis service binary file is located at src/redis-server. You can directly run the Redis service by entering this path. However, it will run in the foreground, so once it ends or the window is closed, the Redis service will stop as well.

Running Redis

To facilitate future management and maintenance, you can move the Redis src directory to a specific location, such as mv src/ /usr/local/redis.

You can also make a copy of the redis.conf file in the redis-6.0.9 directory:

cp redis.conf /etc

By default, Redis runs in the foreground. If you need to run it in the background, you need to modify the redis.conf configuration file. Change

daemonize no

to

daemonize yes

Then enter the command /usr/local/redis/redis-server /etc/redis.conf to restart the Redis service. Now it will run in the background. You can see the process using the ps command:

[root@hecs-centos-7 ~]# ps -ef|grep 'redis'
root     10217     1  0 22:00 ?        00:00:00 /usr/local/redis/redis-server 127.0.0.1:6379

Setting Environment Variables

Entering the Redis absolute path every time to run it may be inconvenient. You can add the Redis path to the Linux environment variables by appending it to the bottom of the /etc/profile file:

export PATH=$PATH:/usr/local/redis

Then enter the command source /etc/profile to make it effective. Now you can directly execute Redis commands without entering the full path. For example:

[root@hecs-centos-7 ~]# redis-server -v
Redis server v=6.0.9 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=ef93b08070de4db5

Redis Client

After compilation, Redis comes with a command-line client called redis-cli. You can check if Redis is running normally by entering the following command:

[root@hecs-centos-7 ~]# /usr/local/redis/redis-cli 
127.0.0.1:6379> ping
PONG

Register as Systemd Service

If you want to set Redis to start automatically, you can register Redis as a Systemd service for easy management in the future. First, create a service file: vi /etc/systemd/system/redis.service, with the following content:

[Unit]
Description=Redis Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis/redis-server /etc/redis.conf

[Install]
WantedBy=multi-user.target

Then enter systemctl daemon-reload to reload the service. Now you can use the systemctl command to manage it:

# Start Redis
systemctl start redis
# Stop Redis
systemctl stop redis
# Enable auto-start on boot
systemctl enable redis

If you want to learn more about Systemd services, you can refer to: Linux Systemd Service Writing Practice

Conclusion

Redis has many configuration parameters, but this article does not go into detail. For more usage instructions, it is recommended to refer to the official documentation. Although tools like Baota, LNMP, and Oneinstack provide easy installation of Redis, it is still necessary to understand the manual installation method for future maintenance.

Redis Official Website: https://redis.io/

This article references:


Comments