Magento2: Install and configure Varnish cache with HTTPS in Ubuntu

Posted on 3 August, 2018

What is varnish cache? Why should we use varnish?

While loading web pages from the web server with extensive content in the web application like Magento, website speed is the primary concern. Magento provides by default full-page cache management system. When the number of product reach thousands in number, website speed slows down significantly. Even when heavy traffic approaching your website, it may decrease website performance.

Now when we open any website for the first time, the data is fetched directly from the server. It takes a long time for content to load. If we use Varnish cache, Varnish itself stores the data. Now when the user requests data, it comes from Varnish and also from the browser cache.

For that purpose, we use Varnish cache(HTTP accelerator). Varnish has a methodology to store the file or sections of files in memory. This is how varnish can reduce the response time and ultimately loading the website in less time. It also reduces the usage of internet. Varnish is exclusively designed for working with HTTP protocol. But, to use it with HTTPS or SSL, we need to configure the reverse proxy. It is explained step by step in the below note:

1) Install varnish with the command :

sudo apt-get install varnish

2) Then, from magento admin panel, go to Stores > Configuration > General > Web > Base URLs (Secure). From there, change the Offloader header to X-Forwarded-Proto from SSL_OFFLOADED and save the configuration.

3) Also from magento admin panel, go to Stores -> Configuration -> Advanced -> System -> Full Page Cache, change the Caching Application to Varnish Cache (Recommended) from the Built-in Cache.

  • Uncheck the value of TTL for public content and set the default value 86400.
  • From Varnish configuration tab, change the access list and backend host to 127.0.0.1 from localhost. Also set the backend port as 8080 and save the configuration.

  • Also flush magento cache with the below command:
php bin/magento cache:flush
  • After saving configuration, go to the Export Configuration from Stores -> Configuration -> Advanced -> System -> Full Page Cache -> Varnish Configuration tab, click on the Export VCL for Varnish. That will download a varnish.vcl file and also export the varnish.vcl file to the /var/www/html/magento/var/ directory.
  • Login in to ssh with the root user. Rename the file /var/www/html/magento/var/varnish.vcl to /var/www/html/magento/var/default.vcl.
mv /var/www/html/magento/var/varnish.vcl 
/var/www/html/magento/var/default.vcl
  • Go to the location of varnish by the following command
cd /etc/varnish/
  • Take the backup of original default.vcl file and put the default.vcl file which is exported from magento.
mv default.vcl default.vcl.original
mv /var/www/html/magento/var/default.vcl.

4) Open the /etc/default/varnish, locate for the DAEMON_OPTS and change the port to -a :80 from the -a :6081 shown as below:

DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-p thread_pool_min=1 \
-p thread_pool_max=1500 \
-p http_resp_hdr_len=42000 \
-p http_resp_size=98304 \
-S /etc/varnish/secret \
-s malloc,768m"

5) To change varnish port from 6081 to 80, we need to edit the systemd service configuration as follows:

sudo nano /etc/systemd/system/multi-user.target.wants/varnish.service

The above command will open a file in which you need to find the line as below:

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T 127.0.0.1:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

and comment it with the hash symbol as below:

#ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T 127.0.0.1:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Below this line, add a line as following:

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T 127.0.0.1:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,1536m

6) Now we need to change Apache listening port from 80 to 8080. To do that, open the Apache ports configuration file and change it as follows:

nano /etc/apache2/ports.conf
Listen 80 -> Listen 8080
nano /etc/apache2/sites-available/magento.conf
<VirtualHost *:80> -> <VirtualHost *:8080>

7) Now we need to run following commands for restart varnish and apache service and also for check their status:

sudo systemctl daemon-reload
sudo service apache2 restart
sudo service apache2 status
sudo service varnish restart
sudo service varnish status

8) You can check the ports by the following command:

sudo netstat -ltnp | grep :80

Output of the command should be that varnish on the 80 port and apache should be on the 8080 port.

Configure Varnish for the HTTPS or SSL

To make the varnish work with the HTTPS or SSL, you need to do reverse proxy. For reverse proxy you must enable the following modes:

sudo a2enmod ssl
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http

To enable reverse proxy do as follows:

sudo nano /etc/apache2/sites-available/magento-ssl.conf

Locate the SSLEngine on and paste the following lines before the SSLEngine on

ProxyPreserveHost On
ProxyPass           	/ http://127.0.0.1:80/
RequestHeader set X-Forwarded-Port "443"
RequestHeader set X-Forwarded-Proto "https"

Restart the apache by this command:

sudo service apache2 restart

Also check the status of apache by this command:

sudo service apache2 status

Verify varnish cache is working or not

To verify varnish is working or not, you can check it by the following command:

curl -I -v --location-trusted 'https://www.example.com'

Also, there are some headers, which are useful to debug that varnish cache is ‘hit’ or not.

  • First to debug about the headers, magento must be in the developer mode.
  • As per the below headers you can know that the varnish is caching the page or not.
X-Magento-Cache-Control: max-age=86400, public, s-maxage=86400
Age: 0
X-Magento-Cache-Debug: HIT
  • If the header shows X-Magento-Cache-Debug: MISS, which means varnish is not caching the page.
  • To fix that, one point can be xml files, in which cacheable=false can be written. In this scenario varnish cache shows the X-Magento-Cache-Debug: MISS, though there can be other possibilities also.
  • To find out cacheable=false, use grep command to know which extension uses the cacheable=false like below:
grep -r “cacheable=false” /var/www/html/magento/app/
  • Using the below command will provide you with an updating list of URL requests going to the back-end (Misses).
varnishtop -i BereqURL
  • The below command will provide you with an updating list of ALL requests.
varnishtop -i ReqURL

How to remove (uninstall) varnish?

sudo apt-get purge --auto-remove varnish

In apache and conf file, change the port to 80 as it was there before.

nano /etc/apache2/ports.conf
	Listen 8080 -> Listen 80
nano /etc/apache2/sites-available/magento.conf
            <VirtualHost *:8080> -> <VirtualHost *:80>
sudo service apache2 restart

This is step by step technical note for Installation and configuration of Varnish Cache. Feel free to ask your questions & provide your views in the comment section below. Also don’t forget to share this note with your team.


Tejas Pala , Senior eCommerce Developer

Magento Technical Notes

About Emipro

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. Our customers are companies of all sizes ranging from startups to large enterprises who realize that they need a professional internet solution to generate revenue streams, establish proper communication channels, to achieve desired goals and streamline business operations. [....] Read More

Our writings seems informative ?

Subscribe for our Magento Technical Notes and get more amazing stuff directly to your inbox!

Post Your Review

X

Your Review has been posted

0 Comment(s)