How To Configure Varnish In Apache With Magento 2?

- Features of Varnish In Apache With Magento 2
- Understanding the benefits of configuring Varnish in Apache with Magento 2
- See below step by step guide to configure varnish with Magento 2 and Apache
- Install Varnish on server
- Configure Varnish in Magento
- Add varnish.vcl on server
- Check DAEMON_OPTS
- Change varnish port from 6081 to 80
- Change the Apache listening port from 80 to 8080
- Restart varnish and apache service
- Configure Varnish for the HTTPS or SSL
- Verify varnish cache is working or not
- How to remove (uninstall) varnish?
- Scaling Varnish Caching in a Multi-Server Environment with Apache and Magento 2
- Troubleshooting common issues and debugging Varnish integration with Magento 2 in Apache
- Wrapping Up
Magento is a Content Management System(CMS) built in PHP, Zend framework and MySQL database. It’s a free and open-source system. If you are in 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 the revenue numbers. Are you suffering from the speed with Magento 2 and the apache server? Here we came up with the solution on how to boost your Magento store by configuring Varnish. 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
- Caching Layer: Varnish acts as a powerful caching layer in front of Apache. Further, dramatically improving 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. Whilst, resulting 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 personalized 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, 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, enhancing scalability and fault tolerance.
Understanding the benefits of configuring Varnish in Apache with Magento 2
Consider the following pointers:
- 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 utilisation.
- Customizable Caching Rules: Varnish allows for flexible configuration of cache rules, enabling customisation based on specific Magento 2 pages or user behaviour.
- Improved User Experience: Faster page loads enhance the user experience, reducing bounce rates and increasing conversions.
See below step by step guide to configure varnish with Magento 2 and Apache
Install Varnish on server
- Connect your server using putty or terminal and run below command to install varnish
sudo apt-get install varnish
- Once you install varnish you can check installed varnish version by running below command
varnishd -V
Configure Varnish in Magento
Step 1
- Login to 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 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
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 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
- You can check the ports by the following command:
sudo netstat -ltnp | grep :80
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 to 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
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 updating list of URL requests going to the back-end (Misses).
varnishtop -i BereqURL
- 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 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 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 with Magento 2 in Apache
Consider the following pointers:
- 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.
Wrapping Up
We hope that the above-discussed steps will provide you with configuring varnish in Apache with Magento 2. Make sure when you are following these steps in Magento 2 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. Still facing issues in configuring then you can always contact professional web developing agency for the best outcomes.