X
  1. Home
  2. Developer tools
  3. How do I get started with Redis via socket in Laravel?

How do I get started with Redis via socket in Laravel?

This guide is intended for advanced users.

These instructions are written specifically for Laravel 9. They may not work for older versions of Laravel.

We offer Redis on all our web hosting accounts and Agency services. We run Redis via socket on our servers, thus Laravel needs to be reconfigured to use it for e.g. queue workers (as Laravel usually expects Redis to be run on a specific port).

To set up a Laravel worker, please refer to these instructions.

  1. Begin by entering that the Predis Composer package is required by the project. I.e., run the following command in the root of your project:
    composer require predis/predis
    This is needed since the PECL version of phpredis cannot be used to access Redis via socket.
  2. Under the Redis settings in the config/database.php file of the project, add the following rows for scheme and path:
'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'),
             'scheme' => env('REDIS_SCHEME', 'tcp'),
             'path' => env('REDIS_PATH', ''),
         ],
'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'),
             'scheme' => env('REDIS_SCHEME', 'tcp'),
             'path' => env('REDIS_PATH', ''),
         ],
  1. In your .env file for the project, replace all lines beginning with REDIS_* with the following:
REDIS_CLIENT=predis
REDIS_SCHEME=unix
REDIS_PATH=/tmp/redis
REDIS_PASSWORD=null
REDIS_PORT=0
  1. To use Redis for queue management, update QUEUE_CONNECTION to redis according to the default settings for Redis in Laravel. You can set redis as both CACHE_DRIVER and SESSION_DRIVER.
  2. Clear the cache for all settings by running php artisan config:cache. This will apply the settings according to the instructions above.

If anything is added to the queue, Redis will manage it from now on.

Was this article helpful?

Related Articles