Install and Configure Redis on Ubuntu with Simple Steps
Install and Configure Redis on Ubuntu means setting up Redis, an open source in memory data store, on your Ubuntu server. First install Redis using the apt command then enable and start the Redis service. Check the status to confirm Redis is running. Use the redis-cli tool to test your installation.
🤖AI Overview:
Install and Configure Redis on Ubuntu refers to the process of setting up the Redis database server on an Ubuntu operating system and adjusting its settings for optimal performance. The main goal is to ensure that Redis runs smoothly on your system and is ready to handle data storage and retrieval tasks. This process typically includes installing the Redis software, enabling it to start automatically, and modifying configuration files as needed. Following these steps allows Ubuntu users to work efficiently with Redis for their data management needs.
Prerequisites to Install Redis on Ubuntu
To let this tutorial works correctly, provide the options below and move on.
- A system running Ubuntu.
- A non-root user with
sudo
privileges. - Access to a terminal window/command line.
- A firewall configured with
ufw
.
Tutorial Install and Configure Redis on Ubuntu
When you are done with the prerequisites, you are ready to start the process to install and configure Redis on Ubuntu. This guide works on Ubuntu 18.04 and Ubuntu 20.04. Let’s get started to review all the required steps to have a Redis server on your Ubuntu.
Step 1: Install Redis on Ubuntu
To install Redis on Ubuntu, run the command below to update the APT repository. Redis is included in the official package of Ubuntu, but you are recommended to start the installation process by updating the package repository.
sudo apt update
Now, use the following command to install Redis on Ubuntu and download its dependencies.
sudo apt install redis-server
When you are prompted, press Y and press Enter to move on.
Also, you can run the command below to check the installation status by viewing the Redis Version.
redis-cli --version
Clearly, you will see the version of the installed Redis server on your system in the output. To check if the Redis instance is running, type:
sudo systemctl status redis
When you see the “Active: active (running)” line in your output, it means installing Redis on Ubuntu is successfully completed, and you can continue to configure it on your Ubuntu.
Step 2: Configure Redis on Ubuntu
Install and Configure Redis on Ubuntu requires 4 main steps and one optional step that will be explained. Once you make sure the installation step is done, you can start to configure Redis. You need to modify the Redis configuration file. So, open your favorite text editor and do this. Here we use nano.
sudo nano /etc/redis/redis.conf
Then, find the supervised
directive. This line is set tono
by default. Since you are running Ubuntu, set the supervised
directive tosystemd
(Ubuntu’s init system) to manage Redis as a service.
/etc/redis/redis.conf
. . .
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd
. . .
You just need to save the changes and close the file. To do this press CTRL + X, Y, and then ENTER if you are using nano.
Finally, run the command below to restart the Redis service:
sudo systemctl restart redis.service
Step 3: Check Redis Installation on Ubuntu
When you are done with installing and configuring Redis, let’s verify whether the recent steps are passed correctly or not. Testing the status of the service running and connecting to the server is a reasonable idea. You can start by checking Redis’s service status to ensure that Redis is functioning as you planned.
Use the following command to check if the Redis service is running:
sudo systemctl status redis
Once your output displays that the service is active and running, Redis is set to start up every time the server boots. However, if you prefer to start up Redis manually every time the server boots, run the command below to configure Redis for that.
sudo systemctl disable redis
Also, you can check the Redis connection. So, to verify the connection with the Redis server, type:
redis-cli
Next, it moves you to the redis-cli command prompt. Run the following command to test the connectivity.
ping
Output:
PONG
The above output confirms that the server connection is still alive.
Now, you can test setting up Key-value pairs. Redis is an in-memory key-value NoSQL database. So, you can test if it retrieves assigned values based on the specified key. To do this, start with setting a key.
set key1 "You have successfully set up a key-value pair!"
As you see, the key is labeled as key 1 which has the value of “You have successfully set up a key-value pair!”.
when you press Enter, you see the prompt responds with OK. Then, use the command below to check if you have assigned the given value to the key correctly.
get key1
If the output displays the message you attached as the value, you’re all set, and you can exit out of the Redis shell.
quit
Step 4: Secure Redis on Ubuntu
In the middle of the guide on how to install and configure Redis on Ubuntu, you reviewed the required steps to install Redis on Ubuntu and set up it. It is time to set up Redis authentication. While Redis includes an authentication feature as an additional security layer, you need to enable the feature since it is inactive by default. To activate the authentication feature, modify the configuration file.
Open the Redis configuration file for editing:
sudo nano /etc/redis/redis.conf
Then, locate the requirepass
directive under the SECURITY section and uncomment it. Replace your desired password with foobared
after uncommenting the line. When you are finished, save and close the file. Again, you need to restart the Redis service:
sudo systemctl restart redis.service
After enabling the authentication feature of Redis, you will not be able to do any query without providing the required password. So, if you face the message of: (error) NOAUTH Authentication required
in your output, provide the password defined in the configuration file to continue working in Redis.
Just run the following command to wait for the output to respond with OK.
auth [your_password]
Installing Redis on Ubuntu with the recent steps makes Redis accessible from localhost. To restrict connections to localhost (if you have changed the default setting), open the Redis configuration file for editing:
sudo nano /etc/redis/redis.conf
Then, scroll down and find the NETWORK section in the file. Locate this line and make sure it is uncommented (remove the # if it exists):
/etc/redis/redis.conf
. . .
bind 127.0.0.1 ::1
. . .
Save and close the file when finished and restart the service by running:
sudo systemctl restart redis
Also, you can use the following command to verify that the modification is now in effect.
sudo netstat -lnp | grep redis
Now, you can make sure that you have managed to bind Redis to localhost, restricting access to other connections.
Step 5: Rename Dangerous Commands (Optional)
The last step to complete Install and Configure Redis on Ubuntu is optional, but it is a security feature built into Redis that you can use to protect your data. To restrict normal users from using commands that would harm your system, you can use the useful security feature of renaming or killing dangerous commands.
Firstly, let’s see some of the commands that are considered dangerous:
FLUSHDB
,FLUSHALL
,KEYS
,PEXPIRE
,DEL
,CONFIG
,SHUTDOWN
,BGREWRITEAOF
,BGSAVE
,SAVE
,SPOP
,SREM
,RENAME
, and DEBUG
.
Depending on your individual requirements or those of your site, you may choose to disable or rename a command. You can disable a command if you are certain that you will never use it. If not, renaming it might be in your best interests. Open the Redis configuration file to disable or rename your desired command:
sudo nano /etc/redis/redis.conf
Locate the SECURITY section and scroll down to the #Command renaming
line.
For example, to rename the SHUTDOWN
command, add the line:
rename-command SHUTDOWN [new_command_name]
In this way, the mentioned command will be renamed to a new command which a normal user would not be able to guess.
You can rename the dangerous command into an empty string to remove certain commands and kill them. So, to disable the SHUTDOWN
command, add the line:
rename-command SHUTDOWN ""
And finally, save the changes when you are finished with making the changes and restart the Redis service. I guess you agree with me that it was easy to Install and Configure Redis on Ubuntu especially if you follow the above steps properly.
Conclusion
You have now learned how to install and configure Redis on Ubuntu. Following these best practices helps provide a secure and reliable environment for Redis to operate. Always keep your system updated and regularly review your configuration to follow the latest security recommendations.
FAQ
2. How do I install Redis on Ubuntu for the first time?
To install Redis on Ubuntu, you can use the official package manager. Begin by updating your package list with sudo apt update, then install Redis with sudo apt install redis-server. This process ensures you receive the latest available version.
3. What are the basic configuration steps after installing Redis on Ubuntu?
After you install Redis, you should review its configuration file, usually found at /etc/redis/redis.conf. Key settings to check include the default port, binding address for security, and persistence options. Restart the Redis service after making changes by running sudo systemctl restart redis-server.
4. How do I test if Redis is running correctly on Ubuntu?
You can check the status of your Redis service with sudo systemctl status redis-server. To interact with Redis, use the redis-cli command and enter ping. If Redis is running, it will respond with PONG.
5. Is Redis secure by default on Ubuntu, and what basic steps can I take to enhance its security?
The default Redis installation is not fully secure for production environments. After installation, configure Redis to listen only on localhost or a specific internal IP address. You should also set a strong password in the configuration file and enable protected mode for added security.
6. Can I configure Redis to start automatically on Ubuntu startup?
Yes, Redis can start automatically when your Ubuntu server boots. The installation process sets up Redis as a systemd service. You can confirm this by running sudo systemctl enable redis-server, ensuring Redis starts on system restart.
7. How do I manage and monitor the performance of Redis on Ubuntu?
You can use Redis built-in commands such as INFO and MONITOR to review statistics and performance. Additionally, there are third-party tools and monitoring solutions to help you analyze Redis usage and optimize its configuration for your needs.
8. What are some common issues when installing or configuring Redis on Ubuntu?
Common issues include incorrect binding addresses, permission errors, and port conflicts. Ensure you are running commands with appropriate privileges and that your firewall settings allow traffic on the Redis port if necessary.
9. How can I upgrade Redis to the latest version on Ubuntu?
To upgrade Redis, first update your package list with sudo apt update and then run sudo apt upgrade redis-server. Always back up your data and configuration files before performing any upgrade.
10. Where can I find further documentation or support for Redis on Ubuntu?
Official documentation can be found on the Redis website and Ubuntu’s help pages. For community support, forums such as Stack Overflow or the Redis community forums are excellent resources to get assistance and tips.