Cache Service Laravel redis

Install and Configure Redis Server for Laravel

Let's see How to install Redis for Laravel Project

If you are into Laravel development, you may need to utilize the awesome features like Cache or Queue to drastically improve the performance or to defer the Process of time consuming Jobs respectively , the configuration of Laravel Cache or Laravel Queue require you to choose a driver. Among the all other available drivers options, redis is one.

Due to the flexibility of the redis, its something cool your must try, Redis gives a structured way to store data in memory.  Therefore it is faster than your traditional database that writes to disk.  There are some other programs that do this like memcache; however redis offers a few more data structures to store your data in.Redis can also be used as a messaging queue using it's pubsub functionality.  Due to these features, Redis is often used to store state temporarily.  This is common with microservice architectures, session stores and data that doesn't need long term persistence.

Prerequisites
you must have root SSH access to perform the following steps

Step 1 - Login to Cpanel Server where your Laravel Project is deployed

Connect to your server via SSH as root user.

Step 2 -  Pre Installation Requirement

Run the following commands in the order, which are some of the important pre requirement for redis installation. However nowadays most linux o/s are configured with these packages already.

#update and upgrade the linux
yum -y update
yum -y upgrade

#I like nano
yum install -y nano

#Install gcc & tcl, needed for compilation
yum install -y gcc*

Step 3 - Installing Redis

run the following commands in the order to install the redis service.

wget http://download.redis.io/releases/redis-3.0.1.tar.gz
tar xzf redis-3.0.1.tar.gz
cd redis-3.0.1
make
make test
make install
cd utils
chmod +x install_server.sh
./install_server.sh
sudo chkconfig --level 2345 redis on

Step 4 - Status check and Start,Stop,Restart redis services

use the following commands to check the current status of the redis service, whether its running or down, if you didn't choose default port no during the installation, then you have to adjust port no that matching to yours, 

service redis_6379 status

to start,stop and restart redis service, you may run the following commands, 

service redis_6379 start
service redis_6379 stop
service redis_6379 restart

Step 5 -  Set a Password

you must set a password to secure your redis service, let's go and edit the redis configuration file using nano editor, use the following command.

nano /etc/redis/6379.conf

and then find the line # requirepass foobared in the file,

un-comment the above line and replace "foobared" with your password like below

requirepass somePassword

and press CTRL+O to save the file and restart the redis service.

Step 6 -  Configure Redis for Laravel Project

Before using Redis with Laravel, you will need to install the predis/predis package via Composer:

composer require predis/predis
Configuration

The Redis configuration for your application is located in the config/database.php configuration file. Within this file, you will see a redis array containing the Redis servers utilized by your application:

'redis' => [

    'client' => 'predis',

    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],

],

The default server configuration should suffice for development. However, you are free to modify this array based on your environment, Please note that you must set the following value in your .env file of the Laravel project with the values that match your configuration details.

  • REDIS_HOST=localhost
  • REDIS_PASSWORD = somePassword
  • REDIS_PORT = 6379


If you have any other questions, experience or insights on "Install and Configure Redis Server for Laravel Project" please feel free to leave your thoughts in the comments bellow which might be helpful to someone some day!.


Share your thoughts

You may use <pre> </pre> tag for writing multi lines code or <code> </code> tag for writing single line code in the comment section bellow.