Optimization

How to install and configure Redis on a Linux VPS

You just got a fresh Linux VPS with Ubuntu 24.04, and the app you are about to deploy needs a fast key-value store for caching and queues. Installing Redis on a Linux VPS is a twenty-minute job, but doing it wrong ends with an open port on the internet and a database that loses data on reboot. This guide takes you through the install, the systemd service, a mandatory password, persistence and the firewall rules, with a verify step at every stage.

  • Use the official PPA instead of the distro package. The redis-server package in Ubuntu 24.04 is maintained, but the Redis team PPA ships the newest stable branch with fixes you actually want.
  • Bind to 127.0.0.1 unless you have a reason not to. Then you do not need a password for local apps, and nothing is exposed externally.
  • Turn on AOF persistence for anything you cannot rebuild. For pure cache, RDB snapshots are enough.
  • Open the firewall only if Redis must be reached remotely, and use a strong password when you do.

These points carry through the whole walkthrough, so keep them in mind as you run the commands.

Prerequisites

  • An Ubuntu 24.04 VPS with a non-root sudo user. All commands below assume you are that user.
  • A VPS with at least 1 GB of RAM for a single instance. Redis is lightweight, but the OS and your app need room too.
  • SSH access to the machine. If you are starting from zero, run through the SSH-first-time guide before this one.
  • No other service already listening on port 6379. Check with ss -tlnp | grep 6379.

Why run Redis on a VPS at all

Redis sits between your application and your database, or between your users and your application, and it makes both faster. It keeps hot data in RAM, serves it in microseconds, and takes the load off PostgreSQL or MariaDB. On a single VPS, the setup is trivial: one service, one config file, one port, and you can point every app on the box at localhost:6379.

The other common use is a job queue. With BRPOPLPUSH or a list you treat as a queue, a worker process can pull tasks reliably, and Redis guarantees the handoff even if the worker dies mid-task. That pattern needs only a few MB of RAM and works happily on a 2GB VPS alongside your web server.

For a single-server deployment, running Redis on the same box is the normal choice. It avoids network round trips and keeps the attack surface small because the socket never leaves the machine. Later, if you scale, you can move Redis to its own host and change one IP in your app config.

Step 1: Install Redis from the official PPA

The default Ubuntu 24.04 repository ships Redis 7.0.6, which works, but the Redis team PPA gives you the current stable branch with the latest patches. Add the PPA, update the package list, then install redis-server.

sudo add-apt-repository ppa:redis/redis -y
sudo apt update
sudo apt install redis-server -y

After the install, confirm the version and that the binary is on your PATH.

redis-server --version

Expected output shows a line like Redis server v=8.0.x sha=... with the current stable release. If the command errors, the package did not install correctly, restart with the apt install step.

The PPA is signed and maintained by the Redis team, so you are not trusting a random third-party build. That matters on a production Linux VPS where a compromised package would be a real incident.

Step 2: Verify the service is running

Ubuntu starts redis-server automatically through systemd once the package is installed. Check that it is actually running before you touch any config.

systemctl status redis-server

You are looking for active (running) in the output. If the service failed, inspect the log with journalctl -u redis-server --no-pager -n 30 and fix whatever it reports before moving on.

Now test the connection with the Redis CLI before you change anything.

redis-cli ping

The expected reply is PONG. That confirms the server is up, the socket is reachable, and no authentication is required yet, which you are about to change.

Step 3: Bind Redis to localhost and enable the firewall

By default, Redis 7 and 8 bind to 127.0.0.1, which is exactly what you want on a single-VPS setup. Do not change that unless you truly need remote access. If you leave it bound to 0.0.0.0 with no password, anyone who scans the internet can connect to your cache and write arbitrary keys.

Ubuntu 24.04 ships with ufw disabled. Turn it on and allow only SSH, which also protects Redis by blocking the port entirely from the outside.

sudo ufw allow OpenSSH
sudo ufw enable

Confirm the rules with sudo ufw status. You should see OpenSSH listed as allowed and the firewall state as active. Redis port 6379 is not in the list, which is what you want for a localhost-only setup.

If you ever need to access Redis from another machine, you will add a rule explicitly and set a strong password, which is the next step. Do not open 6379 to the world on a hunch.

Step 4: Set a Redis password even on localhost

A password on a localhost-bound service seems like paranoia until a vulnerable web app on the same box becomes an SSRF vector. An attacker who can make your server issue HTTP requests can reach Redis on 127.0.0.1, and without a password they can flush your entire database. Set a password now and avoid that class of incident entirely.

Generate a strong one with openssl, then put it in the config file.

openssl rand -base64 32

Copy the output, then edit the Redis config.

sudo nano /etc/redis/redis.conf

Find the requirepass line, which is commented out by default, and change it to your generated password.

requirepass Y9kQ2mT7xLpR4vB8nC1sD5fG6hJ0zW3q

Restart Redis and test that the password is enforced.

sudo systemctl restart redis-server
redis-cli ping

You should see (error) NOAUTH Authentication required. Then authenticate and ping again.

redis-cli -a 'Y9kQ2mT7xLpR4vB8nC1sD5fG6hJ0zW3q' ping

The reply is PONG. Note the warning Redis prints about passing the password on the command line. For interactive use that is fine, but put the password in your app config or use a config file, never in a shell history.

If your app reads this Redis instance, update its connection string now, otherwise it will fail to authenticate and you will spend an hour debugging a null cache.

Step 5: Configure persistence for restarts and crashes

Redis keeps everything in RAM, and RAM is wiped on reboot. The save directives in the config create RDB snapshots at intervals, and the appendonly option writes every write to an AOF log for near-durable persistence. For a pure cache, RDB alone is fine, because losing a few seconds of cache data only costs a cache rebuild. For queues or session stores, turn on AOF.

Edit the config again and find the appendonly directive.

sudo nano /etc/redis/redis.conf

Change the line from appendonly no to appendonly yes, and set the fsync policy to everysec, which is the recommended balance between durability and performance.

appendonly yes
appendfsync everysec

If you are running low on disk, keep an eye on the AOF size. The rewrite mechanism in Redis compacts it automatically, but on a busy instance the file can grow briefly. On a VPS with NVMe storage this is rarely a problem.

Restart Redis and confirm the AOF file exists.

sudo systemctl restart redis-server
ls -l /var/lib/redis/appendonlydir/

You should see an appendonly.aof.1.base.rdb file or similar. That file is your durability guarantee. Test a restart to be sure the data survives.

redis-cli -a 'Y9kQ2mT7xLpR4vB8nC1sD5fG6hJ0zW3q' set testkey "hello"
sudo systemctl restart redis-server
redis-cli -a 'Y9kQ2mT7xLpR4vB8nC1sD5fG6hJ0zW3q' get testkey

The reply should be hello. If it returns nil, persistence is not working, check the AOF directory permissions and the Redis log.

Step 6: Move Redis to systemd, not init.d

On Ubuntu 24.04 the package already registers Redis as a systemd service, which gives you systemctl start, enable, restart and automatic start on boot. Verify that boot-start is enabled so a VPS reboot does not leave your app without a cache.

systemctl is-enabled redis-server

The expected output is enabled. If it says disabled, run sudo systemctl enable redis-server and confirm again.

For the most common tuning, set maxmemory so Redis never consumes all RAM and OOMs the box. Edit the config, find the maxmemory line, and set it to a safe fraction of your VPS RAM, leaving room for your app and the OS.

maxmemory 256mb
maxmemory-policy allkeys-lru

For a 2GB VPS, 256MB is a reasonable cache size. For a larger box with an 8GB VPS, you can go up to 2GB, but always leave headroom for the rest of the stack. The allkeys-lru policy evicts the least recently used keys when the limit is hit, which is the right behavior for a cache.

Restart and confirm the settings took effect.

sudo systemctl restart redis-server
redis-cli -a 'Y9kQ2mT7xLpR4vB8nC1sD5fG6hJ0zW3q' CONFIG GET maxmemory

The reply shows the value you set, in bytes: 268435456 for 256MB. If it returns the default 0, your config edit did not apply, recheck the file path and the restart.

SettingRecommended valueWhy
bind127.0.0.1Keep Redis off the public network
requirepassStrong generated passwordProtect against SSRF and local attacks
appendonlyyesNear-durable persistence for queues and sessions
appendfsynceverysecBalance durability and write performance
maxmemory256MB on a 2GB VPSPrevent OOM kills on the whole box
maxmemory-policyallkeys-lruEvict least recently used keys as a cache

Troubleshooting common failures

Three things go wrong most often after an install. The first is systemctl status redis-server showing a failed state right after the PPA install. Run journalctl -u redis-server --no-pager -n 20 and look for a permission error on the data directory. Fix it with sudo chown -R redis:redis /var/lib/redis and restart.

The second is your app getting NOAUTH Authentication required after you set a password. That is not a Redis failure, it is your app missing the password in its connection config. Update the app, do not remove the password. The third is Redis losing all data on reboot despite AOF being enabled. Check that appendonly yes is actually in the config file with redis-cli CONFIG GET appendonly and that the file is yes, then verify the directory permissions.

If a connect call times out from another machine, Redis is either still bound to localhost or the firewall blocks the port. Check ss -tlnp | grep 6379 to see the bind address, and sudo ufw status to see the firewall rules.

FAQ

How do I check which Redis version is installed on my VPS?

Run redis-server --version. With the Redis team PPA on Ubuntu 24.04, you will see the current stable branch version, which as of 2026 is in the 8.0 series. The distro package would show 7.0.6.

Is it safe to run Redis without a password if it is bound to localhost?

No. Any web application on the same VPS can be used to reach 127.0.0.1:6379 through an SSRF vulnerability. With no password, an attacker can flush your cache or overwrite keys. Set a password even for localhost.

Does Redis survive a VPS reboot with AOF enabled?

Yes. With appendonly yes and the default everysec fsync, Redis replays the AOF log on startup and restores your data within one second of the last write. Test it with a save, a reboot and a get on the test key.

What happens if Redis hits the maxmemory limit?

With maxmemory-policy allkeys-lru, Redis evicts the least recently used keys to stay under the limit. If no keys can be evicted because they are all protected, Redis returns errors for writes instead of accepting them.

Do I need to open port 6379 in ufw for a local Redis install?

No. If your apps run on the same VPS, they reach Redis through the localhost interface and the firewall does not filter that. Only open 6379 for remote access, and then bind Redis to a specific interface, not 0.0.0.0, and use a strong password.

Related articles

在 Linux VPS 上安装 Redis 的要点

在 Ubuntu 24.04 VPS 上安装 Redis 时,使用官方 PPA 获取最新稳定版,绑定到 127.0.0.1 避免暴露公网,并设置强密码防止 SSRF 攻击。开启 AOF 持久化确保重启后数据不丢失,同时用 systemd 管理服务。对于 2GB 内存的 VPS,建议将 maxmemory 设为 256MB 并使用 allkeys-lru 淘汰策略,防止内存耗尽导致系统崩溃。

Note: This guide is for general reference. Every system and infrastructure has its own specifics, so test each step in a safe environment and consult a qualified engineer before applying it in production.