By Bobby Iliev
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!
Accepted Answer
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.