Introduction
Redis is one of the best memory key value store. There are a lot of other store also available in market such as ArangoDB and Hbase.
But:
Redis is the highly rated memory key value store for it’s performance and language support.
Here, in this tutorial, we are going to see how to install redis on ubuntu 16.04 server.
Requirements
- You will need a sudo non root user configured on Ubuntu 16.04 server. If you dont have one, follow this guide to create one.
- Get 50$ Credit on Vulture Clouds Here and Test the Instruction for free.
Install Redis Ubuntu
Here, We are going to install latest version of Redis. For that, we will download the package directly from the source and install it on our server.
We have to install build-essential meta-package and tcl package before installing redis.
We can accomplish the task by executing the below command. First, update the package index.
$ sudo apt-get update $ sudo apt-get install build-essential tcl
Download the Source Code of Redis
First move to /tmp directory.
$ cd /tmp
Then, Use the below command to download the redis.
$ curl -O http://download.redis.io/redis-stable.tar.gz
Then, extract the file using tar command.
$ tar xzvf redis-stable.tar.gz
Then, switch to the unpacked redis folder.
$ cd redis-stable
Compile and Install Redis
Here, we have to compile the redis binaries using the make command.
$ make
Then, test whether those binaries are combined properly.
$ make test
Then, install the binaries using the next command.
$ sudo make install
After that, you have to configure the Redis.
Configuring the Redis
Once you install the redis, you have to create the configuration directory for redis. You can do that using the below command.
$ sudo mkdir /etc/redis
This command will create the Redis directory and you have to move the redis configuration file from tmp directory to the newly created directory. Here is the command for that.
$ sudo cp /tmp/redis-stable/redis.conf /etc/redis
Now, we have to make some changes in the redis configuration file. Open redis file using the nano editor.
$ sudo nano /etc/redis/redis.conf
Go through the file and find the supervised directive and it would be set to no.
Change that to systemd.
/etc/redis/redis.conf . . . # 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 . . .
After that, you have to find the dir directory and set it to /var/lib/redis.
This will let the redis store the persistent data in a location which is not viewable by ordinary user.
Here is how you make the changes.
. . . # 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 . . .
After that, save and close the file.
Systemd Unit File Creation for Redis
Now, open the redis.service file using nano editor.
$ sudo nano /etc/systemd/system/redis.service
Inside the file, add the below lines.
[Unit] Description=Redis In-Memory Data Store After=network.target
Here, we are going to add few important lines in the file which will tell server about how our server will work with the system.
We will not run redis on root user. rather we will use a new user called redis and redis group. Then, we wil define how redis has to start and what it should do to recover from errors.
Also, we will mention how to shutdown redis.
[Unit] Description=Redis In-Memory 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
Then, add this command also. It will locate the systemd target.
/etc/systemd/system/redis.service [Unit] Description=Redis In-Memory 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
After that, save and close the file.
Creating The Redis User
Now, you have to create the redis user and group. Here is the command for that.
$ sudo adduser --system --group --no-create-home redis
After that, create var/lib/redis directory using the below command.
$ sudo mkdir /var/lib/redis
Now, set the ownership of the directory to Redis user and group.
$ sudo chown redis:redis /var/lib/redis
Then, add access restriction by changing the permission for the directory.
$ sudo chmod 770 /var/lib/redis
Now, Start the Redis
$ sudo systemctl start redis
Then, check the Redis using the below command.
$ sudo systemctl status redis
You should get the following output.
redis.service - Redis Server Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2017-03-12 11:20:07 EDT; 5min 32s ago Process: 3115 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=0/SUCCESS) Main PID: 3124 (redis-server) Tasks: 3 (limit: 512) Memory: 864.0K CPU: 179ms CGroup: /system.slice/redis.service └─3124 /usr/local/bin/redis-server 127.0.0.1:6379
There should not be any error.
Now, start the redis server to test it.
$ redis-cli
Then, a prompt will open. In that type ping
ping
You should get the following as an output.
PONG
Then, type this line again.
set test "It's working!"
You will see output as OK.
OK
Again, type get test
get test
Now, the redis will return the previously input string.
"It's working!"
Then type exit and press enter to get out of the redis server environment.
exit
Now, you have to make the redis to initiate at system boot. Here is the command for that.
$ sudo systemctl enable redis
You should be getting the following output.
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.
Thats all. You have perfectly configured redis on your server.
Conclusion
Handling the key value store is little bit tricky. Your base system should be perfect and running.
Make sure, subscribe for the upcoming tutorials to get notified.
Leave comments, if you have any issues in executing the commands or following the steps.