To configure Magento 2 to use Varnish on CentOS, install Varnish from the official repository, point it at your web server, then register the Varnish host in Magento's admin and apply the generated VCL file. The result is that Varnish serves cached pages in front of Apache or Nginx, cutting PHP and database load dramatically. This guide assumes Magento 2 is already installed and serving pages through Apache or Nginx on CentOS 7 or 8, that you have root or sudo access, and that your store runs in production mode. Menu labels and file paths can differ slightly by Magento and Varnish version, so confirm against your installed release before changing anything.
What You'll Need
- A working Magento 2 installation on CentOS with Apache or Nginx already serving the site.
- Root or sudo access to the server, plus SSH.
- The Varnish package from the official Varnish repository (version 6 or 7 works with current Magento 2 releases).
- The Magento admin credentials and the store's base URL.
- A maintenance window or a staging copy, because changing the HTTP port and VCL will briefly interrupt traffic.
Step-by-Step
- Confirm the current web server port. Varnish must listen on port 80 so browsers reach it first, which means Apache or Nginx has to move to another port. Check what your web server currently binds to. For Apache, look at the
Listendirective in its main configuration; for Nginx, look at thelistenline in the site's server block. Note the port (usually 80) and the site's document root. - Install Varnish. Add the official Varnish repository for your CentOS release, then install the package with your package manager (for example
yum install varnishordnf install varnish). Enable and start the service so it comes back after a reboot. Verify it is running before continuing. - Move the web server to a backend port. Change Apache or Nginx to listen on a port Varnish will proxy to, such as 8080. For Apache, edit the
Listendirective and the virtual host's port. For Nginx, change thelistendirective in the server block. If the site uses HTTPS, the TLS listener stays on 443 and must be handled separately (see the tips below). Restart the web server and confirm the site still answers on the new port locally. - Point Varnish at the backend. Edit the Varnish service configuration (the systemd unit or the
/etc/varnish/varnish.paramsfile, depending on version) so the listen address is port 80 and the backend host is your web server on the new port. Also set the cache storage size to something sensible for your RAM. Reload Varnish and check that requests to the site now pass through it. - Generate the VCL in Magento. In the Magento admin, go to Stores > Settings > Configuration > Advanced > System > Full Page Cache. Set the caching application to Varnish Cache and add your Varnish server under the Varnish configuration fields, using the host and the backend port. Save the configuration, then export the VCL file that Magento generates.
- Deploy the VCL to Varnish. Copy the exported VCL to the Varnish configuration directory on the server and make the Varnish service load it (the exact filename and include path depend on your Varnish version). Reload Varnish. If the VCL fails to compile, Varnish will refuse to reload, so check the service log for the line number.
- Verify caching is working. Request a page with a tool that shows response headers, such as curl with the header option, and look for the Varnish age and cache hit or miss headers. A second request to the same page should show a hit and a rising age. Then place a test order or add a product to the cart to confirm that customer-specific pages are not cached.
- Purge and re-test. From the Magento admin, flush the Magento cache and confirm that Varnish responds by purging the affected URLs. Change a product and check that the storefront reflects it after the purge, not before.
Common Mistakes
The most frequent failure is leaving the web server on port 80, so Varnish and Apache or Nginx fight over the same port and one of them will not start. Another is forgetting to open port 80 in the firewall after moving the web server, which makes the site appear down. People also export the VCL but never copy it to the server, or copy it to the wrong path, so Varnish keeps serving the default configuration. A subtler mistake is caching pages that should never be cached, such as checkout or customer account pages; the stock Magento VCL handles this, but hand-edited VCL often breaks it. Finally, running Magento in developer mode disables full page cache, so Varnish appears to do nothing.
Tips
Keep the VCL under version control and re-export it after every Magento upgrade, because the template changes between releases. If your store uses HTTPS, terminate TLS at a proxy or load balancer in front of Varnish, or configure Varnish to accept TLS through a separate listener, since Varnish itself does not handle certificates. Size the cache storage to a fraction of available RAM and leave headroom for the web server and database. Test on a staging copy first: a wrong port or a broken VCL takes the storefront offline, and recovering means editing files over SSH while traffic fails. Document the ports you chose so the next person does not have to reverse-engineer them.
Troubleshooting
If the site does not load at all, check which service owns port 80 and confirm the web server is listening on the backend port. If pages load but never show cache hits, confirm Magento is in production mode and that the VCL currently loaded is the one Magento generated. If Varnish refuses to start after a VCL change, read the compile error in the service log and fix the reported line. If changes in the admin do not appear on the storefront, the purge is not reaching Varnish: check the Varnish host and port in the Magento configuration and confirm the purge request arrives. If only some pages are stale, look for blocks or cookies that the VCL is told to bypass, and compare against the stock template.
Frequently Asked Questions
Does Varnish replace Magento's built-in cache?
No. Varnish sits in front of the web server and caches full pages; Magento's own caches still handle configuration, layout and other data. Both need to be working.
Can I run Varnish and HTTPS on the same server?
Varnish does not terminate TLS by itself. Put a TLS-terminating proxy or load balancer in front of it, or use a TLS listener arrangement supported by your Varnish version.
Why do I see no cache hits after setup?
The usual causes are developer mode, a VCL that was never deployed, or requests bypassing Varnish because DNS or the firewall still sends traffic straight to the web server.
Do I need to re-export the VCL after upgrading Magento?
Yes. The VCL template changes between releases, and an old file can break caching or purge behaviour after an upgrade.
Will Varnish cache the cart and checkout?
The stock Magento VCL is written to bypass customer-specific pages. If you edit the VCL, keep those bypass rules intact or you risk showing one customer's cart to another.