Setting Up Redis on a VPS for Laravel

Dr. Adam Nielsen
2 min readAug 27, 2024

--

Redis is a powerful in-memory data store often used for caching and queuing in Laravel applications. In this guide, we will walk through the steps to install Redis on a virtual private server, configure it in a Laravel project, and ensure it’s correctly set up.

Step 1: Install Redis on Your Server

First, connect to your server via SSH. Then, update your package list and install Redis:

sudo apt-get update
sudo apt-get install redis-server

After installation, Redis should start automatically. To ensure Redis is running:

sudo systemctl status redis-server

Step 2: Configure Redis for Laravel

1. Update Your .env File

Laravel needs to know how to connect to Redis. Update your .env file to include Redis configuration settings:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_DB=0
REDIS_CACHE_DB=1

These are all default values, so by default, it should also work without setting them explicitly.

2. Laravel Configuration Files

Laravel’s config/database.php file should reflect your Redis configuration. This is the default configuration, so you should not have to change it:

'redis' => [

'client' => env('REDIS_CLIENT', 'phpredis'),

'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_database_'),
],

'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],

'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],

],

Step 3: Test Redis Setup

After configuring Redis, it’s crucial to test that everything is working as expected.

1. Set and Retrieve a Cache Value

Use Laravel Tinker to test caching:

php artisan tinker

Inside Tinker:

Cache::put('test_key', 'test_value', 600); // Cache for 10 minutes
$value = Cache::get('test_key');

2. Check Redis Directly

To ensure that Redis is storing the keys in the correct database, you can use the Redis CLI:

redis-cli

Switch to the cache database (usually 1):

SELECT 1

Now, list the keys:

KEYS '*'

You should see keys like:

your_app_name_database:test_key

Retrieve the value:

GET your_app_name_database:test_key

This should return:

"test_value"

Step 4: Ensure Redis Starts on Boot

Redis should be configured to start on server boot. This is managed by systemd, which controls how services are started, stopped, and restarted.

To ensure Redis is enabled on boot:

sudo systemctl enable redis-server

You don’t have to do any changes on deployment. Redis will remain active.

Conclusion

Setting up Redis on a vhosted server for a Laravel application enhances performance by providing efficient caching. With the steps above, you’ve installed Redis, configured it in Laravel, tested its functionality, and ensured it starts automatically on boot.

--

--