How to Install and Configure Varnish Cache on VPS for Web Performance

Varnish Cache is a high-performance HTTP accelerator designed to cache HTTP responses to reduce the load on your backend servers. When configured properly, it can significantly speed up your website by serving cached content to users, reducing server load, and improving user experience. Varnish is often used in conjunction with web servers like Nginx or Apache. This guide will walk you through installing Varnish on a VPS, configuring it, and integrating it with your existing web server for maximum performance.


Step 1: Set Up Your VPS

Before we begin, ensure that you have a VPS running with a clean installation of Ubuntu 20.04 LTS or later. You can use any VPS provider like DigitalOcean, Linode, Vultr, or AnonVM for this setup.

  1. Create a VPS

    • Choose a VPS with at least 1GB of RAM. For better performance, a VPS with 2GB of RAM or more is recommended.
    • Use Ubuntu 20.04 LTS as the operating system, but this guide can work on other Linux distributions with slight adjustments.
  2. SSH Into Your VPS

    • After creating your VPS, connect to it via SSH:
       
      ssh username@your-vps-ip
  3. Update Your System

    • Make sure your system is up-to-date:
       
      sudo apt update && sudo apt upgrade -y

Step 2: Install Varnish Cache

  1. Add Varnish Repository

    • To install the latest stable version of Varnish, add its official repository to your system:
       
      sudo apt install -y software-properties-common sudo add-apt-repository ppa:varnishcache/ppa sudo apt update
  2. Install Varnish

    • Now, install Varnish Cache:
       
      sudo apt install varnish -y
  3. Verify the Installation

    • After installation, verify that Varnish is installed correctly by checking its version:

       
      varnishd -v
    • You should see output indicating the installed version of Varnish.


Step 3: Configure Varnish Cache

  1. Default Configuration Location

    • The main Varnish configuration file is located at /etc/varnish/default.vcl. You’ll need to edit this file to define caching rules and backends.
  2. Configure the Backend Server

    • Open the default.vcl file in your favorite text editor:

       
      sudo nano /etc/varnish/default.vcl
    • Set up your backend server, typically pointing to your web server (e.g., Nginx or Apache) which is running on port 8080:

      vcl
       
      backend default { .host = "127.0.0.1"; .port = "8080"; }
  3. Configure Varnish to Listen on Port 80

    • By default, Varnish listens on port 6081. We’ll change it to port 80 to handle incoming web traffic. Open the Varnish service configuration file:

       
      sudo nano /etc/systemd/system/varnish.service
    • Modify the file to configure Varnish to listen on port 80:

      ini
       
      ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,1G
    • Save and exit the file.

  4. Adjust Firewall Rules (if necessary)

    • If you have a firewall running, make sure ports 80 (HTTP) and 6082 (for Varnish administration) are open:
       
      sudo ufw allow 80/tcp sudo ufw allow 6082/tcp sudo ufw reload
  5. Enable and Restart Varnish

    • After modifying the configuration, reload the Varnish service:
       
      sudo systemctl daemon-reload sudo systemctl enable varnish sudo systemctl start varnish

Step 4: Integrate Varnish with Your Web Server (Nginx/Apache)

If you are using a web server like Nginx or Apache, you need to configure it to listen on a different port (e.g., 8080) because Varnish will now handle requests on port 80.

For Nginx:

  1. Modify Nginx to Listen on Port 8080

    • Edit the Nginx configuration to make it listen on port 8080 instead of port 80:

       
      sudo nano /etc/nginx/sites-available/default
    • Change the listen directive from 80 to 8080:

      nginx
       
      server { listen 8080; server_name yourdomain.com; ... }
  2. Restart Nginx

    • After saving the configuration, restart Nginx to apply the changes:
       
      sudo systemctl restart nginx

For Apache:

  1. Modify Apache to Listen on Port 8080

    • Open Apache’s ports configuration:

       
      sudo nano /etc/apache2/ports.conf
    • Add a Listen 8080 directive if it is not already present:

      apache
       
      Listen 8080
  2. Modify Virtual Hosts

    • Edit your virtual host configuration file to make Apache listen on port 8080:

       
      sudo nano /etc/apache2/sites-available/000-default.conf
    • Change the VirtualHost directive to use port 8080:

      apache
       
      <VirtualHost *:8080> ... </VirtualHost>
  3. Restart Apache

    • Restart Apache to apply the changes:
       
      sudo systemctl restart apache2

Step 5: Test Varnish Cache

Once Varnish is up and running, test its cache functionality:

  1. Verify Cache Headers

    • Use curl to send a request to your website and check if Varnish is caching responses:

       
      curl -I http://yourdomain.com
    • Look for the X-Varnish and Via headers in the response:

      text
       
      HTTP/1.1 200 OK Server: Varnish X-Varnish: 123456 789012 Via: 1.1 varnish
    • If you see these headers, Varnish is successfully caching responses.

  2. Clear Cache (Optional)

    • You can clear the Varnish cache if needed by running:
       
      sudo varnishadm ban "req.url ~ /"

Step 6: Tune Varnish Cache (Optional)

To further optimize Varnish performance, consider the following settings:

  1. Increase Cache Size

    • You can increase the memory allocated for caching by editing the app.vcl file or the varnish.service file to allocate more memory for Varnish's storage backend. For example:
       
      -s malloc,2G
  2. Cache Time-to-Live (TTL)

    • You can define how long content is cached by setting a TTL value in your default.vcl file:
      vcl
       
      sub vcl_backend_response { set beresp.ttl = 1h; }

Conclusion

You have now installed and configured Varnish Cache on your VPS to improve web performance. By caching HTTP responses, Varnish reduces server load and speeds up your website, providing a better experience for users. With proper configuration and tuning, Varnish can handle high traffic loads and serve cached content quickly.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution