Speed Up Apache with Varnish Cache on CentOS
In this article
Speeding up Apache with Varnish Cache on CentOS dramatically improves website performance by serving cached content directly from memory, reducing server load and response times. Varnish acts as a reverse proxy, sitting in front of Apache to deliver static and dynamic content faster. This setup is ideal for high-traffic websites, blogs, or CMS platforms like WordPress running on Apache. Before proceeding, ensure your server is running CentOS 7 or 8 with Apache already installed and serving content. You should also have root or sudo access and a basic understanding of command-line operations.
What You'll Need
- A CentOS 7 or CentOS 8 server with Apache installed and running
- Root or sudo user access
- Basic knowledge of Linux command line and systemctl
- Domain or public IP pointing to your server (optional but recommended for testing)
- Firewall configured to allow HTTP (port 80) traffic
Step-by-Step
- Update your system packages to ensure you're working with the latest security patches and software versions. Run:
sudo yum update -y(CentOS 7) orsudo dnf update -y(CentOS 8). - Install EPEL (Extra Packages for Enterprise Linux) repository, which contains Varnish. Use:
sudo yum install epel-release -y. This step is required to access Varnish via yum. - Install Varnish Cache using the package manager:
sudo yum install varnish -y. This installs the Varnish daemon and configuration tools. - Stop Apache temporarily to free up port 80:
sudo systemctl stop httpd. This allows Varnish to bind to port 80 in the next step. - Configure Varnish to listen on port 80. Edit the Varnish service configuration file:
sudo vi /etc/varnish/varnish.params. Find the lineVARNISH_LISTEN_PORT=6081and change it toVARNISH_LISTEN_PORT=80. Save and exit. - Configure Apache to listen on a different port, such as 8080. Edit the Apache configuration:
sudo vi /etc/httpd/conf/httpd.conf. Find theListen 80directive and change it toListen 8080. Also, if you have virtual hosts, update any<VirtualHost *:80>blocks to use<VirtualHost *:8080>. - Restart Apache to apply the port change:
sudo systemctl start httpd. Verify it's running on 8080 by visiting your server's IP address with:8080in the browser. - Configure Varnish backend to point to Apache. Edit the default VCL file:
sudo vi /etc/varnish/default.vcl. Locate thebackend defaultsection and change the port from 8081 to 8080 so it reads:backend default { .host = "127.0.0.1"; .port = "8080"; }This tells Varnish to forward requests to Apache running on port 8080. - Enable and start the Varnish service:
sudo systemctl enable varnishandsudo systemctl start varnish. Check status withsudo systemctl status varnishto ensure it's active and running on port 80. - Adjust firewall settings if using firewalld. Allow HTTP traffic:
sudo firewall-cmd --permanent --add-service=http, then reload:sudo firewall-cmd --reload. - Test the setup by visiting your server's IP or domain in a browser. You should see your Apache-hosted site. To confirm Varnish is working, run:
curl -I http://your-server-ipand look for theX-Varnishheader in the response. - Optimize Varnish cache settings. Edit
/etc/varnish/varnish.paramsand adjustVARNISH_VCL_CONFandVARNISH_STORAGEif needed. For example, increase cache size with-s malloc,512Mto allocate 512MB of RAM to Varnish. - Monitor Varnish performance using built-in tools. Run
sudo varnishstatto view real-time cache hit rate and memory usage. Aim for a high cache hit ratio (over 80%) for optimal performance.
Common Mistakes
- Forgetting to change Apache’s port from 80 to 8080, causing port conflict with Varnish.
- Not updating virtual hosts to reflect the new Apache port, leading to 404 or connection errors.
- Leaving Varnish on port 6081 without reconfiguring it to 80, so it doesn’t serve traffic directly.
- Ignoring firewall rules, blocking external access to port 80.
- Not clearing browser cache when testing, resulting in misleading results.
- Over-allocating Varnish memory beyond available system RAM, causing crashes.
Tips
- Use
varnishlogto debug request flow and identify uncached content. - Configure VCL to cache specific file types (like images, CSS, JS) longer using
set beresp.ttl. - Exclude dynamic pages (e.g., login, cart) from caching using conditional logic in VCL.
- Set up automatic Varnish restart on failure with systemd service restart options.
- Regularly monitor
varnishstatto adjust cache size and TTLs based on traffic patterns. - Use
curl -Iwith different URLs to verify caching behavior and headers.
Troubleshooting
If the site doesn’t load, first check if both services are running: systemctl status httpd and systemctl status varnish. If Varnish fails to start, verify port 80 isn’t in use with netstat -tuln | grep :80. If Apache isn’t reachable on 8080, confirm the Listen directive and virtual hosts are updated. If caching isn’t working, inspect the response headers for X-Varnish and absence of Set-Cookie on static content. Clear Varnish cache with sudo varnishadm 'ban.url .' if stale content appears. If you get 503 errors, ensure Apache is running and reachable from localhost on port 8080. Check Varnish logs with sudo varnishlog -g request to trace backend communication.
Frequently Asked Questions
Does Varnish cache dynamic content?
Yes, but carefully. By default, Varnish may not cache pages with cookies or sessions. You can customize VCL rules to cache certain dynamic content based on URL patterns or headers.
Can I use Varnish with HTTPS?
Directly, no. Varnish does not handle SSL/TLS. Use a TLS termination proxy like Nginx or HAProxy in front of Varnish, or use Let's Encrypt with Nginx as a reverse proxy.
How do I clear the Varnish cache?
Run: sudo varnishadm 'ban.url .' to purge all cached content. For specific URLs, replace . with the path, e.g., /blog.
What happens if Varnish crashes?
Apache continues running on port 8080, but traffic won’t be cached. Configure systemd to restart Varnish automatically by editing its service file.
Is Varnish safe for production use?
Yes. Varnish is widely used by high-traffic websites. Ensure proper configuration, monitoring, and security updates are applied.
How much RAM does Varnish need?
Allocate based on expected cache size. Start with 256MB–1GB, but monitor varnishstat and adjust. Avoid using more than 70% of total system RAM.