Ubuntu Linux

How to Install Varnish Cache on Ubuntu 24.04 with Apache

Richard
Written by
Richard
Mar 11, 2025 Updated Mar 20, 2026 5 min read

This article explains how to install and use Varnish Cache with Apache backend on Ubuntu 24.04.

Varnish Cache is a web application accelerator called a caching HTTP reverse proxy. It’s designed to optimize web applications by storing copies of web pages, which can significantly reduce backend server load and increase the speed at which web content is delivered to users.

Using Varnish Cache with an Apache backend on Ubuntu dramatically enhances the performance and scalability of web applications. Varnish caches content, enabling it to serve cached responses directly to users without querying the backend Apache server, significantly enhancing response times.

Install Apache

If you plan to use Varnish with an Apache backend server, you must have the Apache HTTP server installed.

To install Apache on Ubuntu, run the command below.

🐧Bash / Shell
sudo apt update
sudo apt install apache2

For more details on installing Apache, read the post below.

Install Apache on Ubuntu

Change Apache port

Since Apache will reside in the backend and Varnish in front of it, we need to change Apache’s default HTTP port from 80 to 8080.

Run the command below to open Apache ports configuration file.

🐧Bash / Shell
sudo nano /etc/apache2/ports.conf

Then, change the port number from 80 to 8080 and 443 to 4433.

💻Code
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8080

<IfModule ssl_module>
Listen 4433
</IfModule>

<IfModule mod_gnutls.c>
Listen 4433
</IfModule>

Do the same for the default virtual host file.

🐧Bash / Shell
sudo nano /etc/apache2/sites-available/000-default.conf

Change port 80 to 8080 in the VirtualHost.

💻Code
<VirtualHost *:8080>
# The ServerName directive sets the request scheme, hostname and port t>
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

Save the file and exit.

Restart Apache by running the command below.

🐧Bash / Shell
sudo systemctl restart apache2

The Apache server will now listen on port 8080.

Build and configure Varnish

Since Apache is now configured to listen on port 8080, let’s install Varnish Cache.

First, install all dependencies for Varnish by running the commands below.

🐧Bash / Shell
sudo 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

Next, run the commands below to download the Varnish packages.

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

Then, change to the Varnish cache directory and run the commands sequentially to compile Varnish.

Command Prompt
cd varnish-cache
sudo sh autogen.sh
sudo sh configure
sudo make

Next, run the commands below to install the Varnish cache.

🐧Bash / Shell
sudo make install

It should take a few minutes to install. After that, Varnish will be installed in [/usr/local].

The varnish binary is in /usr/local/sbin/varnishd. To make sure that the necessary links and caches of the most recent shared libraries are found, run

🐧Bash / Shell
sudo ldconfig

If Varnish was successfully installed, run the commands below to start it.

🐧Bash / Shell
sudo varnishd -n /usr/local/sbin/ -a :80 -T localhost:6082 -b localhost:8080

If successful, you will see a message similar to the one below.

💻Code
Child launched OK

That should do it. To test, run the commands below.

💻Code
curl -I http://localhost

And you should see something like the lines below.

💻Code
HTTP/1.1 200 OK
Date: Tue, 11 Mar 2025 15:50:54 GMT
Server: Apache/2.4.58 (Ubuntu)
Last-Modified: Tue, 11 Mar 2025 15:20:51 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 Ubuntu (Varnish/trunk)
ETag: W/"29af-63012a259b614-gzip"
Accept-Ranges: bytes
Connection: keep-alive

That should do it!

Conclusion:

Using Varnish Cache with an Apache backend on Ubuntu 24.04 significantly improves website performance and scalability. By caching web content, Varnish reduces the load on backend servers and decreases user response times. Here are the key points to remember:

  • Enhanced Performance: Varnish serves cached content quickly, leading to faster load times.
  • Reduced Server Load: Offloading requests from the backend Apache server minimizes resource consumption.
  • Easy Installation: Installing Varnish alongside Apache is straightforward, requiring just a few commands.
  • Customizable Configuration: Adjusting the Apache port and setting up Varnish allows for flexible server configurations.
  • Testing and Verification: Simple commands can verify successful installation and caching functionality.

Implementing Varnish Cache is an effective way to optimize your web application’s performance on Ubuntu.

Frequently Asked Questions

What is Varnish Cache and how does it work?

Varnish Cache is a caching HTTP reverse proxy that accelerates web applications by storing copies of web pages. It serves cached responses directly to users, reducing the load on backend servers and improving response times.

How do I install Apache on Ubuntu 24.04?

To install Apache on Ubuntu 24.04, run the command 'sudo apt update' followed by 'sudo apt install apache2'. This will install the Apache HTTP server on your system.

Why do I need to change Apache's default port when using Varnish?

You need to change Apache's default port from 80 to 8080 because Varnish will be placed in front of Apache. This allows Varnish to handle incoming requests on port 80 while forwarding them to Apache on port 8080.

What commands are needed to install Varnish Cache on Ubuntu?

To install Varnish Cache, first install the necessary dependencies using 'sudo apt install ...' followed by downloading Varnish from GitHub. Then, compile and install it using 'sudo make install'.

How can I verify that Varnish Cache is running properly?

You can verify that Varnish Cache is running by using the command 'systemctl status varnish'. Additionally, you can check if it's caching content by accessing your web application and observing the response headers for Varnish-specific information.

Was this guide helpful?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version