Varnish Cache

Installing Varnish Cache on a cPanel Server

I’ve recently added Varnish Cache to my server with cPanel installed. Varnish Caching is now installed server-wide. It acts as an invisible intermediary between clients and the server and delivers some major speed improvements, noticeably to Magento and Drupal websites.

The Varnish install script created by Shubham Mathur worked very well but it currently doesn’t consider additional IPs. Also, you’ll need something to amend the $_SERVER['REMOTE_ADDR'] server variable to show the client’s IP address and not the server’s.

So to get Varnish working correctly across multiple IPs (required when you’re hosting TLS websites), here is my step-by-step guide:

  1. In Shell, run:
    wget https://raw.githubusercontent.com/itseasy21/w3tools-varnish/master/install.sh -O ./varnish-install.sh
    sh ./varnish-install.sh
  2. Edit the Varnish configuration file located at /etc/varnish/default.vcl (see below)
  3. Install mod_remoteip either using EasyApache or collect the files from GitHub.
  4. Set Apache configuration to load mod_remoteip (I recommend adding it to pre_main_global.conf):
    LoadModule remoteip_module modules/mod_remoteip.so
    RemoteIPHeader X-Forwarded-For
  5. Restart Apache and Varnish:
    /usr/local/apache/bin/apachectl graceful
    /etc/init.d/varnish restart

Here is the default varnish configuration file. I’d recommend copying the original (fully commented out) version before starting on this one.

You can probably see where your IP address will fit in and the names for these corresponding websites – change the regular expresssion to match more precisely. The last function vcl_error is completely optional.

I will update this post when Shubham amends his script. Have a look at its source code if you need to reset the default Apache port.

Leave a Reply

Your email address will not be published. Required fields are marked *