How to Install Varnish Cache on Ubuntu 24.04 with Apache
Varnish Cache speeds up an Ubuntu 24.04 server running Apache by storing copies of your web pages in memory. It works as a reverse proxy, which is a server that sits in front of your web server and handles client requests. When a visitor asks for a page Varnish already holds, it serves that copy directly and Apache never sees the request. That takes a lot of pressure off Apache during traffic spikes.
This tutorial installs Apache, moves it off the standard web ports, builds Varnish from source and tests the result. You need a terminal on the server and an account that can use sudo.
To install Varnish Cache with Apache on Ubuntu 24.04, first move Apache off ports 80 and 443 by changing the Listen lines in /etc/apache2/ports.conf to 8080 and 4433, then update the VirtualHost in /etc/apache2/sites-available/000-default.conf to *:8080. Restart Apache with sudo systemctl restart apache2, then build Varnish from source so it takes over port 80.
Install Apache
In this setup that is Apache, so it must already be installed. If Apache is already running on your server, skip ahead to the port changes.
- Run the command below to install Apache on Ubuntu. You will see apt list the packages and then unpack and set them up.
sudo apt update
sudo apt install apache2
On Ubuntu, Apache starts automatically after installation. Once the install finishes, you should have a working web server on the default port. The post below covers the Apache setup in more detail.
Change Apache port
Varnish has to receive incoming web traffic on port 80, so Apache must stop using that port. You will move Apache HTTP traffic to port 8080 and HTTPS traffic to port 4433. Varnish then intercepts each request on port 80 and passes it to Apache on 8080 only when it has no cached copy.
Apache will be unreachable on its old ports until Varnish is running, so do this on a server that can tolerate a short interruption. Also, Varnish itself does not handle HTTPS. If your site uses TLS, you will need something in front of Varnish to terminate it, and that is outside the scope of this tutorial.
- Run the command below to open Apache's ports configuration file in a text editor.
sudo nano /etc/apache2/ports.conf
- Find the lines that set the listening ports. Change 80 to 8080 and 443 to 4433. Change only the port numbers and leave the rest of each line as it is.
The file should now show 8080 and 4433 where it showed 80 and 443 before.
# 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>
- Repeat the process for the default virtual host file. A virtual host defines a site Apache serves and the port it answers on, so it must match the new port. Open the file with the command below.
sudo nano /etc/apache2/sites-available/000-default.conf
- Inside the VirtualHost block, change port 80 to 8080. If you have other virtual host files for your own sites, they need the same change. Otherwise those sites will not be reachable through Varnish.
<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 your changes and close the file.
- Restart Apache by running the command below. The new ports only take effect after a restart.
sudo systemctl restart apache2
If the restart prints an error, reopen the two files and check for typos in the port numbers. Another program already holding one of the ports can cause the same failure. If everything went well, Apache now listens for connections on port 8080.
Build and configure Varnish
In this part you install the compiler tools and libraries Varnish needs, download the Varnish source, compile it and start it. Getting the dependencies in place first means the build runs cleanly and does not stop partway with missing-library errors. Because Varnish is compiled from source here, it is not managed by apt. You will update it by hand later.
- Install the required dependencies first by running the commands below. If one fails, fix that error before moving on, since the build depends on all of them.
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
- Download the Varnish packages with the following commands. They fetch the source files into your current directory.
- Change into the Varnish cache directory, then run
configureandmakeone after the other, using the commands below.configurechecks that your system has everything required.makecompiles the source code. Compiling prepares Varnish to cache incoming web traffic.
Do not run make until configure has finished without errors. The compile can take several minutes, depending on your server.
cd varnish-cache sudo sh autogen.sh sudo sh configure sudo make
- Run the commands below to finish the Varnish installation.
This step takes a few moments to finish. The compiled Varnish software ends up inside the /usr/local directory, ready for configuration.
- Note the location of the main Varnish binary: /usr/local/sbin/varnishd. Then run the command below to refresh the system's library links and cache. Without this, Varnish may not find the newly installed shared libraries when it starts.
sudo ldconfig
- Start Varnish with the following commands once the installation has completed successfully.
sudo varnishd -n /usr/local/sbin/ -a :80 -T localhost:6082 -b localhost:8080
A successful start produces output resembling the message below. If you see an error instead, the usual causes are a skipped library refresh in the previous step or another process already using the port Varnish wants.
Child launched OK
- Run the commands below to test the Varnish deployment.
curl -I http://localhost
Compare your results with the output lines below. If the test shows response headers, headers added by Varnish, such as X-Varnish or Via, mean the request passed through it. If your output differs, go back and check that Apache is listening on 8080 and that Varnish started cleanly.
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
Varnish is now installed and running in front of Apache, and the configuration is complete.
Conclusion:
Pairing Varnish Cache with an Apache backend on Ubuntu 24.04 improves both the speed and the capacity of your site. Varnish answers repeat requests from memory, so Apache handles fewer of them and users get faster responses. Keep these key points in mind:
- Enhanced Performance: Varnish serves cached content straight from memory, which leads to faster load times.
- Reduced Server Load: Offloading requests from the backend Apache server cuts its resource use, which helps most during traffic spikes.
- Easy Installation: Installing Varnish alongside Apache takes a short series of commands, though building from source means you handle updates yourself.
- Customizable Configuration: Moving Apache to its own port and putting Varnish on port 80 gives you flexibility in how the two servers are set up.
- Testing and Verification: A few simple test commands confirm that the installation works and that requests pass through Varnish.
Deploying Varnish Cache gives you a reliable way to accelerate web applications on Ubuntu.
Have a question about this guide?
Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.
Did this guide fix your problem?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
No comments yet — be the first to share your thoughts!