How to Install Redis with Magento 2
Redis stands for REmote DIctionary Server. It stores a site’s full data sets by writing data to a file system every 2 seconds. This constant caching provides a system that gives users quick access to memory. Redis is used by companies all over the world, including large companies like Twitter. It creates a better user experience through faster site speeds even with increased traffic. Plus, Redis gives you the option to increase your site’s size without compromising performance. In this blog we will show you how to install Redis with Magento 2. We will use a base install of both Redis and Magento 2 on an Alpha Hosting Mercury package for the demo.
Step 1: Clone Your Site & Install Redis
First, create a clone of your production website, and name it staging.website.com or development.website.com. We always suggest making changes like this on a staging or development site. Setting up caching on a live site can be dangerous. At least make a quick backup of your site. If you break something on your live site, you will not be able to take orders from your visitors.
Next, you’ll need to install redis on the server your website runs on. At Alpha Hosting that’s a quick button click from the client portal. Log into your client portal at: manage.alphahosting.com. Once there, go to the service you want to install redis for and in the actions menu click view controls. Scroll down to the redis heading and click install.
Step 2: Set Up Redis
Open a terminal window and login to the server. Next, navigate to your Magento 2 home directory We’ll need to edit the env.php file so that Magento 2 knows to use Redis for default, page, and session caching. It’s important you make a copy in case you mess up the edit and then you can easily roll back the changes. I use a simple copy command (cp /app/etc/env.php /app/etc/old.env.php) to take my env.php file and make a copy called old.env.php in the same directory. Next we’ll edit the env.php file using a series of commands. It’s best to use commands instead of inline editing of the file. When using commands you won’t mess up the syntax and you’ll get a confirmation message when you’re done. You can find these commands along with more info about Redis and Magento on the Magento DevDocs Site.
(https://devdocs.magento.com/guides/v2.4/config-guide/redis/redis-pg-cache.html and https://devdocs.magento.com/guides/v2.4/config-guide/redis/redis-session.html)
- Run the following command for setting up Redis as the default cache:
bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db=0
- Run the following command for setting up Redis as the page cache:
bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1
- Run the following command for setting up Redis as the session cache:
bin/magento setup:config:set --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-log-level=4 --session-save-redis-db=2
Accept the changes and that’s it! You can run some test commands outlined in the DevDocs we linked above to confirm everything is set up correctly.
Step 3: Check Your Caching Speed
Head to one of your pages and click on a product or link that hasn’t been cached yet. Then go back and reclick the same link and it should load faster than it did previously. You can also run other tests to prime cache with different tools. These tools can force cache, so you don’t have to wait for people to visit your site to get those speed improvements. If you need help or advice with these tools, we can help! Open a ticket, send us a chat, or call us and we can help.
Site speed can always be improved and installing redis is a great way to do that! We hope this guide was helpful and if you have any questions let us know. Check out the video below to see these steps put into action.