If you run an online store, or have a client with Magento 2 eCommerce website and the Magento website is loading very slow then you need to engage in Magento performance optimization. A slow website results in increased rate of cart abandonment and you certainly do not want to lose your customers.

Magento performance optimization can be a challenging task but have no worries because we have compiled for you 7 steps to optimize your Magento store which ensures that your website is operating at its maximum speed.

 

1. Installing Nginx

NGINX

The first step in speeding up your Magento 2 website is installing the Nginx server. Nginx has a performance  edge on serving static content and handling heavy traffic so you should use it as your web server of choice with Magento. The following official Magento tutorial: http://devdocs.magento.com/guides/v2.0/install-gde/prereq/nginx.html#install-magento2-ubuntu serves as a good guide for setting up Magento 2 with Nginx web server.

2. Varnish Cache Management

Varnish Cache Management

Varnish is a web application accelerator also known as a caching HTTP reverse proxy. It acts as a middle proxy between Magento back-end and the web server. When a user requests a page, it first checks if the page is cached in its repository and serves it to the user without requesting Magento. If the page is not cached, then it requests Magento to serve that page and adds it to its cache repository. Hence, next time when the user requests that page it will serve it from its own repository.

 

So now, how to configure Varnish with Nginx for Magento Performance Optimization?

Here are the steps to configure Varnish on your server:
– To install Varnish, execute the following command in shell:

"sudo apt-get varnish"

- Open /etc/default/varnish and set change the following attribute as - VARNISH_LISTEN_PORT=80



- Tell Varnish to listen on port 80 by setting the DAEMON_OPTS - a parameter to 80 as follows:

DAEMON_OPTS="-a :80 \
- T localhost:6082 \
- f /etc/varnish/default.vcl \
- S /etc/varnish/secret \
- s malloc,256m"

and save these changes…

- Open /etc/varnish/default.vcl
- Locate the following stanza
backend default {
.host = "127.0.0.1";
.port = "80";
}

Then, tell Varnish to communicate with the content server (Nginx) by setting .port to 8080
– Varnish sits in front of Nginx on port 80 and talks to Nginx on 8080. Nginx defaults to 80, so you should make sure every Nginx server config listens on 8080. If not, Nginx will intercept Varnish.
– Open /etc/nginx/sites-available/default and add the listen directive to the default Nginx server config:

server {
listen 8080;
...
}

 

– Restart Nginx, PHP and Varnish and that concludes the Varnish setup:

sudo service nginx restart
sudo service php7.0-fpm restart
sudo service varnish restart

 

Note:- In the above command replace the 7.0 with your PHP version.

 

3. Nginx Browser Caching

The next step is how to proceed with Nginx browser caching:

- Open /etc/nginx/sites-enabled/default
- Find the server configuration block, which looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server;

. . .

– Add the following two new sections here: one before the server block to define how long to cache different file types, and one inside it to set the caching headers appropriately.

map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
expires $expires;

 

. . .
– Now, restart Nginx to apply changes

sudo service nginx restart

 

How to Enable GZIP Compression on NGINX

4. GZip Compression in Nginx server

Moving on to how to enable GZip compression in Nginx server:

- Open /etc/nginx/nginx.conf
- Find the gzip settings section, which looks like this:
# `gzip` Settings
#
#
gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

 

. . .

You can see that by default, GZip compression is enabled by the GZip on directive, but several additional settings are commented out with # comment sign. You will need to make several changes to this section:

– Enable the additional settings by uncommenting all of the commented lines (i.e. by deleting the # at the beginning of the line)
– Add the gzip_min_length 256; directive, which tells Nginx not to compress files smaller than 256 bytes. This size is very small and thus has barely any benefit from compression.
– Append the gzip_types directive with additional file types denoting web fonts, ico icons, and svg images.
– Save and restart nginx sudo service nginx restart.

 

5. Minify Javascript and CSS from Magento Admin Panel

The fifth step is to learn how to minify Javascript and CSS for Magento Speed Optimization:
– Navigate to the following sections inside the Admin Panel:
Stores > Configuration > Advanced > Developer > CSS Settings
Stores > Configuration > Advanced > Developer > JavaScript Settings
and set Minify CSS, Minify Javascript to Enabled

 

6. Activate the Flat Catalog

The sixth step is to learn how to activate the flat catalog for Magento Optimization?
– On the Admin Panel, Stores > Settings > Configurations.
– On the left panel, under Catalog, select Catalog.
– Open the Storefront section,
– Choose Yes in the Use Flat Catalog Category field.
– Choose Yes in the Use Flat Catalog Product field.
– Save Config to complete.

When you see a system message about the cache update, tap the Cache Management link to flush the cache.

magento flat catalog

 

7. Optimize your Magento website by setting up Content Delivery Network (CDN)How CDN works

Another way to speed up Magento 2 website with thousands of products is to set up a CDN or Content Delivery Network.

CDN is basically a web service that allows you to host external resources of Magento store like CSS/JS files, images etcetera. The primary reason to do is to speed up the process of file serving as opposed to when there will be on your personal server. So now, how do you set up a CDN to speed up your Magento 2 website?

This is not a complex task, and no extensions are required to set up a CDN as it is by default supported by Magento. First, Go to System > Configuration > Web and then set the respective URLs for media, CSS, JS, skin etcetera. You can choose from number of CDNs like MaxCDN, Amazon CloudFront, KeyCDN and many more.

 

Conclusion

These are 7 of the most effective tips to optimize and speed up your Magento 2 website which you can follow step-by-step. In addition to the above, it is also important to have a good web host like Digital Oceam or Vultr as cheap web hosts crowd their servers. So, if your Magento 2 website is loading very slow then you should definitely follow these tips to improve the performance of your Magento 2 website.

Did we miss any other effective tips? Let us know in the comments.

Note: Some of these tips require the services of a Magento developer so if you’re not sure about the content mentioned above then consider hiring an expert Magento developer.