How to Improve Performance for Magento
In this article, we will share our expertise on how to make Magento 2 faster and provide six practical tips to optimize the performance of any Magento 2 sites. The six tips are:
- Enable CSS/JS Minification
- GZIP Compression
- Leverage Browser Caching
- Set Indexers to Update on Schedule
- Full-Page Cache
- Enable Production Mode
1. Enable CSS/JS Minification
In the WebShell, execute command below to enable CSS/JS minification:
cd /cloudclusters/magento
gosu www-data bin/magento config:set dev/js/minify_files 1
gosu www-data bin/magento config:set dev/css/minify_files 1
2. GZIP Compression
When running Apache, you can enable gzip by adding content below to .htaccess file inside your Magento root folder:
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
3. Leverage Browser Caching
To leverage browser caching for Apache, add content below into your .htaccess file:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##
4. Set Indexers to "Update on Schedule"
Magento indexers can be set to two modes: Update on Save or Update on Schedule.
When they are set to Update on Save, every time you save a product, attribute or a category, the specific index starts running. Indexers can become resource consuming, which might slow down your server.
It's best to set the indexers to Update on Schedule mode. In this way, you can expect that they are executed by the cron job at a specific time that you set. Choose a time when the traffic on your website is low.
You can view the current indexers mode by running the command:
cd /cloudclusters/magento
gosu www-data bin/magento indexer:show-mode
You can change the indexers mode to Update on Schedule by running the command:
cd /cloudclusters/magento
gosu www-data bin/magento indexer:set-mode schedule
5. Full-Page Cache
Using full-page cache as part of your Magento optimization efforts can tremendously increase your website speed. This will create cached versions of your pages and will deliver them to the user instead of running all the queries for each request.
Of course, not all pages are cached. For example, the cart page will not be cached, otherwise, all users will see the first cached version of the page. These are dynamic pages or sections of pages that are user- and session-specific.
To enable Magento full-page cache, you can run the following CLI command:
cd /cloudclusters/magento
gosu www-data bin/magento cache:enable full_page
6. Enable Production Mode
Magento has three running modes: default, developer, and production mode.
The production mode is intended for deployment on a production system. This mode hides exceptions, serves the static files from cache only and does not allow you to enable or disable cache types in Magento Admin. It also prevents automatic code file compilation.
While you are working and developing the store, the developer mode is active. Don’t forget to switch to production mode when you deploy your site to the live server!
The CLI command to see the current mode is:
cd /cloudclusters/magento
gosu www-data bin/magento deploy:mode:show
The CLI command to switch to production mode is:
cd /cloudclusters/magento
gosu www-data bin/magento deploy:mode:set production