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!
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!
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.
Prerequisites:
In order to complete this tutorial, you will need:
An Ubuntu Droplet with Laravel installed, you can follow the steps from this guide here on How to Install and Configure Laravel with LEMP on Ubuntu 18.04
A Managed Redis Cluster, you can follow the steps here: How to Create Redis Database Clusters
Step 1: Install PhpRedis
In order to install the latest version of the PhpRedis extension, we can use
pecl
. To installpecl
you need to install the PHP Extension and Application Repository or PEAR for short:You might also have to install the PHP Dev tools:
Then we would want to go ahead and install this package here. To do that, just run:
Finally, make sure that you enable the new PHP extension by adding the following line to your php.ini:
Then restart PHP-FPM:
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 theREDIS
details as follows: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:
Then, let’s create a method for our Redis connection test. In your controller add the following method:
Also make sure to include your Redis facade so that you have Redis class available. Add the following at the top of your Controller:
After that, let’s make a route so that we could reach our controller. Add the following to your
routes/web.php
: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: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