Everything About Redis Installation and Configuration with Magento

Redis-on-Ubuntu-18

Reading Time: 6 mins

Overview

Being open-source, Redis is an in-memory data structure store and primarily used as a database, cache and message broker. It supports almost most of the programming languages and works well in systems like Linux, OS X, etc. without the need for any external dependencies. This article will take you to the how-to of installing and configuring Redis on Ubuntu system. Here, Redis is configured to be used exclusively for database caching purpose with Magento. 

Pre-conditions

  • It is to be noted that the given instructions are applied to installing Redis server on Ubuntu 18.04.
  • Presuming that you are logged in as a non-root user (in which sudo privileges is necessary).

Installing Redis on Ubuntu System

Step 1: Updating Packages

To update the package index files and to get the newest versions of existing packages, run the following commands:

sudo apt-get update
sudo apt-get upgrade

Step 2: Installing Redis

You will find all the necessary packages for Redis in the Ubuntu repositories itself. To install the Redis server, run the command:

sudo apt-get install redis-server

To check the Redis service status,

sudo systemctl status redis-server
You will get the output like this,

Output

●  redis-server.service – Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-10-28 05:10:45 PDT; 2h ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 2197 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 2201 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
  Main PID: 2226 (redis-server)
    Tasks: 4 (limit: 2319)
   CGroup: /system.slice/redis-server.service
           `-2226 /usr/bin/redis-server 0.0.0.0:6379

If by any chance, it didn’t get started on system start-ups, you can enable and start the Redis server by using the following commands:

sudo systemctl enable redis-server
sudo systemctl start redis-server

Step 3: Configuring Redis

Open the default Redis configuration file with your preferred text editor.

sudo nano /etc/redis/redis.conf

To gain more control, locate the supervised directive within the configuration file and change the init system to systemd (since you are using Ubuntu) which is initially set as NO.

/etc/redis/redis.conf
. . .
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      – no supervision interaction
#   supervised upstart – signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd – signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    – detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal “process is ready.”
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd
. . .

In case if you use the Redis server with the favour of a PHP application, you need to install the Redis-PHP extensions. To do so, use the command:

sudo apt-get install php-redis

Note: You can also set the maximum memory limit as per requirement and your server’s availability.

Step 4: Configuring Redis Cache with Magento

Before configuring Redis cache with Magento, you need to make sure that both the Redis and Magento has been successfully installed on your Ubuntu system. Need to install Magento on your Ubuntu 18.04 system, visit All about Magento Installation Process
Since we also use Varnish for caching purposes, it is necessary to clear the Varnish cache using the below commands:
Go to the Magento web root directory,

cd /var/www/sample.com/public_html/
sudo php bin/magento cache:flush
sudo rm -rf var/cache/*
sudo rm -rf var/page_cache/*

Open your terminal and run the below command:

varnishstat

If it is displayed as MAIN.cache_hit, it means that your caching is working as expected and when you restart Varnish, the cache will be deleted.
As we have cleared the varnish cache from the Magento web root directory, it is safe to configure Redis cache with Magento now. 
Go to the Magento web root directory,

cd /var/www/sample.com/public_html

Open the file called env.php using the command:

sudo nano /app/etc/env.php

Or else, you can directly open the file using the command:

sudo nano /var/www/sample.com/public_html/app/etc/env.php

Now, add the following lines to your env.php file and make sure to replace your respective values.

<?php
return array (
‘backend’ =>
array (
‘frontName’ => ‘admin’,
),
‘crypt’ =>
array (
‘key’ => ‘ad1a4e3e67c7ee92ee765d0358b91’,
),
‘session’ =>
array (
‘save’ => ‘files’,
),
‘db’ =>
array (
‘table_prefix’ => ”,
‘connection’ =>
array (
‘default’ =>
array (
‘host’ => ‘localhost’,
‘dbname’ => ‘testing_db’,
‘username’ => ‘testing_db’,
‘password’ => ‘HvK8pS4qKW’,
‘active’ => ‘1’,
),
),
),
‘resource’ =>
array (
‘default_setup’ =>
array (
‘connection’ => ‘default’,
),
),
‘x-frame-options’ => ‘SAMEORIGIN’,
‘MAGE_MODE’ => ‘production’,
‘cache_types’ =>
array (
‘config’ => 1,
‘layout’ => 1,
‘block_html’ => 1,
‘collections’ => 1,
‘reflection’ => 1,
‘db_ddl’ => 1,
‘eav’ => 1,
‘customer_notification’ => 1,
‘full_page’ => 1,
‘config_integration’ => 1,
‘config_integration_api’ => 1,
‘translate’ => 1,
‘config_webservice’ => 1,
‘compiled_config’ => 1,
),
‘install’ =>
array (
‘date’ => ‘Tue, 22 Nov 2016 03:30:03 +0000’,
),
‘cache’ =>
array (
‘frontend’ =>
array (
‘default’ =>
array (
‘backend’ => ‘Cm_Cache_Backend_Redis’,
‘backend_options’ =>
array (
‘server’ => ‘localhost’,
‘port’ => ‘6379’,
‘password’ => ‘redis_demo’,
‘database’ => ‘0’,
),
),
‘page_cache’ =>
array (
‘backend’ => ‘Cm_Cache_Backend_Redis’,
‘backend_options’ =>
array (
‘server’ => ‘localhost’,
‘port’ => ‘6379’,
‘password’ => ‘redis_demo’,
‘database’ => ‘1’,
),
),
),
),
);

Now, restart the Redis server for the changes to come into effect using the below command:

sudo service redis-server restart

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top