Hello MageComp Blog Readers👋,

Have you checked our recently published blog? If “NO” then check it now What is hCaptcha, and how does it compare to Google reCaptcha? (hCaptcha Vs Google reCAPTCHA). 

Today, I am going to illustrate to you all How to Setup Varnish for Magento 2 on an EC2 server? Does it sound interesting? Put all the belongings apart for some time and read this tutorial.

Introduction:

What is Varnish & Why Varnish is Essential for Magento Stores?

Varnish cache is utilized to speed up the website. Majorly, Varnish is known as an HTTP booster, designed to boost huge webpages and provide enhanced performance for your Magento 2 Store. Mainly varnish is developed for utilizing it with the HTTP protocol. 

When Varnish is integrated with your Magento 2 store, then Magento will not require to give specific time and resources to serve replicate requests from the end-users. Overall this will help your Magento 2 store to increase store performance and raise customer experience of your store. All the received requests will be treated faster than before and the page load time will be improved from seconds to milliseconds. 

Steps to configure Varnish installation:

Basically, this illustration is divided into 2 parts from here. I will explain how to Install Varnish for Apache Server as well as how to Install Varnish for Nginx Server so you can follow the tutorial according to your configuration respectively.

Note: Make sure you are configuring in Ubuntu System as this tutorial is recommended specifically for that only.

Install Varnish for Apache Server:

Follow the given below steps accordingly.

Install Varnish

Ensure twice that you are using a varnish version is 4.x or above. Check it using the below commands.

Switch Apache’s Default port to 8080

Now apply below changes

Listen 8080

Now open /etc/apache2/sites-available/000-default.conf

Apply below changes

<VirtualHost *:8080>

Now at last Restart the Apache

We are going to use varnish in webserver and that is apache so configure varnish to port 80

Add the following

Save and Close the file

Now Run the below-given command to regenerate the dependency tree

Restart Varnish

Now we will verify by checking the ports 

Apache_port

As per the above Screenshot you can view Varnish is listening on port 80 and Apache on 8080.

After this, we will verify it with Magento installation by running the CURL command to view the headers

Check for headers in the below screenshot

Apache_config

Install Varnish for Ngnix Server:

Follow the given below steps accordingly.

Install Ngnix

Ensure twice that you are using varnish version is 4.x or above. Check it using the below commands.

Switch Ngnix Default port to 8080

Now apply below changes

Listen 8080

Now open/etc/nginx/sites-available/magecomp.conf

Apply below changes

<VirtualHost *:8080>

Now at last Restart the Ngnix

We are going to use varnish in webserver and that is apache so configure varnish to port 80

Add the following:

Save and Close the file

Now Run the below-given command to regenerate the dependency tree

Restart Varnish

Now we will verify by checking the ports 

Ngnix_port

As per the above Screenshot you can view Varnish is listening on port 80 and Ngnix on 8080.

After this, we will verify it with Magento installation by running the CURL command to view the headers

Check for headers in the below screenshot

ngnix_config

Steps to configure varnish in Magento 2 store:

Navigate to STORES > Settings > Configuration > ADVANCED > System > Full Page Cache:

Varnish

Now, select the Varnish Caching from the Caching Application list and tap to Save Config.

After the above configuration, your Magento 2 store is utilizing the Varnish cache instead of the default Full-Page Cache. Now implement the below steps for configuring Varnish with Magento.

Below the Varnish Configuration inside the Magento, you will need to change Access-list & Backend Host to your Magento server IP and Save the Configuration.

Don’t forget to open your 8080 port for your server instance as your Apache and Ngnix is listening on 8080

Tap to Export VCL for Varnish that depends on the installed varnish version

Now confirm it by implementing the below command.

Here we have used Varnish 5 therefore we will tap to Export VCL for Varnish 5:

For Backup of your existing default.vcl

You can rename exported varnish.vcl to default.vcl and copy to /etc/varnish/ directory

In case you find an error then follow the below steps:

Remove Health Check

Remove Grace Period

Remove Collect All Cookies

Remove the Following from sub vcl_deliver

Varnish_Continuation

Now, Setup Magento Admin to Purge Varnish Cache

Run the below command to purge Varnish cache from the cache management tool in Admin

php bin/magento setup:config:set –http-cache-hosts=your-magento-server-ip:varnish-port

Verify Varnish cache

Check <magento_root>/var/page_cache directory is empty:

Log in to your Magento server, or switch to, the Magento file system owner.

Run the given below commands:

Access one or more cacheable Magento pages.

Check the var/page_cache/ directory.

If you find the directory empty, Kudos You have triumphantly configured Varnish and Magento.

If you cleared the var/page_cache/ directory, restart Varnish.

That’s It You are good to go.

Conclusion:

Hopefully, all are able to configure Varnish for Magento 2 on the EC2 server by implementing the above steps in your system. In case of any queries you come across comment down in the comment section, I will be happy to help. 

Make sure to share the article with your Magento Developer Friends.

Happy Varnishing!

[Total: 1   Average: 5/5]