top of page

🚀 How to Install Redis on Ubuntu + Redis Configuration and Common Commands

  • Foto del escritor: PixelHost
    PixelHost
  • 14 ago 2024
  • 4 Min. de lectura

Developers often look for systems that can boost the speed and performance of their projects. One popular system for this purpose is Redis, an open-source, in-memory database used as a cache and message broker. It’s also known as a data structure server.

What makes Redis unique compared to relational database systems is its ability to store high-level data types like maps, lists, and sets. Redis also offers an easy-to-use interface, atomic data manipulation, and exceptional performance.

In this tutorial, we’ll explain how to install Redis on Ubuntu 18.04, 20.04, and 22.04, as well as any necessary configurations to ensure it works correctly.


🌟 What Makes Redis Useful?

Redis stands out for its exceptional performance and features, making it superior to traditional database systems. Some popular use cases include:

  • Cache: Redis's enhanced ability to persist data on disk makes it a superior alternative to traditional caching solutions.

  • Queues: The system can be used to enqueue background jobs.

  • Counters: Redis allows for simple creation and deployment of counters without needing to read data or update the database. Counters in Redis will remain consistent.

  • Publish and Subscribe: Users can easily distribute data using the Pub/Sub paradigm.


🛠️ How to Install Redis on Ubuntu in 4 Steps

To install and configure the Redis server on Ubuntu, you'll need a Virtual Private Server (VPS) already running with the Ubuntu operating system. With all prerequisites in place, connect via SSH and start the installation.

1. Update the APT Repository

Redis is already included in Ubuntu’s official package repository. However, it’s recommended to frequently update the APT repository to get the most recent version.

2. Install Redis Server on Ubuntu

Installing Redis is as simple as using the appropriate command with sudo privileges.

Press "y" and then Enter to continue.

3. Verify the Redis Version

Once Redis is installed, you can verify if the installation was successful by checking the installed version.

The output will show the Redis server version installed on your machine.

4. Start the Redis Service

After installation is complete, it’s recommended to check if the Redis instance is running. To test connectivity, use the appropriate command.

Look for the line indicating "Active: active (running)."

If Redis has not started and the status is inactive, you can enable the Redis client by entering the necessary command.


🔧 How to Find and Edit the Redis Configuration File on Ubuntu

The default Redis configuration file is located in the /etc/redis/redis.conf directory. By default, the Redis server listens to all available connections.

You can make it listen to interfaces of your choice using the bind configuration directive, followed by one or more IP addresses.

To instruct the Redis server to listen on a specific IP, edit the /etc/redis/redis.conf file. Open it with your preferred editor, in this case, using nano.

Locate the line "bind 127.0.0.1 ::1".

Change the IP address by entering the values for the connections you want the Redis server to listen to. Here’s an example:

For multiple IP addresses, simply separate them with a space:

Enter the IP addresses of your network here.

However, if you want the server to listen on all network interfaces, you can comment out the bind line completely:

Once you’ve finished making changes, save and close the file. Then, restart the Redis service to apply the changes.


📝 Using Common Redis Commands

There are several groups of commands in Redis, including:

  • String commands

  • List commands

  • Set commands

  • Hash commands

  • Sorted set commands

  • Pub/Sub commands

Here are some of the commands used in the Redis prompt:

  • redis-server /path/redis.conf: Starts Redis with a specific configuration file.

  • redis-cli: A command to run the Redis CLI client.

  • APPEND key value: Appends a value to a key.

  • BITCOUNT key [start end]: Counts the number of bits set in a string.

  • SET key value: Sets a value to a key.

  • EXPIRE key 120: Expires a key in 120 seconds.

  • INCR key: Increments the value of a key.

  • KEYS pattern: Finds all keys matching a specific pattern.

  • DEL key: Deletes a key.

  • STRLEN key: Gets the length of a key.

  • MSET key value [key value …]: Sets multiple keys and values.

  • MGET key [key …]: Gets values of multiple keys.

  • GETSET key value: Sets a new value, returning the old value.

  • INCRBY key increment: Increases the count of a key.

  • DECRBY key increment: Decreases the count of a key.


🔒 Renaming Dangerous Commands (Optional)

A common practice to secure Redis is to rename or disable potentially unsafe commands. These commands are dangerous because any unauthorized user could use them to manipulate or even destroy all the database data.

Note that this process is completely optional, and you can decide whether to rename, disable, or leave the command active. To start, open the /etc/redis/redis.conf file with your preferred editor. We’ll use nano in this example.

Next, find the SECURITY section where you can rename or disable a command. In our example, we’re renaming the FLUSHALL, SHUTDOWN, and DEL commands to CANTSEE_FLUSHALL, CANTGUESS_SHUTDOWN, CANTHEAR_DEL.

We are also completely disabling the DEBUG and CONFIG commands.

Other potentially unsafe commands include RENAME, SAVE, SREM, FLUSHDB, PEXPIRE, and BGSAVE.

To test everything, restart the Redis service.

Then, log in to the Redis command-line client.

To test a disabled command, simply try using it. For example, testing the DEBUG command should result in an error since this command has been disabled.

Next, test the renamed command. In our case, it’s FLUSHALL.

As you can see, the FLUSHALL command no longer works, while our renamed CANTSEE_FLUSHALL works perfectly.


📚 Learn More About Databases on Ubuntu

Check out our article to learn more about how to install PostgreSQL on Ubuntu for database management.


🎯 Conclusion

The Redis database server is one of the most popular alternatives to relational databases like MySQL. With Redis, users can expect high performance and speed, especially for high-traffic websites.

In this tutorial, we’ve covered the main aspects of Redis and why it’s so valuable.

We’ve also reviewed the installation process on Ubuntu 18.04, 20.04, and 22.04.

Finally, we’ve shown some useful Redis commands and how to secure Redis by renaming unsafe commands.

We hope you found this tutorial helpful. If you have any questions or comments, feel free to leave them below.


Best Regards,

João @PixelHost


 
 
 

Comments


bottom of page