How to install and setup Redis for Magento 2 (Ubuntu)

Redis is an application that runs on your web server and is designed to improve the performance of applications such as Magento. It’s not difficult to setup, but there are a lot of steps. So, in this video I’ll walk you through installing Redis in Ubuntu and then setting up Magento 2 to take full advantage.

1. Introduction

Before we begin, I want to highlight some key points about this tutorial:

  • It’s written for Ubuntu 16.04 and 18.04
  • At the time of writing, this will install version 5.0.5 of Redis
  • Typically, there are two ways to install Redis. I have chosen the Repository method, which is much simpler to follow than the Source method

So, right now I’m signed into my server as the “superuser”. If you’ve been following along for a while now, you’ll know I don’t use “root” user for security purposes.

2. Installing Redis

2.1 Add Repositories

In order to install the latest stable version of Redis, we’ll need to add a 3rd party Repository to our Operating System. Think of this like adding an App Store to your mobile phone. To do this, enter: [01:20]

sudo add-apt-repository ppa:chris-lea/redis-server

If this is the first time you’ve used your sudo command since logging in then you’ll be prompted to enter your password.

When prompted to confirm that you wish to add the new repository, press Enter.

2.1 Update Repositories

The next step is to update our new and existing repositories by entering: [01:36]

sudo apt-get update

2.2 Install Redis

Next we need to install Redis by entering: [01:46]

sudo apt-get install redis-server -y

The “-y” at the end isn’t necessary, it simply automatically says “Yes” to any confirmation prompts during the installation.

Once the installation is complete, Redis will run automatically. It also gets added to the list of startup applications for times when your server is rebooted.

3. Configuring Redis

3.1 Open the config file

So, now that Redis is installed, we still need to tweak the configuration settings. The first step of this process is to use the nano text editor to open the Redis configuration file by entering: [02:25]

sudo nano /etc/redis/redis.conf

3.2 Add a supervisor

With the file open, use a combination of the Arrow and Page Up/Down Keys to find a line that says “supervised no”.

Quick Tip: You can also press Ctrl+W to search for the word “supervised” that’ll get you there a little quicker

You’ll want to replace the “no” to “systemd”.

Now save your changes and exit the nano editor by pressing Ctrl+X to exit, Y to save and Enter to confirm.

And then for our changes to take effect, we’ll restart Redis by entering: [03:04]

sudo systemctl restart redis-server

4. Testing Redis

4.1 Running the tests

Now is the perfect time to test that Redis is running and configured correctly. Let’s start by checking the status of the service by entering: [03:22]

sudo systemctl status redis-server

Notice that the “Active Status” is showing as “active (running)”, which is always a good sign. So, simply press Ctrl+C to go back to the Command Line.

So, we’ve confirmed that Redis is running but we need to ensure that it responds correctly. To do that we’ll connect to Redis by entering: [03:48]

redis-cli

Note how the Command Prompt has changed to display the localhost IP and Port. Now if we type: [03:56]

ping

Redis acknowledge us by replying with “PONG”.

The next test is to make sure that Redis is capable of saving values - Which is the whole point of the service. We can create a test to save the word “Hello!” by typing: [04:14]

set test "Hello!"

Redis will reply with “OK” to acknowledge our request. So, to retrieve the value and ensure it’s saved correctly type: [04:28]

get test

And all being well it replies with "Hello!"

At this stage you’ll probably think we’ve done enough tests, but there is still one more. We need to ensure that the value we just saved remains persistent even after the service has been stopped or restarted. So, we’ll exit Redis by typing: [04:48]

exit

Then restart the Redis service by typing: [04:52]

sudo systemctl restart redis-server

Then log back into redis with: [05:00]

redis-cli

And then check if it remembered what we asked it to save by typing: [05:06]

get test

And there you have it, it replies with “Hello!”. At this point it’s time to exit Redis by entering: [05:14]

exit

5. Setting a Password for Redis

5.1 Generate a password

With Redis, you can enable authentication as a security method. But before we configure Redis, we’ll need to generate an incredibly strong password. Normally, I’d say to just make a 20-character password but that wouldn’t be enough with Redis against brute force attacks - Due to how quickly it can process login attempts

So, to quickly generate a 60-character password we can use the openssl tool to create one for us by entering: [05:42]

openssl rand 60 | openssl base64 -A

Highlight and copy this password as we’ll need it later on.

Quick Tip: If you’re using Putty for Windows, you can copy to your clipboard by simply highlighting text with your mouse. To paste anything from your clipboard you can simply right-click on the window (The text will be pasted where your Text Cursor is, not your Mouse Pointer)

5.2 Setting a password

Now, we’re going to open the Redis configuration file again (just like in Step 3) by entering: [06:12]

sudo nano /etc/redis/redis.conf

Look for a line that starts with requirepass.

Quick Tip: You can also press Ctrl+W to search for the word “requirepass” that’ll get you there a little quicker

Once you’ve found it you’ll want to remove the # at the beginning and then replace foobared with the password we just generated.

Now save your changes and exit the nano editor by pressing Ctrl+X to exit, Y to save and Enter to confirm.

And then for our changes to take effect, we’ll restart Redis by entering: [06:50]

sudo systemctl restart redis-server

5.3 Testing the Password

To test that the Password is in effect, we’re going to connect to Redis again using: [07:02]

redis-cli

And then we’re going to set a second test by entering: [07:08]

set test2 "Hello again!"

Notice that Redis rejects our command because we’ve not authenticated ourselves. You’ll only need to authenticate once after connecting to Redis as opposed to entering it before each command.

So, to authenticate type (auth followed by your password): [07:28]

Important: Remember to replace “XXXX” (in the next command) with the password you set in Step 5.2

auth XXXX

This time when we run our test again: [07:34]

set test2 "Hello again!"

We’ll see OK. Now if we try to retrieve our saved value with: [07:44]

get test2

We’ll get confirmation that everything is working. Remember, to go back to the Command Line just type: [07:50]

exit

There are other security practices you can implement in Redis aside from authentication, and you should look them up when you get the chance. But for the purpose of this video I feel we’ve covered the basics.

6. Setting up Magento

6.1 Switching users

At this point, you’re probably tired of the Command Line Interface, but I’m afraid we’ve still got a little further to go. However, if you need to take a break and come back later then now is probably a good time.

Luckily, Magento comes with scripts that make it super-easy to setup Redis. Unless you specify otherwise, it uses a many default settings - That for the most part - Work really well.

But before we can do anything, we need to switch to the “magento user”. As you know from my previous videos, my “magento user” is called “magento”. So, I’ll type: [08:36]

su magento

Followed by the “magento users” password.

And then I’ll switch to the Magento root directory with: [08:46]

cd /var/www/html/

6.1 Configure Redis default caching

So, to configure Redis default caching with the default settings enter the following command: [08:58]

Important: Remember to replace “XXXX” (in the next command) with the password you set in Step 5.2

bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-password=XXXX --cache-backend-redis-db=0

6.2 Configure Redis page caching

Next we need to setup Redis page caching. Again, if you’re happy with the default settings then just enter the following command: [09:10]

Important: Remember to replace “XXXX” (in the next command) with the password you set in Step 5.2

bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-password=XXXX --page-cache-redis-db=1

To ensure that the 2 caches are setup correctly, just enter: [09:28]

Important: Remember to replace “XXXX” (in the next command) with the password you set in Step 5.2

redis-cli -a XXXX monitor

Now when you refresh your Magento store in your browser, you should see something similar to this, which confirms it’s alive and well:

1476826133.810090 [0 127.0.0.1:52366] "select" "1"
1476826133.816293 [0 127.0.0.1:52367] "select" "0"
1476826133.817461 [0 127.0.0.1:52367] "hget" "zc:k:ea6_GLOBAL__DICONFIG" "d"
1476826133.829666 [0 127.0.0.1:52367] "hget" "zc:k:ea6_DICONFIG049005964B465901F774DB9751971818" "d"
1476826133.837854 [0 127.0.0.1:52367] "hget" "zc:k:ea6_INTERCEPTION" "d"
1476826133.868374 [0 127.0.0.1:52368] "select" "1"
1476826133.869011 [0 127.0.0.1:52369] "select" "0"
1476826133.869601 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_DEFAULT__10__235__32__1080MAGENTO2" "d"
1476826133.872317 [0 127.0.0.1:52369] "hget" "zc:k:ea6_INITIAL_CONFIG" "d"

To exit the monitor, simply press Ctrl+C to go back to the Command Line.

6.3 Configure Redis for session storage

Redis can also be used to assist with session storage, which also improves the performance of Magento. Before setting this up, consider this a Public Service Announcement… Earlier I mentioned that for the most part, the default settings worked well. However, in my experience and many, many others, one of the default values is set too low. This low value causes user to see an error when they open pages in quick succession.

The value is called max_concurrency and defaults to “6”. Generally, you’ll want to experiment with this number by increasing in small increments. However, I’ve found that a safe starting point is 12. So, I’ll include that value when running the script to setup Redis for session storage. And to do that, I’m going to enter the following command: [10:38]

Important: Remember to replace “XXXX” (in the next command) with the password you set in Step 5.2

bin/magento setup:config:set --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-log-level=3 --session-save-redis-max-concurrency=12 --session-save-redis-password=XXXX --session-save-redis-db=2

If you’d like to learn more about the different Redis settings for Magento then checkout the link in the article (Configure Redis (Official Magento Documentation)). But that raps it up for this tutorial.

In tutorials like this that contain a lot of steps, the biggest issues almost always come from missing steps or creating typos, so be really careful when following along.

Remember that you’ll need to backup your entire server and not just Magento because you’re installing Redis on your Operating System, not just Magento.

1 Like