Last active
February 1, 2023 13:41
-
-
Save steffen-wirth/0a1cbead750c8269bbe7f7fd418c1035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://dzone.com/articles/how-to-configure-varnish-in-apache-with-magento-2 | |
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 | |
varnishd -V | |
2) 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 | |
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 | |
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. | |
4) Check DAEMON_OPTS | |
Open the /etc/default/varnish, locate for the DAEMON_OPTS. It should display like below | |
HTTP | |
1 | |
DAEMON_OPTS=”-a :80 \ | |
2 | |
T localhost:6082 \ | |
3 | |
f /etc/varnish/default.vcl \ | |
4 | |
p thread_pool_min=1 \ | |
5 | |
p thread_pool_max=1500 \ | |
6 | |
p http_resp_hdr_len=42000 \ | |
7 | |
p http_resp_size=98304 \ | |
8 | |
S /etc/varnish/secret \ | |
9 | |
s malloc,768m” | |
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: | |
Java | |
1 | |
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: | |
Java | |
1 | |
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T 127.0.0.1:6082 -f | |
2 | |
/etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m | |
And comment it with the hash symbol as below: | |
Java | |
1 | |
#ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T 127.0.0.1:6082 -f | |
2 | |
/etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m | |
Below this line, add a line as following: | |
Java | |
1 | |
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T 127.0.0.1:6082 -f | |
2 | |
/etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,1536m | |
6) Change the Apache Listening Port From 80 to 8080 | |
● Open the Apache ports configuration file and change it as follows: | |
Java | |
1 | |
Nano /etc/apache2/ports.conf | |
2 | |
Listen 80 -> Listen 8080 | |
3 | |
Nano /etc/apache2/sites-available/magento.conf | |
4 | |
<VirtualHost *:80> -> <VirtualHost *:8080> | |
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: | |
Java | |
1 | |
sudo systemctl daemon-reload | |
2 | |
sudo service apache2 restart | |
3 | |
sudo service apache2 status | |
4 | |
sudo service varnish restart | |
5 | |
sudo service varnish 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: | |
HTTP | |
1 | |
sudo a2enmod SSL | |
2 | |
sudo a2enmod proxy | |
3 | |
sudo a2enmod proxy_balancer | |
4 | |
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 | |
HTTP | |
1 | |
ProxyPreserveHost On | |
2 | |
ProxyPass / http://127.0.0.1:80/ | |
3 | |
RequestHeader set X-Forwarded-Port “443” | |
4 | |
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 | |
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. | |
HTTP | |
1 | |
nano /etc/apache2/ports.conf | |
2 | |
Listen 8080 -> Listen 80 | |
3 | |
nano /etc/apache2/sites-available/magento.conf | |
4 | |
<VirtualHost *:8080> -> <VirtualHost *:80> | |
5 | |
sudo service apache2 restart | |
for correct purge check | |
varnishlog -g request -q 'ReqMethod eq "PURGE"' | |
and use cache:flush to see output - if status 200 everything works - if not allowed set purge host with server ip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment