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

August 30, 2019 187 views
Redis Databases PHP PHP Frameworks

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

1 Answer
ahmadt August 31, 2019
Accepted Answer

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.

Have another answer? Share your knowledge.