How to set up Redis on Ubuntu!
Intro
Redis is software used for database or caching that stores a key value running in-memory. Click for more details
Requirements
We need a machine with Ubuntu 16.04 installed for a non-root user with sudo authority in order to have Redis working in your hand.
Pre Setup and Test
I prefer to set up Redis from the source code. If you want to set it up via OS package manager or Homebrew I'll add those later.
First of all, we need to install some packages.
build-essentialtcl
As we always do, we get updates first.
sudo apt-get update
sudo apt-get install build-essential tclDownload
I prefer to download the source code to /tmp.
cd /tmp
curl -O http://download.redis.io/redis-stable.tar.gz
tar xzvf redis-stable.tar.gzCompile and Setup
make
make test
sudo make installConfiguration
After the setup, we need to configure Redis. To save the configuration files, we need to create /etc/redis a directory. To do this;
sudo mkdir /etc/redisCopy the sample configuration files to the directory we just created.
sudo cp /tmp/redis-stable/redis.conf /etc/redisOpen and start editing the file.
In Configuration File;
We need to changecode supervised no to supervised systemd Because Ubuntu using systemd init.
Also, change dir ./ todir /var/lib/redis. This line indicates where permanent data is stored. Save and close the configuration file.
Creating Systemd Unit File
We need to edit redis.service file like the one below and then save it. (Typical service definition)
sudo nano /etc/systemd/system/redis.service[Unit]
Description=Redis standalone
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.targetRedis User/Group/Path Definitions
To create a user;
sudo adduser --system --group --no-create-home redisTo create a directory;
sudo mkdir /var/lib/redisTo create ownership;
sudo chown redis:redis /var/lib/redisTo change write permission;
sudo chmod 770 /var/lib/redisStarting Redis and Checking the Status
To start;
sudo systemctl start redisTo status check;
sudo systemctl status redisWhen you execute the last command, you need to see the green "Active: active (running)" message.
Test with CLI
Redis CLI is a client tool to communicate with the Redis server. To execute;
redis-clilet's ping it;
127.0.0.1:6379> pingresponse;
PONGLet's continue with the data test.
127.0.0.1:6379> set test "Habil"Output is;
OKGive it back to me;
127.0.0.1:6379> get testResponse
"Habil"Starting Redis Service on Boot-up
$ sudo systemctl enable redisThe End
We currently have a fully working Redis installation in our operating system. If you wish, you can take a look at official documents.