Step-by-Step Guide
- Update the Package Index
Open a terminal and update the package index to ensure you have the latest information about available packages:
sudo apt update
- Install Redis
Install Redis using theapt
package manager:
sudo apt install redis-server
- Configure Redis
Open the Redis configuration file for editing:
sudo nano /etc/redis/redis.conf
Make the following changes for better security and performance:
- Set a password: Uncomment the
requirepass
directive and set a strong password.conf requirepass your_password
- Bind to specific IP: By default, Redis listens on all available IP addresses. To restrict access, change the
bind
directive to your server’s IP address.conf bind 127.0.0.1
Save and close the file.
- Enable Redis to Start on Boot
Enable the Redis service to start at boot time:
sudo systemctl enable redis-server
- Start Redis
Start the Redis service:
sudo systemctl start redis-server
- Verify Redis Installation
Check the Redis service status to ensure it is running:
sudo systemctl status redis-server
If Redis is running, you should see an active (running) status.
- Test Redis Installation
To test the Redis installation, use the Redis CLI to connect to the Redis server:
redis-cli
If you set a password, authenticate with:
AUTH your_password
Once connected, you can test Redis by setting and getting a key:
SET mykey "Hello, Redis!"
GET mykey
You should see the response “Hello, Redis!”.
- Secure Redis (Optional)
For additional security, consider configuring a firewall to restrict access to the Redis server. For example, if using UFW, allow access only from trusted IP addresses:
sudo ufw allow from trusted_ip to any port 6379
- Enable Persistence (Optional)
Redis supports two forms of persistence: RDB snapshots and AOF (Append Only File). To enable RDB snapshots, ensure the following lines are present in the Redis configuration file:
save 900 1
save 300 10
save 60 10000
To enable AOF, uncomment and set the appendonly
directive to yes:
appendonly yes
- Monitor Redis
You can monitor Redis performance and statistics using theredis-cli
with theINFO
command:bash redis-cli INFO