This guide provides step-by-step instructions for installing and configuring Redis on an Ubuntu 22.04 server.
Redis, short for Remote Dictionary Server, is a versatile, open-source, in-memory data structure store that can be used as a database, cache, and message broker. It is known for its high performance, flexibility, and support for a wide range of data structures, making it a popular choice for developers and system architects.
Redis stores data in memory, which allows for extremely fast read and write operations. This makes it ideal for applications that require low latency and high throughput. Despite being an in-memory store, Redis provides several persistence options to ensure data durability. These include RDB snapshots, which save the dataset at specified intervals, and AOF (Append-Only File), which logs every write operation.
Prerequisites
- A Ubuntu 22.04 installed dedicated server or KVM VPS.
- A root user access or normal user with administrative previlieges.
Step 1: Update Package Repository
First, ensure your package repository is up to date:
sudo apt update
Step 2: Install Redis Server
Install Redis server package from the default Ubuntu repositories:
sudo apt install redis-server
Once installed, Redis should start automatically. You can verify it by checking its status:
sudo systemctl status redis-server
Step 3: Configure Redis
The primary configuration file for Redis is located at /etc/redis/redis.conf
. Open it with a text editor:
sudo nano /etc/redis/redis.conf
Secure Redis with a password. To add a password for Redis, find the line that starts with # requirepass
and uncomment it by removing the #, then set your desired password:
requirepass your_password_here
Bind Redis to a Specific IP Address
By default, Redis binds to 127.0.0.1
(localhost). If you want Redis to be accessible from a specific IP address, find the bind line and modify it:
bind 127.0.0.1 192.168.1.100
Replace 192.168.1.100 with the IP address you want Redis to bind to. To allow connections from any IP address, you can use 0.0.0.0, but this is not recommended for production due to security reasons.
Set memory limits. To set a maximum amount of memory that Redis can use, find the maxmemory directive and set it to your desired value:
maxmemory 256mb
You can also define a policy for what Redis should do when it reaches the memory limit. For example, to use the least recently used (LRU) policy:
maxmemory-policy allkeys-lru
Enable persistence. Redis offers two persistence options: RDB (snapshotting) and AOF (Append Only File). You can enable one or both.
Enabling RDB Persistence:
RDB persistence performs point-in-time snapshots of your dataset at specified intervals. To configure RDB, look for the save directives and set the intervals:
save 900 1
save 300 10
save 60 10000
This means Redis will save the dataset:
- Every 900 seconds (15 minutes) if at least 1 key changed
- Every 300 seconds (5 minutes) if at least 10 keys changed
- Every 60 seconds (1 minute) if at least 10,000 keys changed
Enabling AOF Persistence:
AOF logs every write operation received by the server. This ensures a higher level of durability at the cost of larger file sizes and potentially slower performance. To enable AOF, find and set:
appendonly yes
You can also configure the AOF file rewrite policy:
cappendfsync everysec
This setting tells Redis to synchronize the AOF file to disk every second.
Configure logging, To configure logging, look for the logfile directive. By default, Redis logs to the /var/log/redis/redis-server.log
file. You can change the log file location if needed:
logfile /var/log/redis/redis.log
Save and Exit
After making the necessary changes, save the file and exit the text editor. For nano
, you can do this by pressing CTRL + X, then Y, and finally ENTER.
For the changes to take effect, restart the Redis server:
sudo systemctl restart redis-server
Step 4: Verify Configuration
Check if Redis is running with the new configuration:
redis-cli
If you set a password, authenticate with:
AUTH your_password_here
Then, you can check if the server responds:
ping
If everything is configured correctly, Redis should respond with PONG
.
Step 5: Adjust Firewall (if needed)
If your firewall is active, you may need to allow Redis connections. By default, Redis listens on port 6379. Use ufw (Uncomplicated Firewall) if it's installed:
sudo ufw allow 6379
That's it! Redis should now be installed and configured on your Ubuntu 22.04 server. Adjust configurations based on your specific use case requirements.