How To Configure Site Performance In Magento 2

Ipad on Macbook Pro Beside Apple Magic Mouse

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.

Speed Up Your Magento Store by 300%

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 var/ and generated/ directories.

Magento Cache

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 cache:status command:

php bin/magento cache:status

Before making any cache configuration changes, you should always put a backup of your website.

Redis Cache

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:

  1. In the admin panel, go to Stores > Configuration > Advanced > System
  2. In the Full Page Cache section, select Redis from the Caching Application dropdown.
  3. Expand the Redis configuration container.
  4. In the Server field, enter your Redis host’s IP address.
  5. In the Port field, enter the Redis port, i.e., 6379.
  6. If Redis authentication is enabled, enter your Redis password in the Password field.
  7. Save the configuration.

Static Content Deployment

Magento 2 uses static files to enhance site performance. Static files include images, CSS, and JavaScript files. In Magento 2, static files are stored in the 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.

Gzip Compression

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 .htaccess file:


        SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_ACEEPT_ENCODING 

        AddOutputFilterByType DEFLATE application/atom+xml  \
                           application/javascript  \
                           application/json  \
                           application/rss+xml  \
                           application/vnd.ms-fontobject  \
                           application/x-font-ttf  \
                           application/x-web-app-manifest+json  \
                           application/xhtml+xml  \
                           application/xml  \
                           font/opentype  \
                           image/svg+xml  \
                           image/x-icon  \
                           text/css  \
                           text/html  \
                           text/plain  \
                           text/x-component  \
                           text/xml 

        Header append Vary User-Agent env=!dont-vary 

This code will be added to the .htaccess file in the Magento 2 installation directory.

Conclusion

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.

Scroll to Top