Step 1: Prerequisites
Operating System:
Redis is compatible with Linux, macOS, and Windows.
Software Requirements:
PHP (v7.3 or higher for Laravel 9+)
Laravel Framework
Composer
Redis Server
Laravel Environment:
Ensure your Laravel project is set up and running.
Redis PHP Extension:
Install the php-redis extension to allow PHP to communicate with Redis.
Step 2: Install Redis Server
On Linux (Ubuntu/Debian):
Update the package manager:
sudo apt update
Install Redis
:
sudo apt install redis-server
Enable Redis to start at boot:
sudo systemctl enable redis
sudo systemctl start redis
Verify Redis is running
:
redis-cli ping
Output: PONG
On macOS
:
Install Redis using Homebrew:
brew install redis
Start Redis
:
brew services start redis
Verify Redis is running
:
redis-cli ping
Output: PONG
On Windows
:
Download Redis for Windows from the official repository.
Install Redis and run the Redis server executable (redis-server.exe).
Verify Redis is running:
redis-cli ping
Output: PONG
Step 3: Install PHP Redis Extension
Using PECL:
Install the PHP Redis extension:
sudo apt install php-redis
Verify the extension is installed
:
php -m | grep redis
Output: redis
On macOS
:
Install the extension via Homebrew:
brew install php-redis
On Windows
:
Download the correct DLL version for your PHP version from PECL.
Place the DLL file in your PHP ext directory.
Add the following line to your php.ini file:
extension=redis
Restart your web server.
Step 4: Configure Redis in Laravel
Install the predis package (optional, if php-redis isn't available):
composer require predis/predis
Set up .env: Update your .env file with Redis configuration:
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
Verify Configuration: Check the config/database.php file for the Redis configuration:
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'), // Or 'predis'
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
],
Step 5: Test Redis in Laravel
Cache Example: Use Redis to store and retrieve cached data in your application:
use Illuminate\Support\Facades\Cache;
Route::get('/redis-test', function () {
Cache::put('key', 'value', 3600); // Store data in Redis for 1 hour
return Cache::get('key'); // Retrieve data
});
Output: value
Session Example: Enable Redis for sessions by setting SESSION_DRIVER=redis in .env.
Queue Example: Use Redis for queues by setting QUEUE_CONNECTION=redis in .env.
composer require predis/predis
composer show predis/predis
Step 6: Manage Redis
Useful Commands:
Start Redis
:
sudo systemctl start redis
Stop Redis
:
sudo systemctl stop redis
Clear Redis Cache
:
redis-cli FLUSHALL
Step 7: Troubleshooting
Check Redis Status:
sudo systemctl status redis
Debug Redis Connectivity: Use the Redis CLI:
redis-cli ping
Test PHP-Redis Connection: Run a simple PHP script:
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('key', 'value');
echo $redis->get('key'); // Output: value
Step 8: Optimize Redis Configuration
For production, tweak the Redis configuration file (/etc/redis/redis.conf):
Enable persistence:
appendonly yes
Adjust max memory usage:
conf
maxmemory 256mb
maxmemory-policy allkeys-lru
Restart Redis to apply changes
:
sudo systemctl restart redis
Top comments (0)