You know sometimes websites feel slow. That’s often because the server works hard to build the same page every time someone visits. If you’re using Apache as your web server, you can speed things up with a tool called Varnish Cache.
Varnish is a caching tool. A cache is like a memory box—it stores web pages so they don’t have to be built from scratch every time. Varnish sits in front of Apache and delivers saved (cached) pages when possible. That means your website loads faster and your server uses fewer resources.
I’ve set up Varnish on several websites. It made a big difference in performance, especially when traffic grew. I’ll walk you through the steps I used so you can try it too.
Let’s start from scratch.
What You’ll Need
Before we begin, you should already have:
- A server running Apache (like Debian or Ubuntu)
- A working website
- Root or sudo access
This guide works on Debian-based systems, but most steps are similar for others.
Step 1: Install Varnish
Let’s install Varnish first. Open a terminal and run:
sudo apt update
sudo apt install varnish
Once installed, Varnish runs on port 6081 by default. Apache usually runs on port 80.
We want Varnish to take over port 80 and move Apache to a different one—like 8080.
Step 2: Change Apache Port
Let’s change Apache to use port 8080 instead of 80.
Open this file:
sudo nano /etc/apache2/ports.conf
Find this line:
Listen 80
And change it to:
Listen 8080
Then open your site configuration. For example:
sudo nano /etc/apache2/sites-available/000-default.conf
Change these lines:
<VirtualHost *:80>
To:
<VirtualHost *:8080>
Now save and exit.
Restart Apache:
sudo systemctl restart apache2
Now your website should load on port 8080. Try:
http://your-server-ip:8080
Still working? Good. Let’s move on.
Step 3: Configure Varnish to Use Port 80
Now we’ll make Varnish listen on port 80 instead of 6081.
Open the Varnish service configuration:
sudo nano /etc/systemd/system/multi-user.target.wants/varnish.service
Look for this line:
ExecStart=/usr/sbin/varnishd -a :6081 ...
And change :6081
to :80
, like this:
ExecStart=/usr/sbin/varnishd -a :80 ...
Save and exit.
Now reload systemd and restart Varnish:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl restart varnish
Step 4: Point Varnish to Apache
Now we tell Varnish to pass traffic to Apache running on port 8080.
Edit the default Varnish configuration:
sudo nano /etc/varnish/default.vcl
Find the backend section:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Make sure the port matches Apache’s new one (8080). Save and close.
Restart Varnish again:
sudo systemctl restart varnish
Now go to your domain in a browser. If everything is set up right, you should see your site served through Varnish.
Step 5: Test Varnish
You can use curl
to test if Varnish is working.
Try this:
curl -I http://localhost
Look for a line like:
Via: 1.1 varnish
If you see it, that means Varnish is active.
If not, something might be wrong. Double-check ports and configurations.
What Varnish Caches and What It Doesn’t
Varnish stores static content—things that don’t change often. Think of pages like your home page or blog posts.
It usually doesn’t cache:
- Pages with login info
- Pages that change for every user
- POST requests (form submissions)
This helps avoid showing the wrong content to someone.
If you’re not sure what’s being cached, you can dig deeper by enabling logging with:
sudo varnishlog
This shows real-time logs. It’s useful but can be a little noisy.
Step 6: Improve the Cache Rules
You can tweak your default.vcl
file to control what gets cached.
For example, you can ignore cookies from static files:
sub vcl_recv {
if (req.url ~ "\.(jpg|png|css|js)$") {
unset req.http.Cookie;
}
}
This tells Varnish not to consider cookies when caching images or scripts.
When to Use Varnish
Here’s when I think Varnish helps the most:
- When your site gets lots of visitors
- When you use a CMS like WordPress or Joomla
- When your server is low on resources
If your site is mostly static and doesn’t use much PHP, you might not need it.
Still, Varnish can be helpful even for light traffic. It reduces load and makes things feel faster.
What Could Go Wrong?
Let me be honest—Varnish isn’t always smooth to set up. I’ve run into problems like:
- Apache not moving to the new port
- Varnish not caching dynamic pages
- Broken contact forms
But I learned to fix most of them by checking logs and taking it slow.
Here are three common mistakes:
- Wrong ports: Make sure Apache and Varnish are not on the same port.
- Wrong backend IP: Some servers use
localhost
, others use127.0.0.1
. - No headers: If you don’t see
Via: varnish
incurl
, caching might be off.
Benefits of Using Varnish
Here’s why I like using Varnish:
- Faster websites – Cached pages load in milliseconds.
- Lower CPU load – Apache doesn’t need to build every page again.
- Better for busy sites – Can handle more users with the same server.
Some people use NGINX as a reverse proxy instead. That works too, and it has caching. But I find Varnish more powerful when it comes to custom cache rules.
Useful Varnish Commands
Here are a few handy commands I often use:
- Restart Varnish:
sudo systemctl restart varnish
- Check logs:
sudo varnishlog
- Clear the cache:
sudo varnishadm ban "req.url ~ ."
That last one is useful if your site changes and you want Varnish to fetch new content.
Recap: What You Did
Let’s sum things up. You:
- Installed Varnish
- Moved Apache to port 8080
- Set Varnish to listen on port 80
- Configured Varnish to pass requests to Apache
- Tested everything
Now you have a basic Varnish setup working with Apache.
Final Thoughts
I think Varnish is a great tool if you run your own server. It takes a little effort, but it pays off.
Your site will be faster. Your server will feel lighter. And if you’re like me, it feels nice to know you’ve set things up clean and smart.
Have you tried using Varnish before? What worked or didn’t work for you?
If you’re running a big site or just want to squeeze out more speed, give it a shot.
Let your server catch its breath while Varnish handles the heavy lifting.