How to Install and Configure Redis on an Ubuntu VPS
Redis is an in-memory data store commonly used for application caching, session storage, queues, and other workloads requiring fast access to frequently requested data.
Unlike traditional disk-based databases, Redis primarily stores its working dataset in memory. This makes it useful for reducing database queries, improving application response times, and accelerating websites that repeatedly access the same information.
Redis is widely used with applications such as WordPress, WooCommerce, Laravel, and PHP-based websites.
In this guide, you will learn how to install and configure Redis on an Ubuntu VPS, secure access, configure memory limits, enable persistence, connect Redis to PHP applications, and troubleshoot common problems.
These instructions apply to Ubuntu 24.04 LTS, Ubuntu 26.04 LTS, and other supported Ubuntu releases.
Prerequisites
- An Ubuntu VPS with root or sudo access.
- SSH access to the server.
- At least 1 GB of RAM for basic testing; production requirements depend on the application and Redis dataset.
- Basic familiarity with Linux commands.
- Optional: An existing PHP, WordPress, or Laravel installation.
Looking for a VPS to host Redis and your applications? Explore Owned Networks KVM VPS hosting for full root access and flexible server resources.
1. What Is Redis and How Does It Work?
Redis is a high-performance data store that supports multiple data structures, including:
- Strings.
- Hashes.
- Lists.
- Sets.
- Sorted sets.
- Streams.
Applications can use Redis to store frequently accessed information in memory instead of repeatedly retrieving it from a database or recalculating it.
Common use cases include:
- Object caching: Reduce repeated database queries.
- Session storage: Store application session data.
- Application queues: Coordinate background jobs.
- Rate limiting: Track request counts and enforce limits.
- Temporary data: Store information with expiration times.
Important: Redis is not automatically a replacement for MariaDB, MySQL, or PostgreSQL. In many applications, Redis complements an existing database by providing a fast caching layer.
2. Update Ubuntu Packages
Update the package index:
sudo apt update
Review available package upgrades:
apt list --upgradable
For production servers, review pending upgrades and apply approved changes during an appropriate maintenance window.
3. Install Redis on Ubuntu
Install Redis using Ubuntu's package manager:
sudo apt install -y redis-server
Verify the installed Redis version:
redis-server --version
Check the Redis service:
sudo systemctl status redis-server --no-pager
If Redis is not running, start it:
sudo systemctl start redis-server
Enable Redis at system startup:
sudo systemctl enable redis-server
Note: The Redis version available through Ubuntu repositories depends on the operating system release and enabled package sources.
Redis licensing and distribution terms can also vary between upstream releases. Review the license applicable to your installed version when redistributing or embedding Redis in commercial software.
4. Test the Redis Installation
Redis includes a command-line client called redis-cli.
Test the local connection:
redis-cli ping
Expected output:
PONG
This confirms that the Redis server is responding to local requests.
You can also check the listening socket:
sudo ss -tlnp | grep 6379
Redis commonly uses TCP port 6379.
Security warning: Redis should not be exposed directly to the public internet. Keep it accessible only to trusted applications and administrators.
5. Locate the Redis Configuration File
On Ubuntu package installations, the main Redis configuration file is commonly located at:
/etc/redis/redis.conf
Before modifying Redis, create a timestamped configuration backup:
sudo cp -a /etc/redis/redis.conf \
"/etc/redis/redis.conf.backup.$(date +%Y%m%d-%H%M%S)"
List available backups:
sudo ls -lh /etc/redis/redis.conf.backup.*
Open the configuration file:
sudo nano /etc/redis/redis.conf
After making configuration changes, restart Redis during an appropriate maintenance window:
sudo systemctl restart redis-server
Important: Restarting Redis can interrupt application connections and may affect cached or queued data. Understand the application's Redis usage before restarting a production instance.
6. Secure Redis Network Access
For applications running on the same VPS, Redis should generally listen only on the loopback interface.
Open the configuration:
sudo nano /etc/redis/redis.conf
Locate the bind directive and configure:
bind 127.0.0.1 -::1
This binds Redis to IPv4 localhost and attempts IPv6 localhost when available.
Ensure protected mode remains enabled:
protected-mode yes
Verify the configured port:
port 6379
Restart Redis:
sudo systemctl restart redis-server
Check the listening address:
sudo ss -tlnp | grep 6379
Redis should listen on localhost rather than a public interface.
Important: Protected mode is an additional safeguard, not a replacement for network isolation, authentication, and appropriate firewall rules.
7. Configure Redis Authentication
Redis supports authentication and access control lists (ACLs).
Modern Redis deployments can use ACL users to control authentication and command permissions.
For a simple local deployment, you can configure a password for the default Redis user.
Open the configuration file:
sudo nano /etc/redis/redis.conf
Locate the requirepass directive and configure a strong, unique password:
requirepass REPLACE_WITH_A_LONG_RANDOM_PASSWORD
Replace the example value with a securely generated password.
Security recommendation: Avoid entering sensitive Redis passwords directly into shell commands, where they may appear in command history or process listings.
Protect the configuration file because it may contain authentication secrets:
sudo chown redis:redis /etc/redis/redis.conf
sudo chmod 640 /etc/redis/redis.conf
Before changing ownership or permissions on an existing installation, verify the package's expected configuration permissions and service account.
Restart Redis:
sudo systemctl restart redis-server
Connect interactively:
redis-cli
Authenticate:
AUTH REPLACE_WITH_A_LONG_RANDOM_PASSWORD
Then test:
PING
Expected response:
PONG
Exit:
QUIT
Important: Password authentication does not encrypt Redis network traffic. For remote connections, use private networking and TLS where supported.
For production applications requiring granular permissions, consider dedicated Redis ACL users instead of sharing the default user.
8. Configure Redis Memory Limits
Because Redis primarily stores data in RAM, memory limits are important for preventing excessive resource consumption.
Open the configuration:
sudo nano /etc/redis/redis.conf
Locate the maxmemory directive.
For example, on a VPS with 4 GB of RAM, you might allocate 1 GB to Redis:
maxmemory 1gb
This is an example, not a universal recommendation.
The appropriate limit depends on:
- Total VPS memory.
- Other running applications.
- Database memory requirements.
- Expected cache size.
- Redis persistence settings.
- Memory fragmentation and operational overhead.
Important: Redis can consume additional memory beyond its configured maxmemory value. Leave sufficient headroom for the operating system, applications, and persistence operations.
9. Configure Redis Eviction Policies
When Redis reaches its configured memory limit, the eviction policy determines how Redis handles additional writes.
For a dedicated caching instance, a commonly used policy is:
maxmemory-policy allkeys-lru
This allows Redis to evict keys using an approximate least-recently-used algorithm.
Other policies include:
noeviction— Reject writes that would exceed the memory limit.allkeys-lru— Evict approximately least-recently-used keys.allkeys-lfu— Evict approximately least-frequently-used keys.volatile-lru— Evict eligible keys with expiration times using an LRU approximation.volatile-ttl— Prefer eligible keys with shorter remaining expiration times.
Important: Do not use a cache-oriented eviction policy for Redis instances storing critical queues, sessions, or persistent application data without understanding the consequences of key eviction.
For applications that cannot tolerate data eviction, noeviction may be more appropriate, although writes can fail when the memory limit is reached.
10. Understand Redis Persistence
Redis supports persistence mechanisms that allow data to survive certain restarts.
The two primary mechanisms are:
- RDB snapshots: Periodically save the dataset to disk.
- AOF: Record write operations for later replay.
Persistence requirements depend on the Redis workload.
For a disposable object cache, persistence may be unnecessary.
For queues, sessions, or application data that must survive restarts, persistence requires careful planning.
Important: Redis persistence does not replace independent backups or guarantee that every acknowledged write will survive every failure scenario.
11. Configure RDB Snapshots
RDB persistence creates point-in-time snapshots of Redis data.
Open:
sudo nano /etc/redis/redis.conf
Example snapshot rules:
save 3600 1
save 300 100
save 60 10000
These rules request a snapshot when:
- At least one change occurs within 3600 seconds.
- At least 100 changes occur within 300 seconds.
- At least 10,000 changes occur within 60 seconds.
The exact persistence behavior depends on Redis version and configuration.
Check the configured snapshot filename:
dbfilename dump.rdb
Check the data directory:
dir /var/lib/redis
For applications requiring stronger durability guarantees, evaluate AOF persistence and appropriate backup procedures.
12. Configure Append-Only File Persistence
Redis can record write operations using its append-only file mechanism.
Open the configuration:
sudo nano /etc/redis/redis.conf
Enable AOF:
appendonly yes
Configure the synchronization policy:
appendfsync everysec
This commonly used setting balances durability and performance by requesting synchronization approximately once per second.
Important: AOF can increase disk writes and storage consumption. It also does not eliminate all potential data-loss scenarios.
Restart Redis during an approved maintenance window:
sudo systemctl restart redis-server
Verify persistence status using Redis commands after authenticating:
INFO persistence
13. Monitor Redis Memory Usage
Redis provides built-in statistics for memory consumption and operational activity.
Connect using:
redis-cli
Authenticate if required.
Display memory information:
INFO memory
Important metrics include:
used_memory— Memory allocated by Redis.used_memory_human— Human-readable memory usage.used_memory_rss— Resident memory reported by the operating system.maxmemory— Configured memory limit.mem_fragmentation_ratio— A memory fragmentation indicator.
To inspect general server statistics:
INFO stats
To inspect connected clients:
INFO clients
To inspect persistence:
INFO persistence
These metrics help identify memory pressure, cache effectiveness, and operational problems.
14. Test Redis Caching
Connect to Redis:
redis-cli
Authenticate if required.
Create a test key:
SET testkey "Owned Networks Redis Test"
Retrieve the value:
GET testkey
Expected response:
"Owned Networks Redis Test"
Configure an expiration time of 60 seconds:
EXPIRE testkey 60
Check the remaining expiration time:
TTL testkey
After expiration, the key will no longer be available.
This illustrates how applications can store temporary cached information.
15. Install Redis Support for PHP
PHP applications commonly use the PhpRedis extension to communicate with Redis.
Install the extension:
sudo apt install -y php-redis
Verify that the extension is available to the PHP command-line environment:
php -m | grep -i redis
Expected output:
redis
If your website uses PHP-FPM, restart the appropriate PHP-FPM service during a maintenance window.
For example:
sudo systemctl list-units --type=service \
| grep php
Identify the active PHP-FPM service before restarting it.
Important: PHP CLI and PHP-FPM can use different PHP versions or configurations. Verify that the Redis extension is enabled for the PHP runtime serving your website.
16. Use Redis with WordPress
Redis can improve WordPress performance by providing persistent object caching.
WordPress object caching stores frequently requested application objects and database query results in a reusable cache.
To use Redis object caching:
- Install and configure Redis on the VPS.
- Install the PHP Redis extension.
- Verify that WordPress can connect to Redis.
- Install a compatible Redis object caching plugin.
- Enable the object cache through the plugin's configuration interface.
- Verify cache hits and application functionality.
For example, a WordPress installation may use the Redis Object Cache plugin.
Depending on the plugin and deployment, additional configuration may be required in wp-config.php.
Important: Redis object caching is different from full-page caching. It reduces repeated application and database operations but does not replace an appropriate web caching strategy.
For WooCommerce, verify that caching does not interfere with carts, checkout sessions, or other customer-specific application data.
17. Use Redis with Laravel
Laravel supports Redis for caching, queues, and other application features.
A typical Laravel environment may include settings such as:
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=null
If Redis authentication is enabled, configure the appropriate password or ACL credentials using your application's supported secret management method.
Depending on the Laravel version and installed packages, Redis connectivity may use PhpRedis or another supported client.
Laravel can also use Redis for cache and queue backends.
Important: Redis configuration variable names and supported drivers can change between Laravel releases. Review the documentation for your installed Laravel version.
Do not enable cache eviction policies that could unexpectedly remove critical queued jobs.
18. Monitor Redis Performance
Redis includes several commands for monitoring server activity.
Display general server information:
INFO
Check connected clients:
CLIENT LIST
Inspect slow operations:
SLOWLOG GET 10
Review command statistics:
INFO commandstats
Check cache hits and misses:
INFO stats
Look for:
keyspace_hits
keyspace_misses
These values can help evaluate cache effectiveness.
However, cache hit ratios should be interpreted alongside application performance and workload characteristics.
19. Troubleshooting Common Redis Problems
Redis service fails to start:
sudo systemctl status redis-server --no-pager
sudo journalctl -u redis-server -n 100 --no-pager
Check the Redis configuration for invalid directives, duplicate settings, or unsupported options.
Redis connection refused:
sudo ss -tlnp | grep 6379
sudo systemctl status redis-server --no-pager
Verify that Redis is listening on the expected address and port.
Authentication required:
If Redis returns:
NOAUTH Authentication required.
Authenticate using the configured credentials before executing protected commands.
Redis reaches its memory limit:
Review:
INFO memory
CONFIG GET maxmemory
CONFIG GET maxmemory-policy
Investigate application caching behavior, expiration policies, memory fragmentation, and available VPS RAM.
Redis persistence errors:
Check disk space, file permissions, persistence status, and service logs.
df -h
sudo journalctl -u redis-server -n 100 --no-pager
PHP cannot connect to Redis:
Verify that the PHP Redis extension is installed for the correct PHP runtime and that the application uses the correct host, port, and authentication credentials.
20. Redis Security Best Practices
- Keep Redis and Ubuntu updated.
- Bind Redis to localhost whenever remote access is unnecessary.
- Do not expose TCP port 6379 directly to the public internet.
- Enable authentication or ACL-based access controls where appropriate.
- Use dedicated application credentials with limited permissions.
- Protect Redis configuration files and secrets.
- Configure memory limits appropriate for your VPS.
- Select an eviction policy suitable for your workload.
- Enable persistence when application data requires it.
- Monitor memory consumption, client connections, and slow operations.
- Maintain independent backups for important Redis data.
- Test application behavior after Redis configuration changes.
Remember: Redis is often deployed as a caching layer, but it can also store important application state. Its security, persistence, and memory settings must match the application's requirements.
Related Ubuntu VPS Tutorials
- How to Install PHP and PHP-FPM on an Ubuntu VPS
- How to Install and Secure MariaDB on an Ubuntu VPS
- How to Install and Secure PostgreSQL on an Ubuntu VPS
- How to Host WordPress on an Ubuntu VPS with Nginx and Free SSL
- How to Monitor CPU, RAM, Disk, and Network Usage on a Linux VPS
- How to Set Up Automatic Backups on an Ubuntu VPS Using Restic
Improve Application Performance with Owned Networks
Deploy Redis, WordPress, Laravel, databases, and other performance-sensitive applications on an Owned Networks KVM VPS with full root access and flexible server resources.
Combining Redis caching with appropriate application configuration, sufficient memory, and reliable VPS infrastructure can help reduce database load and improve application responsiveness.
Explore Owned Networks VPS Hosting and deploy your next Linux server today.