How to install Varnish cache and configure Magento 2 to use Varnish cache on Ubuntu
Magento 2 Vlogs
0:00 / 0:00
How to install Varnish cache and configure Magento 2 to use Varnish cache on Ubuntu
8 586 просмотров · 5 лет назад
Magento 2 Vlogs
3,66 тыс. подписчиков
8 586 просмотров · 5 лет назад
► Fanpage: / giaphugroupcom
► Subscribe to my channel: https://www.youtube.com/giaphugroupco...
► Skype: cuongnq87
► The website link: https://www.giaphugroup.com/magento-2...
Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series.
#magento #magento2 #magento2tutorial #varnishcache
In the previous lesson, I showed everybody on How to install Redis and configure Magento to use Redis for session storage on Ubuntu. If you don't watch this tutorial yet, you can watch it here https://bit.ly/3u3ZhBj. This practice I mentioned to the Varnish cache and Magento recommends using it.
To continue the exercises are related to improving website performance. Today, I am going to show everybody on How to install Varnish cache and configure Magento 2 to use Varnish cache on Ubuntu.
I have the domains phpmyadmin.giaphugroup.com and giaphugroup.com on my server. It is not using Varnish cache.
Okie, let's go.
Let's do this practice, you need to follow steps by step:
Step 1: Install Varnish cache on Ubuntu.
cat /etc/*release
I have Ubuntu 18.04 on my server.
Install Varnish cache
apt-get install varnish
Verify that Varnish cache is running on your server
1. service varnish status
2. varnishd -V
3. Check the port is used for Varnish
netstat -plnt
I installed the Varnish cache to succeed.
Step 2: Configure Nginx
1. nginx -v
2. By default, Nginx runs on port 80, so we will need to configure the Nginx to listen on port 8088. The port 80 will be used for Varnish cache.
nano /etc/nginx/sites-available/default, Change the default port from 80 to 8088.
Then update all domains on your server to listen on port 8088.
I have phpmyadmin.giaphugroup.com and giaphugroup.com on my server. So I need to update the Nginx virtual host configuration file of these domains.
nano /etc/nginx/sites-available/phpmyadmin
nano /etc/nginx/sites-available/giaphugroup.com
3. Configure the domains to handle HTTPS requests and forward everything to default Varnish port.
nano /etc/nginx/sites-available/phpmyadmin
proxy_pass http://127.0.0.1/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Ssl-Offloaded "1";
proxy_set_header X-Forwarded-Port 443;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Ssl on;
nano /etc/nginx/sites-available/giaphugroup.com
Create the new file nginx.conf having the content is copied from nginx.conf.sample. Then add the following codes to location / {}
proxy_pass http://127.0.0.1/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Ssl-Offloaded "1";
proxy_set_header X-Forwarded-Port 443;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Ssl on;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
fastcgi_buffer_size 32k;
fastcgi_buffers 4 32k;
4. systemctl restart nginx
Step 3: Configure Varnish Cache
1. We will need to configure Varnish to use port 80 so it can route traffic to the Nginx web server via the Varnish cache server.
2. We can do it by editing the file /lib/systemd/system/varnish.service:
nano /lib/systemd/system/varnish.service
Change the Varnish default port from 6081 to 80
3. systemctl daemon-reload
4. nano /etc/varnish/default.vcl and Change the port from 8080 to 8088
5. systemctl restart varnish
Step 4: Configure Magento to use Varnish cache
1. Go to the backend and navigate to STORES - Configuration - Advanced - System - Full Page Cache
2. Set the field named Caching Application to "Varnish Cache (Recommended)"
3. Set the field named Access list to 127.0.0.1
4. Set the field named Backend host to 127.0.0.1
5. Set the field named Backend port to 8088
Step 5: Test and See the result
1. You can check the Varnish cache with the curl command as shown below:
curl -I your-server-ip
2. Check the logs
varnishncsa or varnishlog
Try to use a web browser for accessing to your website and checking.
3. You can also verify Varnish caching statistics with the following command:
varnishstat
4. You also can check the Varnish cache by using F12
Yeah! It works perfectly.
Thank you for your watching. If you have any questions about this practice, please feel free to leave a comment below.
Don't forget to like, comment, share my videos and subscribe to my channel for getting the latest videos.
Please do not hesitate to contact me, if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2.