How do I use varnish

1) Configure firewall to work with Docker
There is 1 step to ensure that CSF works with docker (this should only be done once)

Edit : /etc/csf/csfpost.sh
eg 
vi /etc/csf/csfpost.sh
add a single line
service docker restart
From an SSH terminal run
chmod +x /etc/csf/csfpost.sh
csf -a 172.17.0.0/16
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo csf -r
2) Configure varnish
Make a directory for the varnish configuration file
mkdir /etc/varnish-live
Create the default.vcl file
vi /etc/varnish-live/default.vcl
Paste the attached default.vcl text. Edit the (two instance of) xxx.xxx.xxx.xxx and replace with the IP your site is attached to.

3) Create docker container
Ensure docker is installed from the Plesk extension catalog
Using the search bar enter "varnish"
from the Official Varnish docker image select the version you require based on this table.







Give the container a suitable name eg varnish-live varnish-staging
Tick "Automatically start after system reboot"
Untick " Automatic port mapping "
map the ports to a set that have not been used eg 32780 / 32781
Set "Volume mapping" to /etc/varnish | /etc/varnish-live (to match the setting above)
Set "VARNISH_SIZE" to a suitable figure for the size of your site eg 1G
Click OK


If everything is correct the container should start up.

3) Configuring magento
To ensure this works you will need to make 2 changes to your code base

index.php (and or pub/index.php) insert after <?php
$_SERVER['HTTPS']='on';
.htaccess (and or pub/.htaccess) insert at the top of the file
SetEnvIf X-Forwarded-Proto https HTTPS=on

4) Configure Plesk
Ensure that 
Permanent SEO-safe 301 redirect from HTTP to HTTPS is OFF (you will get a redirect loop other wise)

5) Activate Varnish
Click the Docker Proxy Rules icon >> add rule >> Select the container you created >> OK
Traffic is now flowing through varnish.

6) Configure Magento to use Varnish
/opt/plesk/php/7.4/bin/php bin/magento config:set --scope=default --scope-code=0 system/full_page_cache/caching_application 2
(and to use redis as level 1 cache)
/opt/plesk/php/7.4/bin/php bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=/var/run/redis/redis-server.sock  --cache-backend-redis-db=0
7) Tell magento where to flush cache
/opt/plesk/php/7.4/bin/php bin/magento setup:config:set --http-cache-hosts=xxx.xxx.xxx.xxx:zzzzz
Where  zzzzz  is the port used in step 3.

8) View varnish stats
Run (as root)
docker ps
This will give you the ID of your container.
docker exec -it xxxxxxxxxxxx bash
You are now logged into the container
varnishstat
You can now see your varnish stats in real time



Attached Files
blank-vrnish-6.txt
404kb