@dana
To install Redis and the phpredis extension in Laravel, follow these steps:
- Install Redis on your server:
For Ubuntu:
sudo apt-get update
sudo apt-get install redis-server
For CentOS:
sudo yum install redis
- Install the PhpRedis extension for PHP:
For Ubuntu:
sudo apt-get install php-redis
For CentOS:
You can compile the phpredis extension manually by following these steps:
sudo yum install php-devel
git clone https://github.com/phpredis/phpredis.git
cd phpredis
phpize
./configure
make
make install
- Enable the phpredis extension in your PHP configuration file:
For Ubuntu:
sudo phpenmod redis
sudo systemctl restart apache2
For CentOS:
Edit your php.ini file and add the following line:
extension=redis.so
Then restart your web server:
sudo systemctl restart httpd
- Install the predis/predis package in your Laravel project:
composer require predis/predis
- Configure Laravel to use Redis as the cache and session driver by updating your .env file with the following settings:
CACHE_DRIVER=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120
- You can now use Redis in your Laravel application. To test if it's working, you can run the following code in a controller or route:
Redis::set('key', 'value');
$value = Redis::get('key');
dd($value);
That's it! You have successfully installed Redis and the phpredis extension in Laravel.