Magento 2 varnish configuration

Optimize Magento 2 – Varnish Configuration In Apache Guide

April 23, 2019Posted By: Sheba Maniar
Apache ConfigurationMagento 2 OptimizationVarnish Cache

Introduction

Magento is a Content Management System(CMS) built in PHP, works with the help of Zend framework, and uses MySQL database. It’s a free and open-source system. If you are in the eCommerce business, then Magento will be a very important eCommerce web application for you that runs with many other open-source applications like Apache, MySQL, and many more.

Now defining further, Apache is one of the most popular web server software which validates a computer to host one or more websites that can be examined over the internet using a web browser.

In 1995, The Apache Group released its first version of Apache. The digital presence of a business is increasing day by day, and in the era of high-speed internet, your website speed matters to increase revenue numbers.

Are you suffering from the speed of Magento 2 and the Apache server? If yes, this blog has the answers to your questions. We will provide effective solutions for boosting your Magento store by configuring Varnish. If you are still facing issues in configuring, then you can hire professional Magento Developers.

Note: The combination of Varnish and Magento 2 is incredibly popular for speeding up your website’s overall performance.

So, let’s get started.

Features Of Varnish In Apache With Magento 2

In this section, we will walk you through crucial features of Varnish in Apache with Magento 2.

  • Caching Layer: Varnish acts as a powerful caching layer in front of Apache. Further, dramatically bolsters the performance of Magento 2 by serving cached content and reducing the load on the backend server.
  • Full Page Caching: Varnish supports full page caching, storing entire HTML responses in memory and delivering them directly to the user. This results in faster page load times and improved user experience.
  • Dynamic Content Handling: Varnish is capable of handling dynamic content by using cache tags or cache invalidation techniques. Moreover, ensuring that personalised or frequently changing content is always up-to-date.
  • ESI (Edge Side Includes) Support: Varnish supports ESI. Also, allowing dynamic fragments of a page to be served from the cache. While still incorporating personalized or dynamic content and improving overall performance.
  • Request Routing and Load Balancing: Varnish can be used for intelligent request routing and load balancing. Also, distributing traffic across multiple backend servers enhances scalability and fault tolerance.

Benefits Of Configuring Varnish In Apache With Magento 2

After understanding the essential features of Varnish in Apache with Magento 2, we will walk you through the fundamental benefits.

  • Improved Page Load Speed: Varnish acts as a reverse proxy cache, storing frequently accessed Magento 2 pages in memory. This results in faster page load times for users.
  • Reduced Server Load: By serving cached content directly from Varnish, the load on the Apache server is significantly reduced, allowing it to handle more concurrent requests.
  • Enhanced Scalability: Varnish enables horizontal scaling by offloading the caching process, allowing for seamless handling of increased traffic and better resource utilization.
  • Customizable Caching Rules: Varnish allows for flexible configuration of cache rules, enabling customization based on specific Magento 2 pages or user behavior.
  • Augmented User Experience: Faster page loads enhance the user experience, reducing bounce rates and increasing conversions.

Steps To Configure Varnish With Magento 2 & Apache

Install Varnish On The Server

  • Connect your server using Putty or terminal and run the below command to install Varnish.
sudo apt-get install varnish
  • Once you install Varnish, you can check the installed version by running the below command
varnishd -V

Configure Varnish In Magento

Step 1

  • Login to the Magento admin panel
  • Go to stores > Configuration > General > Web > Base URLs (Secure)
  • Change the Offloader header to X-Forwarded-Proto from SSL_OFFLOADED and save the configuration

Step 2

  • Go to Stores -> Configuration -> Advanced -> System -> Full Page Cache
  • Change the Caching Application to Varnish Cache (Recommended) from the Built-in Cache.
  • Flush Magento cache

Add Varnish.Vcl On Server

  • Login to ssh with root user and Rename the file
[MAGENTO_ROOT]/var/varnish.vcl to [MAGENTO_ROOT]/var/default.vcl
  • Go to the location of Varnish by the following command
cd /etc/varnish/
  • Take the backup of the 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.

Check DAEMON_OPTS

  • Open the /etc/default/varnish, locate for the DAEMON_OPTS. It should display like 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”

Change Varnish Port From 6081 To 80

  • To change varnish port from 6081 to 80, we need to edit the system 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 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 on 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 follows:
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

Change The Apache Listening Port From 80 To 8080

  • 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>

Restart Varnish And Apache Service

  • Now we need to run the following commands to restart Varnish and Apache service and also to check their status:
sudo systemctl daemon-reload sudo service apache2 restart sudo service apache2 status sudo service varnish restart sudo service varnish status
  • You can check the ports by the following command:
sudo netstat -ltnp | grep :80

Configure Varnish For HTTPS Or SSL

  • To make the Varnish work with HTTPS or SSL, you need to do a 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 to do as follows:
sudo nano /etc/apache2/sites-available/magento-ssl.conf
  • Locate the SSLEngine 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
  • To Verify Varnish Is Working Or Not, You Can Check It By The Following Command:
curl -I -v –location-trusted ‘https://www.example.com’
  • Using the below command will provide you with an updated list of URL requests going to the back end (Misses).
varnishtop -i BereqUR
  • The below command will provide you with an updated list of ALL requests.
Varnishtop -i ReqURL

How To Remove (Uninstall) Varnish?

  • Sudo apt-get purge –auto-remove varnish
  • In the Apache and conf file, change the pot 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

Scaling Varnish Caching In A Multi-Server Environment

In this section, you will learn how to scale Varnish Caching in a multi-server environment with Apache and Magento 2.

  • Shared Cache Storage – Keep your Varnish caching data in a shared storage solution like NFS or a distributed file system. To provide cached material without any disturbances, all Varnish instances may share the same cache.
  • Cache Synchronization – Use tools like Varnish High Availability (VHA) and the Varnish Administration Console (VAC) to synchronize your cache. Its purpose is to keep caches from becoming chaotic and avoid stampedes.
  • Setting up Varnish for efficient session management across several servers. To keep session information consistent between servers, you may use session persistence methods like sticky sessions or storing sessions in a shared database.
  • All servers in the environment should undergo regular health checks and monitoring to guarantee their uptime and optimal performance. Use tools like Varnish Agent to keep an eye on cache health and react appropriately if a server goes down.

Troubleshooting Common Issues And Debugging Varnish Integration

In this section, you will learn how to troubleshoot common issues and debug Varnish Integration with Magento 2 in Apache.

  • Varnish Configuration: Review and verify the Varnish configuration file for any syntax errors or misconfigurations.
  • Varnish Logs: Analyze Varnish logs to identify any error messages or warnings related to the integration with Magento.
  • Magento Configuration: Ensure the Magento configuration is properly set up to work with Varnish.
  • Cache Invalidation: Check if the cache invalidation techniques are correctly implemented to ensure timely dynamic content updates.
  • ESI Blocks: Verify the configuration of Edge Side Includes (ESI) blocks if used, ensuring the proper rendering of dynamic content.
  • Magento Extensions: Disable or troubleshoot any conflicting Magento extensions that may interfere with Varnish integration.

Conclusion

We hope that the above-discussed steps will provide you with the ability to configure Varnish in Apache with Magento 2. Make sure when you are following these steps in Magento 2 that the platform must support Varnish 4.x and 5.x.

Whether your site is unresponsive or not working properly, Varnish continues to serve cached content to visitors. These benefits of Varnish are enough to make you configure it in Apache with Magento 2.

The combination of Varnish and Magento will benefit you in various ways, from increasing your website’s overall speed to enhancing customer experience. If you are still facing issues with configuration in your Magento 2 store, then you can contact our Magento maintenance team or email us at hello@zealousweb.com and our professional Magento developers will provide firm clarification.

FAQ

What Are The Key Benefits Of Magento 2 Varnish Configuration For My Apache Server?

Is Magento 2 Varnish Configuration Suitable For All Types Of Online Stores?