Installing Redis with a Ubuntu 16.04 VPS Server
Introduction
What is Redis exactly? It is an open-source (BSD licensed, Redis[1]), in-memory data structure store, used as DB (database), cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.
Prequisuites
- Ubuntu 16 server from vpsserver.com. You can order one at vpsserver.com/plans[2] if you don't have one yet.
- SSH-client used to login and connect to your vps server.
- Knowledge in linux command line utility and Linux basic.
Update & Build Dependencies
Since Redis, like any other Ubuntu packages require some dependencies we will have to download and install those dependencies before we can actually use Redis.
Some dependencies Redis require are build-essential and tcl. Below are the commands to install the above packages:
suo apt-get update
sudo apt-get install build-essential tcl curl make
Once the required packages are ready we can now install Redis.
Installing Redis
To install Redis we will need to get the latest package from Redis website using curl. Type the below command and execute:
curl -O http://download.redis.io/redis-stable.tar.gz
Unpack the stable version:
tar xzvf redis-stable.tar.gz
Extract the Redis source files:
cd redis-stable
Compile the binaries first, entering:
make
Once the compilation is done we can now install Redis on our server:
make install
Configuring Redis
Redis is ready we can now configure for using it.
Let us create a configuration directory where Redis-configuration files can be stored. Let us create it on /etc/redis.:
sudo mkdir /etc/redis
Copy the sample Redis-configuration file included in the Redis-source to the new directory we just created:
sudo cp /tmp/redis-stable/redis.conf /etc/redis
Afterwards, let us open the configuration file so we can add some configuration statements:
sudo nano /etc/redis/redis.conf
In the configuration file please find the supervised directive. Currently, this directive is set to no. Since we are running a system that uses systemd init we will need to update this to reflect systemd.
# 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.
Creating Redis Unit File
After installing and configuring Redis, we will need to create a systemd unit file so the init system can manage the Redis process.
Let us create and open the redis.service file by doing the below command:
sudo nano /etc/systemd/system/redis.service
Once inside we will need to copy paste the below details and add description and requirements for networking:
[Unit]
Description=Redis In-Memory Data Store
After=network.target
Next add a [Service] section, from here we we will specify the services' behavior. For security, we should use a dedicated user and group which we will call redisUser. Copy the below and add it at the bottom of [Unit] Section we added from above.
[Service]
User=redisUser
Group=redisUser
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
Finally, add the [Install] section in the service file. This is where we will define the systemd target that the service should attach to.
[Install]
WantedBy=multi-user.target
Once done, save and close the file.
Creating Redis User, Group and Directories
Next, we will create a Redis user, group and directories before we can actually start Redis.
Let us begin by adding redisUser user and group using the below command:
sudo adduser --system --group --no-create-home redisUser
Afterwards, let us create our /var/lib/redis directory by:
sudo mkdir /var/lib/redis
Then let us give redisUser user and group ownership of the above directory:
sudo chown redisUser:redisUser /var/lib/redis
Let us adjust our permissions so we will have no problem accessing this directory:
sudo chmod 770 /var/lib/redis
Starting and Testing Redis
Now that Redis is ready and configured for use we can now start and test our Redis installation.
To start Redis let us execute the below command:
sudo systemctl start redis
To check if Redis has no errors on startup let us execute the below line:
sudo systemctl status redis
You should see something like below:
Output
â redis.service - Redis Server
Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2016-11-01 20:12:07 EDT; 1min 2s ago
Process: 2165 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=0/SUCCESS)
Main PID: 2166 (redis-server)
Tasks: 3 (limit: 512)
Memory: 752.0K
CPU: 125ms
CGroup: /system.slice/redis.service
ââ2166 /usr/local/bin/redis-server 127.0.0.1:6379
. . .
To test Redis functionality we can run Redis command line client by typing and running:
redis-cli
In the prompt, let us run:
127.0.0.1:6379> ping
You should have:
output
PONG
Now, let us check if we can set a key by running:
127.0.0.1:6379> set test "Hello!"
Our output should be:
output
OK
To get the value that we just stored:
127.0.0.1:6379> get test
Our output should be:
output
"Hello!"
Now let us exit Redis cli and restart Redis instance:
127.0.0.1:6379> exit
sudo systemctl restart redis
Now, let us connect to our Redis client back and confirm if our test values are still available or present:
redis-cli
To get the "test value:
127.0.0.1:6379> get test
Our output should still be:
output
"Hello!"
Running Redis at Boot
Once all our test are working we can now enable Redis to start at boot time. To do this, just run:
sudo systemctl enable redis
Now that Redis is up and running we can now use it to store values from our application. On our next article we will discuss on how to secure Redis for use in a production environment.
For suggestions and comments please write it down below. We do greatly appreciate your feedback.
[1]: https://redis.io/
[2]: https://www.vpsserver.com/plans/
Related Tutorials
Setup and Configuration of Strongswan & Accel-PPP on Ubuntu 14.04 64bitCreating a VPS from VPSServer.com
Installing and Configuring Wordpress on an Ubuntu 14.04 64bit Server
Install LAMP (Linux Apache, Mysql Server and Php) on an Ubuntu 14.04 64bit VPS Server
Installing MetaTrader on Windows 2008 Server