Saturday, 29 April 2017

How to make Magento2 website faster

Introduction
Magento websites usually have large visitors. Its mean website should be fast enough to deal with large number of visitors.
To make magento website fast we can use varnish cache that is open source web application accelerator.
Varnish caches files in memory, this enables Varnish to reduce the response time and network bandwidth consumption on future, equivalent requests.
Here is step by step guide to make your magento 2 website more faster than ever by using varnish cache.
1. Install Varnish (Ubuntu)
  • open terminal run "sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -"
  • Run sudo echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list
  • sudo apt-get update
  • sudo apt-get install varnish
  • check that varnish is installed by typing “varnishd -V”

2. Configure apache

Note: apache runs on port 80 so varnish server cannot run on port 80 so we need to change the apache port from 80 to 8080
  • change apache port 80 to 8080
  • go to /etc/apache2/ports.conf and change Listen 80 to Listen 8080
  • save configurations
  • virtual host settings “sudo nano /etc/apache2/sites-available/000-default.conf”
  • change <VirtualHost *:80> to <VirtualHost *:8080>
  • if you have configured any virtual host change the port of that host from 80 to 8080.
  • restart apache “sudo service apache2 restart”
  • check the localhost with the port 8080 “curl -I http://localhost:8080

3. configuration of varnish to use with apache

  • Setting up varnish port to 80 “sudo nano /etc/default/varnish”
  • change DAEMON_OPTS="-a :6081 \ to DAEMON_OPTS="-a :80 \
  • save configurations
  • run “sudo service varnish restart”
  • check our varnish is working by “curl -I http://localhost
  • check varnish is listening top port 80 netstat -tulpn | grep varnishd

4. Configure Magento 2 to use Varnish

  • open file “sudo nano /etc/varnish/default.vcl
  • change the .host (127.0.0.1) to your own IP(xxx.x.x.x) and port should be 8080

5. replace configuration file

  • First enable the Full Page Cache to use Varnish (Go to Admin > Stores > Configuration > Advanced > System > Full Page Cache), you will see as:
  • Save the settings and hit the Export VCL button (Varnish 3 or 4 as per your Varnish Version).
  • And use this default.vcl to replace the file in /etc/varnish/default.vcl
  • to see the varnish headers
  • magento application must be in developer mode
  • you have enabled the full page cache from system->cache management

6. Add cache hosts to app/etc/env.php

  • Go to app/etc/env.php
  • add code at the end of file


7. add the varnish server in your magento config to http-cache-hosts

Run “php bin/magento setup:config:set –http-cache-hosts=127.0.0.1:6081”
change the (127.0.0.1) to your own IP(xxx.x.x.x)
service varnish restart
service apache2 restart

8. varify varnish is working with magento 2

  • Verify contents in var/page_cache folder is not regenerated (server configured to serve the content from Varnish, once cleared, cache folder (var/page_cache) should always be empty.

No comments:

Post a Comment

Alternative content