Install Redis 7 on Ubuntu 24.04 when your app needs a fast cache, a session store, or a simple queue, and the database is doing work it should not do. Redis keeps data in memory, so reads that took milliseconds from disk now take microseconds, and your MySQL or PostgreSQL server gets room to breathe. This guide installs Redis 7 from the official repository, locks it down, and runs a first cache test.
Redis 7 is available in the Ubuntu 24.04 repositories, so there is no need to add a third-party repo for a standard install. The whole process takes about fifteen minutes, and most of that is testing. If you only need Redis for WordPress object caching, this guide to installing a Redis object cache on a WordPress VPS covers the plugin side once the server is up.
Install Redis 7 on Ubuntu 24.04: Step by Step
Update the package index and install the server package:
sudo apt update
sudo apt install -y redis-server
Ubuntu starts Redis automatically after install. Confirm the version and that it answers:
redis-cli --version
redis-cli ping
The first command should print a 7.x version, and the second should answer PONG. If ping returns an error, check the service status with sudo systemctl status redis-server and look at the last lines of /var/log/redis/redis-server.log before you change anything.
One surprise for new installs: Ubuntu ships Redis with supervised systemd and a configuration file at /etc/redis/redis.conf. That file is long, over a thousand lines, and most of it is comments. You only need to change a handful of lines, and the sections below walk through each one.
Lock It Down Before You Use It
A Redis server with no password and an open port is one of the most commonly exploited services on the internet. The default config already binds to localhost only, which is the safe starting point. Keep it that way unless you have a private network and a firewall rule for it. Then set a strong password:
sudo nano /etc/redis/redis.conf
Find the requirepass line, remove the comment marker, and set a long random password:
requirepass your-long-random-password-here
Generate the password with a password manager or openssl rand -hex 32. While you are in the file, confirm these three lines:
bind 127.0.0.1 ::1
protected-mode yes
port 6379
protected-mode yes refuses connections from outside localhost when no password is set, which is a second layer of protection. If your app runs on the same VPS, localhost binding is all you need. If the app runs on another server, put Redis behind a private network or a VPN, never on the public internet with only a password for protection.
Restart Redis after editing and test the password:
sudo systemctl restart redis-server
redis-cli -a 'your-long-random-password-here' ping
The answer should be PONG. Without the -a flag, the same command should now fail with an authentication error, which is exactly what you want.
Persistence: RDB, AOF, or Nothing
Redis is an in-memory store, so a restart wipes the data unless persistence is on. Ubuntu’s default enables RDB snapshots, which save the dataset to disk every so often. For a cache that is fine: if the data disappears, the app rebuilds it from the database. For sessions or queues, losing data hurts, so turn on AOF as well.
RDB snapshots are controlled by save lines in the config. The defaults save after 3600 seconds with one change, after 300 seconds with a hundred changes, and after 60 seconds with ten thousand changes. Leave them alone for a cache. For a session store, add AOF:
appendonly yes
appendfsync everysec
AOF logs every write, and everysec syncs the log to disk once per second. You can lose at most one second of writes, which is a fair trade for the speed. The always option syncs every write and is slower, so skip it unless your data is worth more than your latency budget.
One more setting matters on a small VPS: maxmemory. Without a limit, Redis grows until the kernel kills it or something else. Set a limit that leaves room for the rest of the stack. On a 2 GB VPS that also runs a web server and a database, 256 MB is a sane start:
maxmemory 256mb
maxmemory-policy allkeys-lru
The allkeys-lru policy evicts the least recently used keys when the limit is hit, which is the right behavior for a cache. For sessions, use volatile-lru instead, so only keys with an expiry get evicted. A managed option exists too: this comparison of managed Redis hosting for small projects shows when paying someone else to run Redis beats running it yourself.
Your First Cache Test with redis-cli
Test the basics before you wire an app to it. Set a key, read it back, and check the expiry:
redis-cli -a 'your-long-random-password-here' SET user:42:name "Firdaus"
redis-cli -a 'your-long-random-password-here' GET user:42:name
redis-cli -a 'your-long-random-password-here' SET session:abc "data" EX 3600
redis-cli -a 'your-long-random-password-here' TTL session:abc
The EX 3600 flag sets a one-hour expiry, and TTL reports the seconds left. Cache keys should always have an expiry. A cache without expiry is a memory leak with extra steps, and it is the most common reason a small Redis instance runs out of memory.
Check memory use and hit rate while testing:
redis-cli -a 'your-long-random-password-here' INFO memory
redis-cli -a 'your-long-random-password-here' INFO stats
INFO memory shows used_memory_human and maxmemory, so you can see how close you are to the limit. INFO stats shows keyspace_hits and keyspace_misses. Divide hits by hits plus misses to get the hit rate. A cache hit rate under 80 percent usually means the app caches the wrong things or the keys expire too fast.
Connect Your App
Most apps connect with a client library, and the connection details are the same everywhere: host 127.0.0.1, port 6379, and the password you set. A PHP app using Predis or PhpRedis connects like this:
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('your-long-random-password-here');
A Python app with redis-py uses a connection string instead:
redis://:[email protected]:6379/0
Keep the password in an environment variable or a config file outside the web root, never in code you commit to git. If you run WordPress, the Redis object cache plugin reads the same details from wp-config.php, and the object cache guide linked above shows the exact constants.
If your app and Redis live on different servers, use a private network address in the bind line and open port 6379 only to that network in your firewall. Test the connection from the app server before you change the app config, or you will debug two things at once.
When Redis Is the Wrong Tool
Redis is fast, but it is not a database replacement. Do not store your only copy of important data in Redis, even with AOF on. A cache can be rebuilt, a queue can be replayed, but a lost order or a lost user record cannot be invented back. Keep the source of truth in MySQL or PostgreSQL and treat Redis as a speed layer on top.
Also think twice before using Redis as your only session store on a single VPS. If Redis restarts and every user gets logged out, that is a minor annoyance for a blog and a real problem for a shop. Either accept it, set up a second Redis for failover, or keep sessions in the database and use Redis only for caching.
If you are choosing between Redis and Memcached for WordPress, the short version is that Redis does more: persistence, data structures, and pub/sub, while Memcached is simpler and uses less memory for a plain cache. This Redis versus Memcached comparison for WordPress object caching walks through the trade-offs in detail. For most VPS owners, Redis 7 is the safer default, and now you have it installed, secured, and tested.