Laravel provides a number of cache drivers like a database, Memcached, and Redis. Out of these options, Redis Cache for Laravel is the best because it is different from Memcached in the sense that it has persistence. Further advantages of Redis over Memcached are that it works with more data types, and it uses less memory.
Cloudways offers Laravel on its Managed Cloud Web Hosting Platform. I’m writing this blog post to help you set up Redis Cache in Laravel in a jiffy!
Setting up Laravel Application on Cloudways
Signup at Cloudways; Select Laravel 5.5 as your application; Fill in your application and server name and launch the server. Your new Laravel 5.5 application will be launched within minutes, as shown in the GIF below.
Enable Redis on Cloudways Server.
I assume that you are signed into your Cloudways account. Follow these steps:
- Select Server Management tab
- Go to Server Settings & Packages tab
- Press the Packages button and turn on Redis by clicking the Install button
In a few moments, Redis is installed and enabled on your Cloudways server.
Setting up Redis on Laravel
Now that the Redis is installed and enabled on your server, let’s set it up on your Laravel 5.5 application. For this, follow these steps
- Go to the Application Management tab.
- Get your application’s SSH terminal and start a SSH session using the provided SSH credentials.
- Now to make sure that Redis is working, type the following command:
If you get PONG in response, Redis is enabled on your server.
- Before using Redis with Laravel, you will need to install the predis/predis package (~1.0) via Composer. To do that, type the following command in terminal
composer require predis/predis
- The Redis configuration for your application is located in the config/database.php file
'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], ],
In the config/cache.php file, you can change your default cache driver from file to Redis, and start using Redis in your application.
You’re done! I hope this post was helpful for those who want to activate Laravel app along with Redis cache on Cloudways Cloud Console. If you have any question, feel free to ask in the comments section below. To check out the features offered by Cloudways for Laravel users, visit this page.