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.
sudo apt update
sudo apt install apache2
For more details on installing Apache, read the post below.
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.
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
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.
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.
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.
cd varnish-cache sudo sh autogen.sh sudo sh configure sudo make
Next, run the commands below to install the Varnish cache.
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
sudo ldconfig
If Varnish was successfully installed, run the commands below to start it.
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.
Child launched OK
That should do it. To test, run the commands below.
curl -I http://localhost
And you should see something like 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.
Leave a Reply Cancel reply