How to Install Redis on Ubuntu Linux

This post shows students and new users how to install and configure Redis on Ubuntu Linux. Redis is an in-memory key-value and data store commonly used as a database to store data structures such as strings, hashes, lists, sets, sorted sets with range queries, etc.

At a higher level of implementation, it can also provide high availability via Redis Sentinel, including monitoring, notifications, automatic failover, and partitioning across multiple Redis nodes.

If you want to speed up your dynamic applications or websites, using Redis to cache and return frequently requested items may help. Redis is commonly implemented with WordPress, Drupal, and other PHP-based dynamic web applications.

Also, for students and new users learning Linux, Ubuntu Linux is the easiest place to start learning. Ubuntu is the modern, open-source Linux operating system for desktops, servers, and other devices.

Ubuntu is a great Linux operating system for beginners.

To get started installing Redis on Ubuntu Linux, follow the steps below.

How to install Redis on Ubuntu Linux

This post assumes that your account on the server can run with administrative privileges. That means you can run the sudo process.

Redis is available in Ubuntu’s default repositories. However, the version that comes with Ubuntu might not necessarily be the latest. If you need to install Redis’s latest version, add its repository and install it there.

Installing Redis on Ubuntu is pretty straightforward. Simply run the commands below to install it on Ubuntu Linux.

sudo apt update
sudo apt install redis-server

After running the above commands, the Redis server should be installed and ready to use. The commands below can stop, start and enable Redis Server to start up every time the system boots up automatically.

sudo systemctl stop redis-server
sudo systemctl start redis-server
sudo systemctl enable redis-server

To verify if the server is running, run the commands below

sudo systemctl status redis-server

That should display the status of the Redis Server 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-11-29 09:02:30 CST; 15s ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
 Main PID: 26589 (redis-server)
    Tasks: 4 (limit: 4674)
   CGroup: /system.slice/redis-server.service
           └─26589 /usr/bin/redis-server 127.0.0.1:6379
...

This is how you know Redis is running

How to configure Redis on Ubuntu Linux

Now that the server is installed and verified use the steps below to configure remote login and adjust the Ubuntu firewall.

By default, Redis doesn’t allow access from remote locations ( access from different servers/clients). All access is restricted to the local host of the server it is installed on. ( example: 127.0.0.1).

Redis Server and its support applications run on a single server in most environments. No remote access is necessary since all the communications are done on a single host computer.

However, remote access will be required if the Redis server and applications are on separate hosts.

To allow remote access, open the Redis configuration file by running the commands below:

sudo nano /etc/redis/redis.conf

Then change the highlighted line as shown below. It is replacing 127.0.0.1 with all zeros ( 0.0.0.0 ) or restricting access to a specific host IP needing access to Redis.

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0 ::1

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.

Save the file and exit.

After making the changes to the file, restart the Redis service.

sudo systemctl restart redis-server

Verify that Redis is listening for all connections on 0.0.0.0 by running the commands below:

ss -an | grep 6379

It would be best if you got an output as shown below:

Output:
tcp  LISTEN 0      128       0.0.0.0:6379             0.0.0.0:*                                   
tcp  LISTEN 0      128      [::1]:6379                [::]:*

If you’re also running an Ubuntu firewall, simply add the policy below to allow all hosts on your subnet ( 192.168.0.0 ) access to the Redis server and port number.

sudo ufw allow proto tcp from 192.168.0.0/24 to any port 6379

That should do it.

To test if Redis hosted on IP address 192.168.0.2 is responding to remote hosts. Type the commands below from the remote server.

redis-cli -h 192.168.0.2 ping

The Redis server should respond with a pong

If you receive a correct reply, then all is done.

Conclusion:

  • Redis is a powerful in-memory data structure store that enhances the performance of dynamic applications and websites.
  • Installing and configuring Redis on Ubuntu Linux is straightforward and allows users to leverage its capabilities effectively.
  • It is essential to adjust Redis settings for remote access if the server and applications are on different hosts.
  • Always remember to use security measures such as protected mode to safeguard your Redis instance from unauthorized access.
  • Testing connectivity through the Redis CLI ensures that the configuration is successful and operational.
  • With Redis, users can improve application speed and efficiency, making it a valuable addition to their technology stack.

Frequently Asked Questions

What is Redis and why should I use it on Ubuntu?

Redis is an in-memory key-value data store that is commonly used as a database for caching and managing data structures. Using Redis on Ubuntu can significantly speed up dynamic applications and websites by caching frequently requested items.

How do I install Redis on Ubuntu?

To install Redis on Ubuntu, you can run the commands 'sudo apt update' followed by 'sudo apt install redis-server'. This will install Redis from Ubuntu's default repositories.

How can I check if Redis is running on my Ubuntu server?

You can check if Redis is running by executing the command 'sudo systemctl status redis-server'. This command will display the status of the Redis service, indicating whether it is active and running.

How do I configure Redis for remote access on Ubuntu?

By default, Redis restricts access to the local host. To configure Redis for remote access, you need to modify the Redis configuration file to allow connections from other servers and adjust your firewall settings accordingly.

What are the benefits of using Redis with PHP-based applications?

Using Redis with PHP-based applications like WordPress or Drupal can greatly enhance performance by caching data and reducing database load. This leads to faster response times and improved user experience on dynamic websites.

Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *