1

I'm trying to configure the Redis config file in Laravel 8.75.0 (for cache purposes) to use TLS with a certificate authority certificate. At the moment, my config file looks like this (based on this answer: https://stackoverflow.com/a/48876398/10031033):

'options' => [
        'cluster' => env('REDIS_CLUSTER', 'redis'),
        'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
        'ssl' => ['cafile' => env('REDIS_CACHE_SSL_CA', null), 'verify_peer' => env('REDIS_CACHE_SSL_VerifyCA', false)]
    ],

    'cache' => [
        'scheme' => env('REDIS_CACHE_SCHEME', 'tcp'),
        'host' => env('REDIS_CACHE_HOST', '127.0.0.1'),
        'password' => env('REDIS_CACHE_PASSWORD', null),
        'port' => env('REDIS_CACHE_PORT', 6379),
        //  'database' => env('REDIS_CACHE_DB', 1),
    ],

I'm sure that the variables work and the connection works as the following config works perfectly.

        $vm = array(
        'host'     => env('REDIS_CACHE_HOST'),
        'port'     => env('REDIS_CACHE_PORT'),
        'timeout' => 0.8, 
        'scheme' => env('REDIS_CACHE_SCHEME'),
        'ssl'    => ['cafile' => env('REDIS_CACHE_SSL_CA'), 'verify_peer' => env('REDIS_CACHE_SSL_VerifyCA')],
    );

    $redis = new Client($vm);
    $redis->auth(env('REDIS_CACHE_PASSWORD'));
    try {
        echo $redis->ping();
    } catch (\Exception $e) {
     //   dd($e);
        echo 'not';
    }

Does anybody know what I'm doing wrong here? I've been trying for a couple of hours but can't seem to get it to work.

Thank you

  • It's not clear where you put your configuration. Options are meant to be in the config/database.php file as per https://laravel.com/docs/8.x/redis#configuration – apokryfos Jan 22 '22 at 13:51
  • Hello @apokryfos, yes, it's indeed stored in the config/database, apologies if that was unclear. The code that i'm trying to use it with is: Cache::get('key') for example – ilovecookies Jan 22 '22 at 13:57
  • There's no explanation here of what the problem is, what error messages are being received, or what these env values are. This question is not answerable. – miken32 Aug 04 '23 at 15:00

1 Answers1

-1

I'm using Google Cloud PHP runtime engine and this was the best question I found, but it didn't quite work. So I'm posting the solution that worked for me:

config/database.php

    'redis' => [

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

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

        'default' => [
            'url' => env('REDIS_URL', null),
            'scheme' => env('REDIS_SCHEME', 'tls'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME', null),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),   // GCP is 6378 by default
            'database' => env('REDIS_DB', '0'),
            'ssl' => [
                'cafile' => base_path('certificates/mycert.pem'),
                'verify_peer' => true,
                'verify_peer_name' => false,
                'allow_self_signed' => true,
            ],
        ],

        'cache' => [
            'url' => env('REDIS_URL', null),
            'scheme' => env('REDIS_SCHEME', 'tls'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME', null),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),   // GCP is 6378 by default
            'database' => env('REDIS_CACHE_DB', '1'),
            'ssl' => [
                'cafile' => base_path('certificates/mycert.pem'),
                'verify_peer' => true,
                'verify_peer_name' => false,
                'allow_self_signed' => true,
            ],
        ],

    ],

It's better to set everything in env variables, but I tried to simplify things.