What is Varnish Cache?

Varnish Cache is a high-performance HTTP accelerator and reverse proxy that sits between clients and your web server (e.g., Apache or Nginx). It is designed to cache content and serve it extremely quickly, significantly reducing the load on backend servers and improving website speed and scalability.

Running Varnish on Ubuntu 24.04 LTS is ideal for modern web applications, content-heavy websites, and eCommerce platforms looking to optimize performance, handle high traffic, and lower response times.


Key Features of Varnish Cache

High-Speed HTTP Caching

  • Varnish can serve cached content in microseconds, making it ideal for high-traffic sites.
  • It stores content in memory, allowing lightning-fast delivery of repeated requests.

VCL (Varnish Configuration Language)

  • Custom caching policies using VCL, a flexible domain-specific language.
  • Modify request/response headers, define caching rules, bypass logic, and backend selection.

Backend Integration

  • Works seamlessly with Apache, Nginx, and other HTTP servers.
  • Supports load balancing between multiple backends with health checks.

Grace Mode and Stale Serving

  • Serve stale content if the backend is down or slow, ensuring high availability.
  • Configurable time-to-live (TTL), stale time, and grace period values.

ESI (Edge Side Includes)

  • Allows caching parts of pages, useful for mixing static and dynamic content.
  • Supports partial page updates without invalidating the entire cache.

Logging and Statistics

  • Access detailed metrics with varnishstat, varnishlog, and varnishncsa.
  • Integrate with Prometheus, Grafana, or ELK stack for visualization.

Content Purging and Invalidation

  • Manual or automated purge support for clearing specific URLs or patterns.
  • Supports soft purges and cache invalidation APIs for dynamic sites.

Why Use Varnish on Ubuntu 24.04?

Ubuntu 24.04 (LTS) offers:

  • Modern and secure kernel with long-term support until 2029
  • Latest systemd, libc, and TLS libraries
  • Compatibility with Varnish 7.x and its dependencies
  • Easy installation via official Varnish APT repository
  • Seamless integration with web servers, firewalls (UFW), and HTTPS reverse proxies

System Requirements

ComponentRequirement
OSUbuntu 24.04 LTS
CPU1+ core (multi-core recommended for load)
RAM1 GB minimum (2+ GB for production caching)
DiskOptional for log storage or persistent cache
NetworkPublic interface for frontend, private for backend
Dependencieslibpcre2, libjemalloc, varnish, systemd

Note: Varnish does not support HTTPS directly; use it behind Nginx or HAProxy for TLS termination.


Common Use Cases

  • Accelerating CMS platforms like WordPress, Joomla, Drupal
  • Caching eCommerce sites (e.g., Magento, WooCommerce)
  • Serving APIs and static assets at scale
  • Reducing load on dynamic applications (e.g., Django, Laravel, Rails)
  • Content Delivery for news, media, and ad servers

Varnish vs Similar Caching Solutions

FeatureVarnish CacheNginxSquidRedis (as cache)
Caching TypeHTTP reverse proxyReverse proxyForward proxyIn-memory data store
Configuration LanguageVCLNginx confACLs, directivesLua, scripting APIs
HTTPS SupportNo (use frontend)YesYesNo (used differently)
Edge-Side Includes (ESI)YesNoLimitedNo
Ideal ForWeb app accelerationGeneral web servingForward proxyingSession caching, key-value storage

Security Best Practices

  • Use Varnish behind HTTPS termination (e.g., Nginx with Let’s Encrypt)
  • Limit public access to Varnish admin port (6082)
  • Configure proper ACLs in VCL for purge and management actions
  • Monitor varnishlog and restrict exposure to sensitive headers
  • Always validate backends and sanitize inputs in VCL

Deployment Stack Example

  • Frontend: Nginx (TLS termination, redirect handling)
  • Cache Layer: Varnish Cache (HTTP acceleration, content caching)
  • Backend: Apache, Nginx, or application server (e.g., PHP-FPM, Node.js)
  • Storage/DB: MySQL, PostgreSQL, Redis, etc.

Monitoring and Management Tools

  • varnishstat: Live performance counters
  • varnishlog: Raw HTTP transaction logs
  • varnishncsa: Apache-like log format
  • Prometheus exporter: Available for metric scraping
  • Varnish Dashboard, Grafana, or Zabbix for visualization

Varnish Cache on Ubuntu 24.04 is a powerful, flexible, and production-grade solution for boosting web performance, handling high traffic, and reducing backend load. With its high-speed in-memory caching and VCL-driven customization, Varnish is ideal for dynamic and static websites that need to scale.

Ubuntu 24.04’s long-term support, up-to-date packages, and stable architecture make it an excellent foundation for running Varnish in modern, secure, and efficient web infrastructures.


Step 1: Launch a Server on Shape.Host

Visit https://shape.host and sign in.

Click Create, then select Instance.

Shape.host Resources dashboard with Create > Instance highlighted.

Choose your preferred server location.

Shape.host instance form with the data center location selector highlighted.

Select Ubuntu 24.04 (64-bit) as your operating system.

Pick a plan with at least 2 CPUs, 4 GB RAM, and 20 GB SSD.

Shape.host instance form with the operating system image selector highlighted.

Click Create Instance.

Shape.host instance form with the Create Instance button highlighted.

After deployment, copy your server IP address from the Resources tab.

Shape.host Resources dashboard with the server IP address highlighted.

Step 2: Connect to Your Server

Linux/macOS:
Open Terminal and run:

ssh root@your_server_ip

Windows:
Use PuTTY, enter your IP, and log in as root.


Step 3: Update Your System

apt update

This updates your system’s list of available packages.

Terminal displaying apt update repository download output.

Step 4: Install Apache

apt install apache2

Installs the Apache web server, which will run behind Varnish.

Terminal showing APT package management commands and output.

Step 5: Change Apache Ports

nano /etc/apache2/ports.conf

Update it so Apache listens on port 8080 instead of 80, and 4433 instead of 443:

Listen 8080

<IfModule ssl_module>
    Listen 4433
</IfModule>

<IfModule mod_gnutls.c>
    Listen 4433
</IfModule>
Terminal showing web server configuration text.

Step 6: Update Apache Virtual Host

nano /etc/apache2/sites-available/000-default.conf

Change this line:

<VirtualHost *:80>

To:

<VirtualHost *:8080>

This matches the new port Apache is listening on.

Terminal showing hostname configuration or system identification details.

Step 7: Restart Apache

systemctl restart apache2

Applies the port changes to the web server.

Terminal showing Apache port configuration and restart commands.

Step 8: Install Build Dependencies for Varnish

apt install make automake autotools-dev libedit-dev libjemalloc-dev libncurses-dev libpcre3-dev libtool pkg-config python3-docutils python3-sphinx graphviz autoconf-archive libpcre2-dev curl git

These packages are needed to build Varnish from source.

Terminal showing APT package management commands and output.

Step 9: Clone the Varnish Source Code

cd /tmp
git clone https://github.com/varnishcache/varnish-cache
cd varnish-cache

Downloads the latest development version of Varnish Cache.

Terminal showing Git repository cloning output.

Step 10: Build and Install Varnish

sh autogen.sh
sh configure
make
make install
ldconfig

This builds and installs Varnish from source and updates the shared library cache.

Terminal showing file and directory setup commands.
Terminal showing sh commands or output during Build and Install Varnish.
Terminal showing file and directory setup commands.
Terminal showing file and directory setup commands.

Step 11: Start Varnish and Connect It to Apache

varnishd -n /usr/local/sbin/ -a :80 -T localhost:6082 -b localhost:8080

Starts Varnish listening on port 80, forwarding requests to Apache on port 8080.

  • -a :80 tells Varnish to listen for HTTP traffic.
  • -b localhost:8080 sends incoming traffic to Apache running on port 8080.
Terminal displaying a Varnish daemon startup command and child process messages.

Step 12: Test the Setup

curl -I http://localhost

This fetches the HTTP headers to confirm that Varnish is responding on port 80.

Terminal showing HTTP response headers from a curl request.

You now have Varnish Cache running on Ubuntu 24.04 in front of Apache, configured to cache and deliver faster web content.


This setup works perfectly on Shape.Host Linux SDD VPS:

  • Lightning-fast instance creation
  • Full root access
  • Flexible plans for every project

Start now at https://shape.host