Question

How to Setup Laravel with DigitalOcean Managed Redis Cluster?

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, and sorted sets.

According to the official Laravel documentation here, for the sake of performace it is better to use the PhpRedis PHP extension rather than predis.

However as all connections to DigitalOcean Redis databases are encrypted with TLS/SSL to protect your data in transit, the PHP Redis module that comes out of the box from the default Ubuntu repository is version 3.1.6 which does not support TLS/SSL.

So if you try to connect your Laravel application using the default phpredis extension you would get a similiar error like this:

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

Here’s how you could solve this!

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
February 16, 2020
Accepted Answer

Prerequisites:

In order to complete this tutorial, you will need:

Step 1: Install PhpRedis

In order to install the latest version of the PhpRedis extension, we can use pecl. To install pecl you need to install the PHP Extension and Application Repository or PEAR for short:

sudo apt install php-pear

You might also have to install the PHP Dev tools:

sudo apt install php-dev

Then we would want to go ahead and install this package here. To do that, just run:

sudo pecl install redis

Finally, make sure that you enable the new PHP extension by adding the following line to your php.ini:

extension = redis.io

Then restart PHP-FPM:

sudo systemctl restart php7.2-fpm.service 

Note change the php7.2 part version that you are actually using.

That way we’ve installed the latest PHP Redis module which would allow us to connect to to our Redis Cluster via TLS/SSL.

Step 2: Configure Redis

Next in your .env file you need to specify the Redis Cluster Credentials. Open the .env file with your fevauirte text editor and update the REDIS details as follows:

...
REDIS_HOST=tls://your_redis_host.db.ondigitalocean.com
REDIS_PASSWORD=your_redis_password
REDIS_PORT=25061
...

Note the tls:// part before the name of the Redis cluster, if you don’t specify it the connection would not work.

Save the file and then we will go ahead and prepare a test Controller and Route to test the connection.

Step 3: Create Controller and Route

First we will go ahead and create a Controller that we would use to test the connection to our Redis Cluster:

php artisan make:controller ConnectionChecker

Then, let’s create a method for our Redis connection test. In your controller add the following method:

...
    public static function redisTest()
    {
	$redis = Redis::connection();
        try{
           var_dump($redis->ping());
        } catch (Exception $e){
            $e->getMessage();
        }
    }
 ...

Also make sure to include your Redis facade so that you have Redis class available. Add the following at the top of your Controller:

use Illuminate\Support\Facades\Redis;

After that, let’s make a route so that we could reach our controller. Add the following to your routes/web.php:

Route::get('/redis', 'ConnectionChecker@redisTest');

Save the web.php file and let’s go ahead and test the connection.

Step 4: Test the connection

Now visit your hostname or IP address followed by /redis and you should be able to see something like this:

bool(true)

Video Demo

Here’s a 5-minute video demo on how to do the above:

Conclusion

As the DigitalOcean Managed Redis Cluster allows only TLS/SSL connections, we had to install the latest PHP Redis extension version which allowed our Laravel application to successfully connect to the cluster.

Hope that this helps! Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.