Magento 2 is one of the most powerful and flexible e-commerce platforms. This flexible platform can handle large amounts of data and transactions, making it an excellent choice for bigger businesses. However, a website’s speed is critical to its success. Improving website speed helps your website rank better in search engines, which ultimately leads to more traffic and revenue. This article will guide you through the best practices to configure site performance in Magento 2.
Table of Contents
Enable Production Mode
Magento 2 has a built-in production mode that optimizes performance by pre-compiling and caching data. By default, Magento 2 operates in developer mode, which enables modules and configurations that help developers to debug and develop their website. However, this configuration can be slow and resource-intensive. Thus, enabling production mode is ideal for running in live environments.
To switch to production mode, use the following command in the Magento 2 root directory:
php bin/magento deploy:mode:set production
You can verify the current mode of Magento 2 using:
php bin/magento deploy:mode:show
Ensure that your webserver has read and write access to
Magento 2 has a built-in caching system to improve website performance. By default, Magento 2 enables many cache types. However, it’s a good practice to disable development mode caches in live environments.
To disable cache types, use the following command in Magento 2’s root directory with enabled production mode:
php bin/magento cache:disable
To enable a specific cache type, use the
cache:enable command with the required cache type identifier as an argument:
php bin/magento cache:enable CACHE_TYPE_IDENTIFIER
To verify the current settings of your cache system, use the
php bin/magento cache:status
Before making any cache configuration changes, you should always put a backup of your website.
Redis is an in-memory cache that stores data in a structured and optimized way. Using Redis can significantly improve Magento 2 performance. To configure Redis cache in Magento 2, you need to install the Redis extension on your server and enable it in Magento 2. To install Redis on the server, run the following command:
sudo apt install redis-server
Once Redis is installed, you can enable it in Magento 2 as follows:
- In the admin panel, go to Stores > Configuration > Advanced > System
- In the Full Page Cache section, select Redis from the Caching Application dropdown.
- Expand the Redis configuration container.
- In the Server field, enter your Redis host’s IP address.
- In the Port field, enter the Redis port, i.e., 6379.
- If Redis authentication is enabled, enter your Redis password in the Password field.
- Save the configuration.
Static Content Deployment
pub/static directory. By default, Magento 2 generates static files only when needed. This behavior can lead to slow page loading and negatively affect user experience.
To generate static files in advance, use the following command:
php bin/magento setup:static-content:deploy
Running this command can result in a large number of files being generated, which can consume a lot of disk space. Therefore, it’s good practice to clean up unnecessary files in
pub/static using the below command:
php bin/magento setup:static-content:deploy -f
This command cleans up the outdated files and replaces them with newly generated files.
Enabling gzip compression for the website content can significantly improve Magento 2 website performance. Gzip compression compresses website content on the server before it’s transferred to the user’s browser, reducing its size and making the content load faster.
You can enable gzip compression in Magento 2 by adding the below code to your
This code will be added to the
.htaccess file in the Magento 2 installation directory.
Magento 2 is an excellent platform for e-commerce websites. However, poorly optimized Magento 2 websites can lead to a poor user experience and negatively affect sales. Therefore, optimizing website performance is crucial to the success of your online store. By following the best practices discussed in this article, you can ensure that your Magento 2 website performs at its best.