In this tutorial, we are going to build and install Redis on our CentOS 7 Cloud VPS from source.
Redis is an in-memory data structure store primarily used as a database and cache. Redis supports different kinds of abstract data structures such as strings, lists, maps, sets, sorted sets, hyper logs, bitmaps, and spatial indexes. The Redis package that is included in the default CentOS repositories is pretty outdated, so installing from the source code will ensure that we have the latest version of Redis installed on our system. Let’s begin with the installation.
Step 1. Install Dependencies
Connect to your CentOS server via SSH and upgrade the installed packages to the newest versions by typing:
yum update
Install the pre-requisite packages necessary for building Redis using the following yum command:
yum install gcc make wget tcl
Once the packages are installed, you can move on to the next step.
Step 2. Install Redis 5.0 on CentOS 7
At the time of writing this article, the latest stable version of Redis was 5.0.0.
The installation process is quite an easy task, start by navigating to the /usr/local/src/
directory:
cd /usr/local/src
Download the Redis archive by running the following wget
command:
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
Once the download has completed, extract the downloaded file with the following tar
command:
tar xf redis-5.0.0.tar.gz
Navigate to the Redis source directory:
cd redis-5.0.0
Start the compilation process using the make
command:
make
The build process may take some time depending on your server resources, once completed the following message will be printed on your screen:
Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory `/usr/local/src/redis-5.0.0/src'
Now, run the make test
command to make sure everything was built correctly:
make test
The next step is to install Redis binaries using the make install
command:
make install
Redis has been successfully installed. Now, let’s configure it.
Step 3. Creating a Redis User
The following commands will create a group and user named “redis”:
groupadd redis adduser --system -g redis --no-create-home redis
Next create the /var/lib/redis
directory which will store the redis databases:
mkdir -p /var/lib/redis
Give the redis user and group ownership over the directory:
chown redis: /var/lib/redis
Set the correct permissions with:
chmod 770 /var/lib/redis
Step 4. Creating Redis Configuration
Now that Redis is installed and intial setup has been completed on your CentOS 7 VPS, you’ll need to create the configuration file.
First, create the /etc/redis
directory with:
mkdir /etc/redis
Next copy the default configuration file by running:
cp /usr/local/src/redis-5.0.0/redis.conf /etc/redis/
Open the file with your preferred text editor (we’ll be using nano
):
nano /etc/redis/redis.conf
Search for the supervised directive and change it to “systemd”:
# If you run Redis from upstart or systemd, Redis can interact with your # supervision tree. Options: # supervised no - no supervision interaction # supervised upstart - signal upstart by putting Redis into SIGSTOP mode # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET # supervised auto - detect upstart or systemd method based on # UPSTART_JOB or NOTIFY_SOCKET environment variables # Note: these supervision methods only signal "process is ready." # They do not enable continuous liveness pings back to your supervisor. supervised systemd
Search for the dir directive and set it to /var/lib/redis
:
# The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. dir /var/lib/redis
Once done, save and close the file.
Step 5. Creating a Redis Systemd Unit File
Now that Redis is installed and configured, the last step is to create a systemd unit file so that you can manage the Redis servis as a regular systemd service.
Open your text editor and create the follwing file:
nano /etc/systemd/system/redis.service
Then add the following content to it:
[Unit] Description=Redis Data Store After=network.target [Service] User=redis Group=redis ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown Restart=always [Install] WantedBy=multi-user.target
Save and close the file. Enable and start the Redis service with:
systemctl enable redis systemctl start redis
To check that the Redis service has no errors, run:
systemctl status redis
The output should look like this:
● redis.service - Redis Data Store Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2018-11-12 04:39:32 CST; 5s ago Main PID: 9788 (redis-server) CGroup: /system.slice/redis.service └─9788 /usr/local/bin/redis-server 127.0.0.1:6379
Finally, to verify that the Redis service is functioning correctly, run the following command:
redis-cli PING
The output should look like:
PING
And that’s it! You now have a working Redis instance that’s configured and ready to be used. Reading the Redis documentation is a good way to start learning how Redis works, as well as how to configure it more in-depth for your specific application.
Of course, you don’t have to install Redis on CentOS 7 if you use one of our Managed Cloud VPS Hosting services, in which case you can simply ask our expert Linux admins to install and configure Redis on CentOS 7 for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post on how to install and configure Redis on CentOS 7, or if you found it helpful, please share it with your friends on the social networks using the sharing shortcuts, or simply leave a comment in the comments section. Thanks.
Very good tutorial,
Thanks
This tutorial was very complete, Thank you so much
Please guide me to install Redis 6. Thanks
Did you try to download Redis 6.0.0 at http://download.redis.io/releases/redis-6.0.0.tar.gz ?