How To Configure Varnish In Apache With Magento 2
How To Configure Varnish In Apache With Magento 2
Looking for a quick and hassle-free configuration guide? Click here to find out how to configure Magento 2 and slid with great speed.
Join the DZone community and get the full member experience.Join For Free
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.
See Below Step by Step Guide to Configure Varnish With Magento 2 and Apache
1) 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
2) Configure Varnish in Magento
- 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
- 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
3) 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
- 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
4) Check DAEMON_OPTS
- Open the /etc/default/varnish, locate for the DAEMON_OPTS. It should display like below
5) Change Varnish Port From 6081 to 80
- To change the varnish port from 6081 to 80, we need to edit the system service configuration as follows:
- The above command will open a file in which you need to find the line as below:
- And comment it with the hash symbol as below:
- Below this line, add a line as following:
6) Change the Apache Listening Port From 80 to 8080
● Open the Apache ports configuration file and change it as follows:
7) Restart Varnish and Apache Service
- Now we need to run following commands for restart varnish and apache service and also for check their status:
- You can check the ports by the following command:
sudo netstat -ltnp | grep :80
8) 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:
- 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
- Restart the apache by this command:
sudo service apache2 restart
- Also, check the status of apache by this command:
sudo service apache2 status
9) 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’
- 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
10) 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.
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.
Published at DZone with permission of Kandarp Bhatt . See the original article here.
Opinions expressed by DZone contributors are their own.