Laravel Redis Database with DigitalOcean

These are instructions on how to set up DigitalOcean managed Redis database with a Laravel application that is using phpredis as a driver, which was the default before Laravel version 6.0. Since then default version is predis and these instructions do not apply to that. I had problems with setting it up on a few legacy projects and it was always a struggle to find the correct answer to what the correct configuration is. In addition to that, there are a few dead-ends out there on the internet, suggesting things like prepending protocol in front of the host, etc.

If you are getting an error message similar to this:

production.ERROR: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed {"exception":"[object] (ErrorException(code: 0): stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed at [project]/vendor/predis/predis/src/Connection/StreamConnection.php:246)

It is possible to fix it by adding one option to the Redis configuration: 'ssl' => ['verify_peer' => false],.

So the config/database.php code fragment should look like this:

'redis' => [

    'cluster' => false,
    'default' => [
        'ssl' => ['verify_peer' => false], // <- this is thek addition
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'scheme' => env('REDIS_SCHEME', 'tcp'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
    ],

],

Also one thing to note is that REDIS_SCHEME=tls should be set to tls.

The mapping from the CONNECTION DETAILS to the Laravel .env the file should look like this:

username = default                                      # username is not used
password = ****************                             REDIS_PASSWORD=****************
host = db-23-do-user-5555-0.a.db.ondigitalocean.com     REDIS_HOST=db-23-do-user-5555-0.a.db.ondigitalocean.com
port = 25061                                            REDIS_PORT=25061
                                                        REDIS_SCHEME=tls

Author

I plan to write more articles about common laravel components. If you are interested let’s stay in touch.
comments powered by Disqus