Configuring Varnish as the Full Page Cache

Varnish may be one of the most interesting elements described in this book, besides Magento 2, of course. What is Varnish and why is it that important? Well, Varnish is like a Ferrari, very fast on the track but hard to maintain or tune. In technical terms, Varnish is an HTTP accelerator designed for heavy websites. Magento users love fast websites.

By default, Varnish support is now included in Magento 2. In Magento 1, we commonly used Turpentine by Nexcess (https://github.com/nexcess/magento-turpentine). The configuration of Varnish is not for the faint-hearted. Varnish includes a Varnish Configuration Language (VCL) file, which holds all the elements to be cached or not.

Setting up a Varnish server may be simple; configuring the VCL is not. Magento 2 provides a default VCL file that works out of the box, but be aware of any custom extensions or layout updates. Any customization has to be added manually in the VCL file before Varnish can cache them.

Tip

By default, Varnish does not support HTTPS; you may need an SSL proxy such as NGINX or Apache to do this.

Getting ready

For this recipe, we will use a Droplet created in Chapter 2, Magento 2 System Tools, at DigitalOcean, https://www.digitalocean.com/. We will be using NGINX, PHP-FPM, and a Composer-based setup including sample data connected to a Varnish server. No other prerequisites are required.

How to do it…

For the purpose of this recipe, let's assume that we need to create a Magento 2 Varnish setup. The following steps will guide you through this:

  1. First, we need to install the Varnish server before we can connect it to Magento. Follow the next step on the shell:
    apt-get install –y apt-transport-https
    

    By default, all current Ubuntu versions support apt-transport-https.

  2. Let's create a new Varnish repository using the following code:
    echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1" | sudo tee -a  /etc/apt/sources.list.d/varnish-cache.list
    
  3. Add the Varnish key to our system using the following command:
    curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -
    
  4. Now we can update our server so that the Varnish software is made available for use. Run the following command:
    apt-get update && apt-get install -y varnish
    service varnish start
    
  5. Now let's test our Varnish server using the following command:
    varnishd –V
    service varnish status
    netstat –anp | grep varnish
    

    As you can see, the Varnish server is running under ports 6081 and 6082.

  6. Before we can use Varnish as a frontend server, we need to change the NGINX or Apache port. In NGINX, we use the following command:
    sed -i 's/80/8080/' /etc/nginx/conf.d/default.conf
    

    We are using port 8080 as an internal port.

    Your configuration file could look as follows:

    server {
      listen 8080;
      
      server_name  yourdomain.com;
  7. Now let's update the Varnish server. We need to change the default port 6081 to 80. Use the following command to change the /etc/default/varnish file:
    sed -i 's/6081/80/' /etc/default/varnish
    
  8. By default, there is a small bug in the Ubuntu system that is using the new systemd setup. The systemd servers will not update their configuration script after a restart or reboot. Let's update this manually using the following command:
    sed -i 's/6081/80/' /lib/systemd/system/varnish.service
    

    Update the systemd process with the following code:

    systemctl daemon-reload
    
  9. Next, we restart the Varnish and NGINX (or Apache) servers. Run the following command:
    service varnish restart && service nginx restart
    
  10. Now you can check whether Varnish and NGINX are running on the correct port. Use the following command:
    netstat -upnlt | egrep 'varnish|nginx'
    
  11. Congratulations, Varnish is running on port 80 and NGINX is running on port 8080.
  12. Now update Magento. Log in to the backend of your Magento site, navigate to Stores | Configuration | Advanced | System | Full Page Cache, and select Varnish Caching in the drop-down menu. If you are running Varnish on the same server as your website, you are okay with the localhost and backend port 8080. It's better to install Varnish on a single dedicated server. You may need to change these settings correctly. Export the correct VCL for the Varnish file. As we are using Varnish 4, we will download it.

    Always make sure that Magento is running in the developer mode when setting up Varnish. When ready to launch, we can switch to the production mode:

    How to do it…
  13. Copy the file to the server and replace the current /etc/varnish/default.vcl file. Now open the file and change backend default to the following:
    backend default {
      .host = "127.0.0.1";
      .port = "8080";
    }
  14. Now let's restart the Varnish server to use the current VCL setup and flush our Magento cache:
    service varnish restart
    php bin/magento cache:clean
    php bin/magento cache:flush
    
  15. Using the Magento 2 developer mode is necessary; it will show us an X-Magento-Cache-Debug notice. Use the following command to see if we have received a cache HIT:
    curl -I http://yourdomain.com
    

    The output of this command should be as follows:

    root@mage2cookbook:~# curl -I http://mage2cookbook.com
    HTTP/1.1 200 OK
    Date: Mon, 14 Dec 2015 19:15:40 GMT
    Content-Type: text/html; charset=UTF-8
    X-Frame-Options: SAMEORIGIN
    X-Content-Type-Options: nosniff
    X-XSS-Protection: 1; mode=block
    X-Magento-Cache-Control: max-age=86400, public, s-maxage=86400
    Pragma: no-cache
    Expires: -1
    Cache-Control: no-store, no-cache, must-revalidate, max-age=0
    Vary: Accept-Encoding
    Age: 1127
    X-Magento-Cache-Debug: HIT
    Accept-Ranges: bytes
    Connection: keep-alive
    
  16. Congratulations, you just finished configuring a Varnish server with Magento 2.

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 16, we installed and configured Varnish to speed up the full page caching.

In steps 1 through 4, we added the official repository to our system and installed Varnish.

In steps 6 and 7, we changed the NGINX port to 8080 instead of 80, and the Varnish port to 80. Now, Varnish will be our gatekeeper after restarting the NGINX and Varnish servers.

In step 12, we told Magento to start communicating with the Varnish server so that all frontend cacheable data is stored here.

There's more…

The current lifetime of the cache is 86,400 seconds, which is one day. So, installing a cache warmer will speed up your pages after an automatic cache flush by Magento. Always keep in mind that by default, all the pages are cold (without a cache hit) and the first GET (page view) can take longer. Varnish needs to build up the cache before customers can benefit from it.

Check out the following Varnish tools to monitor all the incoming data live:

varnishstat

The output of this command will be as follows:

There's more…

Now, let's execute the following command:

Varnishlog

The output of this command will be as follows:

There's more…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.88.62