Question

How to configure Laravel 5.7 with Redis TLS (Using phpredis)

Posted August 30, 2019 4.8k views
PHPPHP FrameworksRedisDatabases

Hi

I’m using laravel 5.7 with phpredis (PHP 7.2), but I’m not able to get it to work with tls, I am trying to use it with digitalocean Redis managed database which only supports tls connections.

Any idea if tls even supported with laravel/phpredis?

1 comment
  • Hi. Struggling from similar issue.

    Got this using phpredis (latest version from pecl):

    In PhpRedisConnector.php line 80:
    
      read error on connection to XXXXXX
    

    And this using predis:

    laravel.ERROR: Error while reading line from the server. {"exception":"[object] (Predis\\Connection\\ConnectionException(code: 0): Error while reading line from the server. [tcp://XXXXXXXXXXXXXXXXXX-ondigitalocean.com:25061] at vendor/predis/predis/src/Connection/AbstractConnection.php:155)
    [stacktrace]
    #0 vendor/predis/predis/src/Connection/StreamConnection.php(314): Predis\\Connection\\AbstractConnection->onConnectionError('Error while rea...')
    #1 vendor/predis/predis/src/Connection/AbstractConnection.php(120): Predis\\Connection\\StreamConnection->read()
    #2 vendor/predis/predis/src/Connection/AbstractConnection.php(112): Predis\\Connection\\AbstractConnection->readResponse(Object(Predis\\Command\\RawCommand))
    #3 vendor/predis/predis/src/Connection/StreamConnection.php(260): Predis\\Connection\\AbstractConnection->executeCommand(Object(Predis\\Command\\RawCommand))
    #4 vendor/predis/predis/src/Connection/AbstractConnection.php(180): Predis\\Connection\\StreamConnection->connect()
    #5 vendor/predis/predis/src/Connection/StreamConnection.php(288): Predis\\Connection\\AbstractConnection->getResource()
    \6 vendor/predis/predis/src/Connection/StreamConnection.php(394): Predis\\Connection\\StreamConnection->write('*6\
    \4\
    \VAL\
    $...')
    
    

    Database config:

    'redis' => [
    
            'client' => 'phpredis',
            'cluster' => false,
    
            'default' => [
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => 0,
                'read_timeout' => 60,
                'timeout' => 5,
                'persistent' => false,
            ],
    
        ],
    

    OS: Ubuntu 18.04
    Laravel: 5.8.35

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

So I managed to get this to work, basically the current phpredis version that is shipped with php 7.2 (Ubuntu 18) is old and does not support tls, to fix this I had to update phpredis from pecl (https://pecl.php.net/package/redis)

  • Hi ahmadt,

    can you share a snipped of the redis part of your config/database.php?
    I am also trying to establish a connection but fail at the moment with phpredis 5.0.2 installed.

    Mine looks like the one below:

    'redis' => [
      'client' => 'phpredis',
      'cluster' => false,
    
      'default' => [
        'host'  => 'tls://db-redis-xxxxxxxx.db.ondigitalocean.com',
        'password' => env('REDIS_PASSWORD', null),
        'port'     => 25061,
        'database' => 0,
      ],
    ]
    

    But I end up with an error when I try to connect.

    PHP Warning:  Redis::connect(): SSL operation failed with code 1. OpenSSL Error messages:
    error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version in /app/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php on line 96
    
    • Hi

      Your configuration is similar to what I have except that I‘m using the private redis domain

      
      'redis' => [
            'client' => 'phpredis',
            'default' => [
                'host' => env('REDIS_HOST', 'localhost'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => 0,
                'read_timeout' => 60,
                'timeout' => 5,
                'persistent' => false
            ],
            'cache' => [
              'host' => env('REDIS_HOST', 'localhost'),
              'password' => env('REDIS_PASSWORD', null),
              'port' => env('REDIS_PORT', 6379),
              'database' => 1,
              'read_timeout' => 60,
              'timeout' => 5,
              'persistent' => false
            ],
            'queue' => [
              'host' => env('REDIS_HOST', 'localhost'),
              'password' => env('REDIS_PASSWORD', null),
              'port' => env('REDIS_PORT', 6379),
              'database' => 2,
              'read_timeout' => 60,
              'timeout' => 5,
              'persistent' => false
            ],
        ],
      
      

      Try with the private domain, if it doesn’t work, check phpredis version, the error message seems to be related to ssl version not compatible with do redis

      • Thanks for sharing the config file @ahmadt.

        You are right, my issue was related to SSL. I found a post on Stack Overflow that stated an issue with the PHP version I was using. Updating to a newer PHP version solved my issue. It is even working with predis instead of phpredis.

        • Hello @sc85,

          I would like to know that Stack Overflow’s post so that I can fix it myself. I’m having the same error.

          Thank you!

          • Hi @anunezsva,

            sorry I cannot find the exact post in my browser history. I just remember that it was an issue with the OpenSSL version which did not know the requested auth feature.
            The approach @ahmadt mentioned to update predis did not work for me. Since I am using a pre-build docker image anyway I tried a version with PHP 7.2 instead of 7.1, which I was using before (webdevops/php-nginx:7.2) and that solved my issue.

            At the moment I am using PHP 7.2.21 and predis/predis ^1.1. Maybe also check your database configuration. I slightly changed the redis related part after finding this post at StackOverflow (Note the options part where the password is repeated once more and the scheme set to ‘tls’).

            'redis' => [
                    'client' => 'predis',
                    'cluster' => false,
            
                    'default' => [
                        'scheme'   => env('REDIS_SCHEME', 'tls'),
                        'host'     => env('REDIS_HOST', 'localhost'),
                        'password' => env('REDIS_PASSWORD', null),
                        'port'     => env('REDIS_PORT', 6379),
                        'database' => 0,
                    ],
                    'cache' => [
                        'scheme'   => env('REDIS_SCHEME', 'tls'),
                        'host'     => env('REDIS_HOST', 'localhost'),
                        'password' => env('REDIS_PASSWORD', null),
                        'port'     => env('REDIS_PORT', 6379),
                        'database' => 1,
                    ],
                    'queue' => [
                        'scheme'   => env('REDIS_SCHEME', 'tls'),
                        'host'     => env('REDIS_HOST', 'localhost'),
                        'password' => env('REDIS_PASSWORD', null),
                        'port'     => env('REDIS_PORT', 6379),
                        'database' => 2,
                    ],
                    'options' => [
                        'parameters' => [
                            'password' => env('REDIS_PASSWORD', null),
                        ],
                    ],
                ]
            

            Hope that helps somehow…

Hello,

I’ve created a step by step guide on how to do that along with a short video demo for anyone who is experiencing the same issue:

https://www.digitalocean.com/community/questions/how-to-setup-laravel-with-digitalocean-managed-redis-cluster

Hope that this helps!
Regards,
Bobby

Submit an Answer