How to Install Varnish Cache 4 on CentOS
In this article
Installing Varnish Cache 4 on CentOS improves website performance by caching HTTP content in memory, reducing server load and speeding up response times. Varnish acts as a reverse proxy between the client and your web server (like Apache or Nginx), serving cached content when possible. This guide walks you through installing and configuring Varnish Cache 4 on CentOS 7 or CentOS 8, ensuring compatibility and optimal setup. Before beginning, ensure you have root or sudo access and a working web server already serving content.
What You'll Need
- CentOS 7 or CentOS 8 server with root or sudo privileges
- An active web server (Apache or Nginx) already serving content
- Basic command-line familiarity (using SSH, editing files, restarting services)
- Internet access to install packages and repositories
- Firewall configured to allow HTTP traffic (port 80)
Step-by-Step
- Update your system to ensure all packages are current. Run:
sudo yum update -yon CentOS 7 orsudo dnf update -yon CentOS 8. - Add the EPEL (Extra Packages for Enterprise Linux) repository, which contains Varnish. Execute:
sudo yum install epel-release -y. This is required for both CentOS 7 and 8. - Install Varnish Cache 4 using the package manager. On CentOS 7, run:
sudo yum install varnish -y. On CentOS 8, use:sudo dnf install varnish -y. - Verify the installation by checking the Varnish version:
varnishd -V. You should see output indicating Varnish Cache 4.x. - Configure Varnish to listen on port 80. By default, Varnish listens on port 6081. Edit the configuration file:
sudo vi /etc/varnish/varnish.params(CentOS 7) orsudo vi /etc/varnish/varnish.params(CentOS 8). - Inside
varnish.params, locate the lineVARNISH_LISTEN_PORT=6081and change it toVARNISH_LISTEN_PORT=80. This allows Varnish to accept HTTP requests directly. - Modify your web server to listen on a different port, typically 8080, so it doesn’t conflict with Varnish. For Apache, edit
/etc/httpd/conf/httpd.confand changeListen 80toListen 8080. For Nginx, edit/etc/nginx/nginx.confand update thelisten 80directive in the server block. - Restart your web server to apply the port change:
sudo systemctl restart httpd(Apache) orsudo systemctl restart nginx(Nginx). - Define the backend (your web server) in Varnish. Edit the VCL (Varnish Configuration Language) file:
sudo vi /etc/varnish/default.vcl. - In
default.vcl, locate the backend section. It should look like:backend default { .host = "127.0.0.1"; .port = "8080"; }Ensure the host and port match your web server’s new configuration. - Enable and start the Varnish service:
sudo systemctl enable varnishandsudo systemctl start varnish. - Verify Varnish is running and listening on port 80:
sudo netstat -tulnp | grep :80orsudo ss -tulnp | grep :80. You should see varnishd in the output. - Test that Varnish is caching by accessing your server’s IP or domain in a browser. Then check Varnish’s cache activity:
sudo varnishstat. Look for non-zero values incache_hitandcache_miss. - Configure the firewall to allow traffic on port 80. If using firewalld, run:
sudo firewall-cmd --permanent --add-service=httpandsudo firewall-cmd --reload. - Optional: Adjust Varnish memory allocation. Edit
/etc/varnish/varnish.paramsand modifyVARNISH_VCL_CONFandVARNISH_STORAGEif needed. For example, setVARNISH_STORAGE="malloc,256M"to allocate 256MB of RAM.
Common Mistakes
- Forgetting to change the web server port, causing a conflict with Varnish on port 80.
- Not updating the backend in
default.vclto reflect the new web server port. - Skipping firewall configuration, blocking external access to Varnish.
- Assuming Varnish starts automatically after install; it must be explicitly enabled and started.
- Editing the wrong configuration file, such as modifying
/etc/sysconfig/varnishinstead of/etc/varnish/varnish.paramson newer systems.
Tips
- Use
varnishlogto debug request flow:sudo varnishlog -g requestshows real-time HTTP transactions. - Set up a health check in
default.vclto monitor backend availability. - Regularly monitor cache hit ratio using
varnishstatto assess performance. - Consider using a custom VCL file for complex routing or cache policies.
- Backup your original web server configuration before making port changes.
Troubleshooting
If Varnish fails to start, check the service status: sudo systemctl status varnish. Common issues include port conflicts, incorrect file paths in configuration, or syntax errors in default.vcl. Use sudo varnishd -C -f /etc/varnish/default.vcl to test VCL syntax. If the web server is unreachable, confirm it’s listening on 8080 and accessible via curl http://127.0.0.1:8080. If cache hits remain zero, ensure your site allows caching (check HTTP headers like Cache-Control). If firewall rules block traffic, verify with sudo firewall-cmd --list-all. In case of high memory usage, adjust VARNISH_STORAGE to a suitable value based on available RAM.
Frequently Asked Questions
Does Varnish work with HTTPS?
Varnish does not natively terminate SSL/TLS. Use a frontend proxy like Nginx or a load balancer to handle HTTPS and forward decrypted traffic to Varnish on HTTP.
Can I run Varnish on a different port?
Yes, but you must ensure your web server listens on port 80 and Varnish is placed behind a proxy or port redirect. For direct public access, port 80 is standard.
How do I clear the Varnish cache?
Use sudo varnishadm ban.url ".*" to purge all cached content or ban.url ^/page to clear specific URLs.
Is Varnish Cache 4 still supported?
Varnish Cache 4 is outdated and no longer actively supported. Consider upgrading to a newer version for security and performance improvements.
Why is my site not loading after installing Varnish?
Check that the web server is running on port 8080 and Varnish is correctly configured with the right backend. Also verify firewall and SELinux settings are not blocking connections.
How much RAM should I allocate to Varnish?
Start with 256MB to 1GB, depending on available system memory and site traffic. Monitor performance and adjust using VARNISH_STORAGE in varnish.params.