Varnish is a powerful HTTP accelerator that sits in front of your web server and caches content to deliver lightning-fast page loads.
In this guide, we’ll walk through how to set up Varnish Cache for Magento 2 with Apache (via WHM/cPanel) and configure Varnish 7.7 for optimal caching performance.
This setup ensures seamless integration with Magento’s Full Page Cache and significantly improves page speed.
Step 1: Enable Maintenance Mode
Before making configuration changes, enable maintenance mode to avoid disruptions:
bin/magento maintenance:enable
Step 2: Install Varnish 7.7
1. Check if Available
dnf info varnish
2. Install (if package available)
dnf install varnish-7.7*
3. Add Repo (if package not found)
If 7.7 isn’t available, add the official repo:
dnf install -y "https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(rpm -E %rhel).noarch.rpm"
Then create a repo file:
tee /etc/yum.repos.d/varnishcache_varnish77.repo > /dev/null <<-EOF
[varnishcache_varnish77]
name=varnishcache_varnish77
baseurl=https://packagecloud.io/varnishcache/varnish77/el/${VERSION_ID%%.*}/$(arch)
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish77/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
EOF
Install Varnish:
dnf install varnish-7.7*
Step 3: Change Listening Ports
Since Varnish will run on port 80, Apache must be moved to a different port (e.g., 8080).
1. Update Varnish Configuration
Edit the systemd service file:
sudo nano /usr/lib/systemd/system/varnish.service
Replace the ExecStart line with:
ExecStart=/usr/sbin/varnishd \
-a :80 \
-a localhost:8443,PROXY \
-p feature=+http2 \
-f /etc/varnish/default.vcl \
-s malloc,2g
2. Update Apache Configuration (via WHM)
- Log in to WHM as
root. - Navigate to
Home ➤ Server Configuration ➤ Tweak Settings ➤ System tab - Find “Apache non-SSL IP/port” and change it to:
:8080 - Save changes.
3. Restart Services after change listening ports
To apply the new configuration:
systemctl restart httpd
systemctl restart varnish
Step 4: Enable Varnish in Magento Admin
- Go to Stores ➤ Configuration ➤ Advanced ➤ System
- Expand Full Page Cache
- Set Caching Application to Varnish Cache
- Click Save Config
Step 5: Set Up Varnish Cache for Magento 2 with Apache Configuration
Magento automatically provides a ready-to-use Varnish configuration file (VCL) that matches your store setup.
This file defines how Magento and Varnish interact — caching rules, ESI blocks, cookies, and headers.
1. Generate the VCL File in Magento Admin
- Go to Stores ➤ Configuration ➤ Advanced ➤ System
- Expand the Full Page Cache section
- In Caching Application, select Varnish Cache
- Scroll down to the Export Configuration section
- Set your preferred Access list, Backend host, and Backend port (usually
127.0.0.1and8080) - Save Config
- Click the Export VCL for Varnish 7 button
Magento will generate and download a file named like:varnish.vcl
This file is automatically adapted for your Magento version and store configuration.
2. Apply the Generated VCL File on the Server
- Open your active Varnish configuration:
nano /etc/varnish/default.vcl - Delete all existing contents.
- Paste in the contents of the
varnish.vclfile you just exported. - Save and exit (
Ctrl + O,Ctrl + X).
3. Restart Services after change Varnish configuration
To apply the new configuration:
systemctl restart httpd
systemctl restart varnish
Step 6: Flush Magento Cache
bin/magento cache:flush
Step 7: Disable Maintenance Mode
Once everything is working:
bin/magento maintenance:disable
Step 8: Verification
Run the following commands to ensure Varnish is working correctly:
curl -I https://yourdomain.com | grep X-Magento-Cache-Debug
You should see:
X-Magento-Cache-Debug: HIT
If you see MISS, it means the page is being cached for the first time — reload to confirm.
Summary
You’ve now successfully:
- Installed Varnish 7.7
- Configured it to run on port 80
- Moved Apache to port 8080
- Connected Varnish with Magento Full Page Cache
- Deployed Magento’s custom VCL configuration
This setup ensures Magento pages are served in milliseconds, improving both LCP metrics and server efficiency.
External References
- Varnish 7.7 Official Documentation — configuration details, parameters, and setup for the latest version.
- Magento 2 Varnish Configuration Guide — official Adobe guide on generating and applying VCL files.
- Apache HTTP Server Documentation — details on configuring reverse proxy and port settings.
- WHM / cPanel Documentation — managing Apache and service configuration via WHM interface.