How to Install Varnish Cache on Ubuntu 24.04 with Apache
Want to speed up your website on Ubuntu 24.04 with Apache? Installing Varnish Cache is a great way to do it by caching content. Varnish Cache acts as a powerful HTTP reverse proxy. It stores copies of your web pages so it can serve them directly to visitors, bypassing your Apache server for faster load times. Adding Varnish to your Ubuntu 24.04 Apache setup will dramatically improve your web app’s performance, cutting down server workload and speeding up delivery.
Install Apache and change its default port to 8080. Then, build and compile Varnish Cache from source code using `make install`. Finally, start Varnish using `varnishd -n /usr/local/sbin/ -a :80 -T localhost:6082 -b localhost:8080`.
Install Apache
If you plan to use Varnish with an Apache backend server, you must have Apache installed.
To install Apache on Ubuntu, run the command below.
sudo apt update
sudo apt install apache2
For more details on installing Apache, check out the post below.
Change Apache port
Since Apache will be 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’s ports configuration file.
sudo nano /etc/apache2/ports.conf
Then, change the port number from 80 to 8080 and 443 to 4433.
# 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.
sudo nano /etc/apache2/sites-available/000-default.conf
Change port 80 to 8080 in the VirtualHost.
<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.
sudo systemctl restart apache2
Apache will now listen on port 8080.
Build and configure Varnish
Now that Apache is configured to listen on port 8080, let’s install Varnish Cache.
First, install all the dependencies Varnish needs by running the commands below.
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 these commands to download the Varnish packages.
cd /tmp
git clone https://github.com/varnishcache/varnish-cache
Then, navigate to the Varnish cache directory and run these commands sequentially to compile Varnish.
cd varnish-cache sudo sh autogen.sh sudo sh configure sudo make
Now, run the commands below to install the Varnish cache.
sudo make install
This installation might take a few minutes. After it’s done, you’ll find Varnish installed in [/usr/local].
The main Varnish binary is located at /usr/local/sbin/varnishd. To ensure all necessary links and caches for the most recent shared libraries are found, run
sudo ldconfig
If Varnish installed successfully, you can start it with the following commands.
sudo varnishd -n /usr/local/sbin/ -a :80 -T localhost:6082 -b localhost:8080
If it worked, you should see a message like the one below.
Child launched OK
That’s it! To test Varnish, run these commands.
curl -I http://localhost
You should see output similar to the lines below.
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.
Was this guide helpful?
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.
No comments yet — be the first to share your thoughts!