How To Install And Configure Redis Server In Ubuntu
Redis is a free, open source key-value database. It is similar to memcached but the dataset is not volatile and other datatypes (such as lists and sets) are natively supported. Redis comes with redis-cli which provides a simple command-line interface to the Redis server. This tutorial walks you through how to install Redis and configure it in Ubuntu. I tested this guide on Ubuntu 18.04 LTS server, however this guide might work on other Ubuntu/Debian-based distros.
Install Redis Server in Ubuntu
Redis Server is available in the default repositories of Ubuntu. So, you can install it using the apt-get package manager.
Run the following command to install Redis Server on your Ubuntu system:
$ sudo apt-get install redis-server
After installing, check if Redis Server service is start or not with command:
$ sudo systemctl status redis-server
Sample output would be:
● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-06-02 12:40:26 IST; 6s ago Docs: http://redis.io/documentation, man:redis-server(1) Main PID: 2623 (redis-server) Tasks: 4 (limit: 2322) CGroup: /system.slice/redis-server.service └─2623 /usr/bin/redis-server 127.0.0.1:6379 Jun 02 12:40:26 ubuntuserver systemd: Starting Advanced key-value store... Jun 02 12:40:26 ubuntuserver systemd: redis-server.service: Can't open PID file /var/run/red Jun 02 12:40:26 ubuntuserver systemd: Started Advanced key-value store.
Just in case, if it is not started already, you can start it as shown below.
$ sudo systemctl enable redis-server
$ sudo systemctl start redis-server
To check the installed version of Redis Server, just run:
$ redis-server -v
Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=1bc80a08306a3efd
Configure Redis Server
The default configuration file of Redis is /etc/redis/redis.conf. By default Redis listens for connections from all the network interfaces available on the server. It is also possible make it to listen to just one or multiple interfaces using the “bind” configuration directive, followed by one or more IP addresses.
To make Redis server to listen to a particular IP address, edit /etc/redis/redis.conf file:
$ sudo vi /etc/redis/redis.conf
Find the following line:
Change it as shown below.
To make it listen to multiple IP addresses, then change it like below.
bind 192.168.43.2 192.168.43.3
Replace the IP addresses with your own values. Then, save and close the file.
Restart Redis service to apply the changes.
$ sudo systemctl restart redis-server
Test Redis Server
Connect to the Redis Server using redis-cli from command line.
Now, you’re connected to your Redis server.
From here, you can run Redis commands to test its functionality. Let us see a simple example.
127.0.0.1:6379> set test "Welcome To OSTechNix" OK 127.0.0.1:6379> get test "Welcome To OSTechNix" 127.0.0.1:6379>
To quit from Redis command prompt, type exit.
To learn more about Redis usage, refer the its official documentation page.