Install and configure Redis on Laragon and on MacOS with Laravel Herd
In this post we will see how to install Redis to develop in Laravel, whether you want to use it for the cache system, for the queue and job system, etc.
MacOS wirh Dbngin and Laravel herd
The Redis driver stores the cache in a database engine called Redis; Redis is one of the most popular configurations in Laravel as it is very fast; although, it requires installation and configuration of a program external to the project in Laravel.
CACHE_STORE=redis
Array
The Array controller stores data in an array in PHP and does not require installation of additional programs.
CACHE_STORE=array
These settings are found in the file:
config\cache.php
'default' => env('CACHE_STORE', 'file'),
/*
|--------------------------------------------------------------------------
| Cache Stores
|--------------------------------------------------------------------------
|
| Here you may define all of the cache "stores" for your application as
| well as their drivers. You may even define multiple stores for the
| same cache driver to group types of items stored in your caches.
|
| Supported drivers: "apc", "array", "database", "file",
| "memcached", "redis", "dynamodb", "octane", "null"
|
*/
***
And at the environment variables:
.env
CACHE_STORE=file
Cache with Redis
Configuring the Redis database as a cache system in Laravel is an excellent way to improve the performance of the application, instead of using the database, which is in most cases the operation we want to avoid (as we show in the previous example), we can use an excellent database engine like Redis, which is characterized by its speed and ease of installation.
Redis installation
If you are using Laravel Dbngin, you can create a database preserving the default configuration:
Laragon on Windows
If you use Laragon, you must configure a DLL depending on the version you are using; more information on the official Laragon forum:
https://dev.to/dendihandian/installing-php-redis-extension-on-laragon-2mp3
You must download the DLL in Windows depending on the version you have running, NTS or TS and your version of PHP, in my case it is NTS:
You download the DLL from:
https://pecl.php.net/package/redis
Then copy the DLL into the version of PHP you are running in Laragon; for example:
C:\laragon\bin\php\php-8.XX-nts-Win32-vs16-x64\ext
And activate the extension:
In Laragon, Redis is already installed by default, previously we configured the DLL or database connector to run the Redis database:
C:\laragon\bin\redis\redis-x6XX\redis-server.exe
And you will see a window like the following:
Which indicates that Redis is running and ready to use; Likewise, you can test the status of redis by running:
$ redis-cli
If you see a message like the following:
Could not connect to Redis at 127.0.0.1:6379: No se puede establecer una conexi¾n ya que el equipo de destino deneg¾ expresamente dicha conexi¾n.
not connected>
It means that you have problems with the execution of the database engine.
You can also run:
$ redis-cli
$ 127.0.0.1:6379> ping
And you should see the output:
PONG
On Linux:
sudo apt-get install redis php8.3-redis
sudo systemctl restart php8.3-fpm.service
Additional settings
It may be necessary to install the Predis package to your Laravel project using Composer:
$ composer require predis/predis
Which is the client or connector so you can use Redis in Laravel, to use Redis for the database, it should not be necessary, so only install it if necessary.
In case you want to change any redis configuration parameter, you can specify it as follows:
.env
REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
You indicate the connection options to Redis (as with any other database):
config/database.php
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
];
config\cache.php
'redis' => [
'driver' => 'redis',
'connection' => env('REDIS_CACHE_CONNECTION', 'cache'),
'lock_connection' => env('REDIS_CACHE_LOCK_CONNECTION', 'default'),
],
Finally, we configure the connector to be Redis either in the .env:
.env
CACHE_STORE=redis
And/or configuration file:
config\cache.php
'default' => env('CACHE_STORE', 'redis')
And that would be all, when changing the cache system, it is independent of the implementation that we are going to carry out.
- Andrés Cruz
This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y Libro Laravel 11 con Tailwind Vue 3, introducción a Jetstream Livewire e Inerta desde cero - 2024.
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter