How to install redis and the phpredis extension in laravel?

Member

by dana , in category: PHP Frameworks , 3 months ago

How to install redis and the phpredis extension in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 3 months ago

@dana 

To install Redis and the phpredis extension in Laravel, follow these steps:

  1. Install Redis on your server: For Ubuntu: sudo apt-get update sudo apt-get install redis-server For CentOS: sudo yum install redis
  2. 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
  3. 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
  4. Install the predis/predis package in your Laravel project: composer require predis/predis
  5. 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
  6. 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.