In this article, we are going to perform, how to install Redis on Ubuntu 18.04 and 16.04 LTS. Before we proceed further, let me give you brief idea about redis.
Table of Contents
Introduction
Redis is an open source tools, which is used as a database and acts as in-memory data structure store, cache and message broker. Message brokers are building block of message-orianted-middleware(MOM), which transfers, route, respond, invoke and interact with sender/receiver end. It supports different data structures such as string, lists, sorted sets with range queries and etc. In redis, we can configure single data base structure and replication (Master-Slave) too.
Prerequisites
- Ubuntu 18.04/16.04 LTS
- 2 CPU’s, 3GB RAM
- SSH access with sudo privileges
- Open Firewall Port: 6379
Step 1: Install Redis on Ubuntu
Let’s install redis by using below command.
$ sudo apt-get install redis-server
Next enable Redis to start on System boot. Enter below command,
$ sudo systemctl enable redis-server.service
Output:
Synchronizing state of redis-server.service with SysV init with /lib/systemd/systemd-sysv-install... Executing /lib/systemd/systemd-sysv-install enable redis-server
To check the status of redis service.
$ sudo systemctl status redis.service
output:
redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2019-08-09 14:17:55 UTC; 3 days ago Docs: http://redis.io/documentation, man:redis-server(1) Main PID: 11843 (redis-server) Tasks: 3 Memory: 11.8M CPU: 2min 46.628s CGroup: /system.slice/redis-server.service └─11843 /usr/bin/redis-server 0.0.0.0:6379
To check redis version, enter below command
$ redis-cli -v
output:
redis-cli 3.0.6
After confirming that redis is running. Let’s run below command.
$ redis-cli 127.0.0.1:6379> ping PONG
If you get the output as “PONG”. Then its working fine.
Step 2: Configure Redis
Default configuration is located in /etc/redis/redis.conf. Now we will change the configuration to access redis from remote servers, defaultly it has access only to localhost. To enable remote access uncomment and change the bindIP as below.
#bind 127.0.0.1
bind 0.0.0.0
1. Redis as daemon
By default redis does not run as daemon and protection mode is enabled, edit the /etc/redis/redis.conf file and change to daemonize as yes, protected-mode to yes
daemonize yes
protected-mode yes
To take effect of above configuration, restart the redis-server.
$ sudo systemctl restart redis.service
To check redis is listening on port 6379, use below command:
$ netstat -tulpn | grep LISTEN
output:
tcp6 0 0 :::6379 :::* LISTEN -
2. Test Redis using CLI
$ redis-cli
127.0.0.1:6379> ping PONG
3. To set the value
127.0.0.1:6379> SET <key> <value>
For example:
127.0.0.1:6379> SET 'URL' https://wwww.fosstechnix.com
output:
127.0.0.1:6379> GET 'URL' "https://wwww.fosstechnix.com"
4.To check keys
127.0.0.1:6379> keys *
Step 3: Secure Redis
By default redis is not secured. In the configuration file /etc/redis/redis.conf, go to SECURITY section and enable authentication
1. Enable Authentication
############ SECURITY ################# #requirepass foobared
Uncomment the above and change password from foobared to <your password> and restart redis-server.
requirepass fosstechnix
$ sudo systemctl restart redis.service
Now let’s test the new configuration. Open redis-cli
$ redis-cli PING
output:
(error) NOAUTH Authentication required.
Now use AUTH and provide your password.
127.0.0.1:6379> AUTH <your password>
127.0.0.1:6379> AUTH fosstechnix
If authentication is successful the output will be OK.
Step 4: Start, Restart, Stop Redis Server
Use below commands to start,restart and stop redis server using command line,
$ sudo systemctl start redis.server
$ sudo systemctl restart redis.server
$ sudo systemctl stop redis.server
To learn more about Redis. Please refer official documentation.
Conclusion
In this article, we have installed redis server on Ubuntu 18.04/16.04 LTS, configured bind-ip and daemonized redis. To protect our data from external users we enabled protection-mode. We created keys, secured the data by setting AUTH and mentioned commands to start,restart and stop redis server using command line.
Related Articles
How to Install Redis on CentOS 7