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!
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.
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
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.
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.
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.
Now visit your hostname or IP address followed by /redis
and you should be able to see something like this:
bool(true)
Here’s a 5-minute video demo on how to do the above:
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
Hi @NormK,
What is the exact Laravel Version that you are using?
I can see that they’ve made some changes to Laravel and the Redis Schema in this PR here:
https://github.com/laravel/framework/pull/34017
And reflected this in the documentation as well: https://github.com/laravel/docs/pull/6279/files
So I believe that if you specify the schema in your database config there is no need to set this up in the
.env
file as well.Can you add an example of your config/database.php setup for redis? I didn’t see that in your post or video. That’s the area I’m having trouble with, especially for TLS. Thanks!