Hosting Guide

How to Install Varnish Cache in Front of Apache (AlmaLinux, Rocky Linux and Ubuntu)

Short answer: Install Varnish from your distribution’s packages (dnf install varnish or apt install varnish), move Apache to port 8080, point Varnish’s backend at 127.0.0.1:8080 in /etc/varnish/default.vcl, and make Varnish listen on port 80. Varnish does not handle HTTPS, so put Nginx or Hitch in front of it on port 443. Test with curl -I and look for the X-Varnish and Age headers. On cPanel servers, use NGINX Manager in WHM instead.

What Varnish does

Varnish Cache is a reverse proxy that keeps copies of pages in memory. The first visitor’s request goes to the web server. Later visitors get the stored copy straight from memory, without PHP or the database doing any work. For busy sites with pages that are the same for most visitors, such as blogs, news sites and catalogs, this cuts server load and speeds up response times a great deal.

Varnish is less useful for pages that are different for each visitor, such as carts, checkouts and account pages. Those must bypass the cache.

A note on the name

In 2025 the open-source Varnish Cache project was renamed Vinyl Cache, while Varnish Software continues to publish Varnish Cache. Most Linux distributions still ship the package as varnish, and the commands and configuration in this guide are the same.

How the pieces fit together

Port Service Job
443 Nginx (or Hitch) Handles HTTPS, then passes the request to Varnish
80 Varnish Answers from the cache, or asks Apache
8080 Apache with PHP Builds the page when it is not in the cache

Apache only listens on 127.0.0.1, so visitors cannot reach it directly.

Requirements

  • ✓ A VPS or dedicated server with root access, running a supported release such as AlmaLinux 9, Rocky Linux 9 or Ubuntu 24.04.
  • ✓ A working Apache and PHP site.
  • ✓ Enough memory for the cache. Start with 256 MB to 1 GB for Varnish, depending on the size of your site.
  • ✓ A plan for HTTPS, because Varnish does not handle TLS itself.

On cPanel servers:

cPanel manages Apache’s ports and configuration, and Varnish is not a supported add-on. Use WHM > Software > NGINX Manager, which puts NGINX with caching in front of Apache, or use LiteSpeed. Do not change Apache’s ports by hand on a cPanel server.

Step 1: install Varnish

# AlmaLinux, Rocky Linux
dnf install -y varnish

# Ubuntu and Debian
apt update && apt install -y varnish

varnishd -V

Distribution packages are older than the newest release but get security updates. If you need a newer version, use the official repositories listed on the project’s website.

Step 2: move Apache to port 8080

# AlmaLinux and Rocky Linux: /etc/httpd/conf/httpd.conf
Listen 127.0.0.1:8080

# Ubuntu and Debian: /etc/apache2/ports.conf
Listen 127.0.0.1:8080

Change <VirtualHost *:80> to <VirtualHost *:8080> in each site’s configuration, then restart Apache:

systemctl restart httpd      # AlmaLinux, Rocky Linux
systemctl restart apache2    # Ubuntu, Debian

Step 3: point Varnish at Apache

Edit /etc/varnish/default.vcl:

vcl 4.1;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    # Never cache admin, login, cart and checkout pages
    if (req.url ~ "^/(wp-admin|wp-login\.php|cart|checkout|my-account)") {
        return (pass);
    }
    # Never cache logged-in users
    if (req.http.Cookie ~ "wordpress_logged_in|woocommerce_items_in_cart") {
        return (pass);
    }
}

Adjust the paths and cookie names to your application. The example covers WordPress and WooCommerce.

Step 4: make Varnish listen on port 80

The listening port is set in the systemd service, and the packaged command differs between distributions. First look at it:

systemctl cat varnish | grep ExecStart

Then create an override, so package updates do not undo your change:

systemctl edit varnish

Add an empty ExecStart= line, which clears the original, then paste the original ExecStart line and change only -a :6081 to -a :80. Keep every other option, such as -F on Ubuntu. On AlmaLinux 9 the result looks like this:

[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,512m

The -s malloc,512m option sets the cache size. Then start Varnish:

systemctl daemon-reload
systemctl enable --now varnish
ss -tlnp | grep -E ":80 |:8080 "

Step 5: add HTTPS in front

Install Nginx, give it your certificate, and pass requests to Varnish on port 80:

server {
    listen 443 ssl;
    server_name example.com www.example.com;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:80;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
}

Our free SSL guide shows how to get the certificate. Make sure your application trusts the X-Forwarded-Proto header, or it may loop between HTTP and HTTPS.

Step 6: test the cache

curl -sI https://example.com/ | grep -iE "x-varnish|age|via"
curl -sI https://example.com/ | grep -iE "x-varnish|age|via"   # run twice

On the second request, Age should be above 0, and X-Varnish shows two numbers. That means the page came from the cache.

Monitor Varnish

Tool What it shows
varnishstat Cache hits and misses, memory use and connection counts
varnishlog Every request in detail, useful for finding why a page is not cached
varnishtop The most frequent log entries, such as the busiest URLs
varnishhist A live chart of response times, split by hits and misses
varnishncsa An access log in the usual Apache format

Clear the cache after changes

After you publish content, old pages may stay in the cache until they expire. Ban everything, or install a purge plugin for your CMS, such as Proxy Cache Purge for WordPress:

varnishadm "ban req.url ~ /"

Set how long pages stay cached

Varnish follows the Cache-Control headers your application sends. If the application sends none, Varnish uses its default lifetime of two minutes. You can set your own lifetime, and a grace period that lets Varnish serve a slightly old page while it fetches a fresh one, or while Apache is down:

sub vcl_backend_response {
    if (beresp.ttl <= 0s) {
        set beresp.ttl = 5m;     # cache for 5 minutes when the app sets nothing
    }
    set beresp.grace = 1h;       # serve stale pages for up to 1 hour if needed
}

Reload Varnish after each VCL change with systemctl reload varnish. A reload keeps the cache.

Troubleshooting

Problem Likely cause Fix
Varnish will not start: address already in use Apache or Nginx still listens on port 80 Check with ss -tlnp | grep :80 and move that service to another port.
503 Backend fetch failed Apache is stopped or not on 127.0.0.1:8080 Start Apache and check the backend address in default.vcl.
Redirect loop on HTTPS The application does not know the visitor used HTTPS Pass X-Forwarded-Proto from Nginx and make the application trust it.
Every request is a miss Cookies or no-cache headers on every page Use varnishlog to find the cookie or header, then remove or allow it in the VCL.
Logged-in users see other people’s pages Personal pages were cached Pass all requests with login or cart cookies, as in Step 3, and clear the cache.

Need help with this?

Need a cache layer set up and tuned for your site? Run it on a Cloud VPS, or let our team configure it for you.

See Cloud VPS plansContact @CustomerPanel

Frequently asked questions

Does Varnish support HTTPS?

Not by itself. Put Nginx, Hitch or another TLS proxy in front of Varnish to handle HTTPS.

Can I use Varnish on a cPanel server?

It is not a supported add-on. Use NGINX Manager in WHM, or LiteSpeed, for caching on cPanel servers.

Why are my pages not cached?

Usually because of cookies or Cache-Control headers from the application. Use varnishlog to see why each request was passed.

Is Varnish still maintained?

Yes. The open-source project continues under the name Vinyl Cache, and Varnish Software publishes Varnish Cache. Distribution packages still use the varnish name.

Can I still follow CentOS 7 steps?

CentOS 7 reached end of life in June 2024. Use a supported system such as AlmaLinux 9 or Ubuntu 24.04.


Sources