User Agents

Web browsers and everyday web tech, explained

How to Install Varnish Cache 4 on CentOS

· 5 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Update your system to ensure all packages are current. Run: sudo yum update -y on CentOS 7 or sudo dnf update -y on CentOS 8.
  2. 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.
  3. 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.
  4. Verify the installation by checking the Varnish version: varnishd -V. You should see output indicating Varnish Cache 4.x.
  5. 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) or sudo vi /etc/varnish/varnish.params (CentOS 8).
  6. Inside varnish.params, locate the line VARNISH_LISTEN_PORT=6081 and change it to VARNISH_LISTEN_PORT=80. This allows Varnish to accept HTTP requests directly.
  7. 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.conf and change Listen 80 to Listen 8080. For Nginx, edit /etc/nginx/nginx.conf and update the listen 80 directive in the server block.
  8. Restart your web server to apply the port change: sudo systemctl restart httpd (Apache) or sudo systemctl restart nginx (Nginx).
  9. Define the backend (your web server) in Varnish. Edit the VCL (Varnish Configuration Language) file: sudo vi /etc/varnish/default.vcl.
  10. 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.
  11. Enable and start the Varnish service: sudo systemctl enable varnish and sudo systemctl start varnish.
  12. Verify Varnish is running and listening on port 80: sudo netstat -tulnp | grep :80 or sudo ss -tulnp | grep :80. You should see varnishd in the output.
  13. 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 in cache_hit and cache_miss.
  14. Configure the firewall to allow traffic on port 80. If using firewalld, run: sudo firewall-cmd --permanent --add-service=http and sudo firewall-cmd --reload.
  15. Optional: Adjust Varnish memory allocation. Edit /etc/varnish/varnish.params and modify VARNISH_VCL_CONF and VARNISH_STORAGE if needed. For example, set VARNISH_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.vcl to 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/varnish instead of /etc/varnish/varnish.params on newer systems.

Tips

  • Use varnishlog to debug request flow: sudo varnishlog -g request shows real-time HTTP transactions.
  • Set up a health check in default.vcl to monitor backend availability.
  • Regularly monitor cache hit ratio using varnishstat to 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.