How to Speed Up a Drupal Website's Performance Using Varnish

Leave your reply

Introduction

Learn how to improve the performance of a Drupal website using Varnish Cache. A web server (proxy) cache is an excellent way to significantly improve the page load times of a Drupal website. Varnish Cache is one of the most popular HTTP accelerators used for Drupal sites.

Varnish speeds up a website by keeping copies of static resources (like webpages and image files) in virtual memory. This allows Varnish to deliver these resources quickly when a visitor requests them.

Requirements

  • A server running Linux (CentOS 7 or Ubuntu 14.04)
  • Drupal installed and running.
  • At least one functioning Drupal website.

For more information on installing Drupal, see our articles:

Reconfigure Apache

For Varnish to work as a reverse proxy, it must be configured to run in front of Apache, so that browser requests go to Varnish first. To do this, we need to change Apache's default Listen port from 80 to 8080.

Edit the Main Apache Configuration File

Edit the main Apache configuration file with the command:

CentOS 7:

sudo nano /etc/httpd/conf/httpd.conf

Ubuntu 14.04:

sudo nano /etc/apache2/ports.conf

Scroll down to the line which reads:

Listen 80

Change the port from 80 to 8080 so that it reads:

Listen 8080

Save and exit the file.

Edit the VirtualHost Entry

Next, you will need to change the Listen ports for any virtual hosts on the server. To do this, you will need to edit the website's Apache configuration file.

By common convention, this Apache configuration file is usually:

CentOS 7:

/etc/httpd/conf.d/example.com.conf

Ubuntu 14.04:

/etc/apache2/sites-available/example.com.conf

If this is a fresh Apache installation, the file will be

/etc/apache2/sites-available/000-default.conf

Note: The location and filename of a site's Apache configuration file can vary based on how you or your server administrator has set up hosting.

Edit this file with your editor of choice, for example with the command:

CentOS 7:

sudo nano /etc/httpd/conf.d/example.com.conf

Ubuntu 14.04:

sudo nano /etc/apache2/sites-available/example.com.conf.

For a fresh Apache installation:

sudo nano /etc/apache2/sites-available/000-default.conf

Find the line which reads:

<VirtualHost *:80>

Change the port from 80 to 8080 so that it reads:

<VirtualHost *:8080>

Save and exit the file, then restart Apache for the changes to take effect:

CentOS 7:

sudo systemctl restart httpd

Ubuntu 14.04:

sudo service apache2 restart

Install and Configure Varnish

The first step is to install Varnish on your server.

Ubuntu 14.04

First, install the apt-transport-https package with the command:

sudo apt-get install apt-transport-https

Next, get the Varnish repository information with the command:

sudo curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -

Add the Varnish repository with the command:

sudo echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1" \
>> /etc/apt/sources.list.d/varnish-cache.list

Update the package list and install Varnish with the commands:

sudo apt-get update
sudo apt-get install varnish

Note: As part of the installation, Varnish will automatically start and be configured to automatically start if the server is rebooted.

For Varnish to work as a reverse proxy, it must be configured to run in front of Apache, so that browser requests go to Varnish first. To do this, we need to change Varnish's default port from 6081 to 80.

To do this, edit the Varnish configuration file with the command:

sudo nano /etc/default/varnish

Scroll down to the line which reads:

DAEMON_OPTS="-a :6081 \

Change the port from 6081 to 80 so that it reads:

DAEMON_OPTS="-a :80 \

Save and exit the file. Restart Varnish for the changes to take effect:

sudo service varnish restart

CentOS 7

First, install the EPEL repository with the command:

sudo yum install epel-release

Next, install Varnish with the command:

sudo yum install varnish

Start Varnish and enable it to automatically start if the server is rebooted with the commands:

sudo systemctl start varnish
sudo systemctl enable varnish

For Varnish to work as a reverse proxy, it must be configured to run in front of Apache, so that browser requests go to Varnish first. First, we need to change Varnish's default port from 6081 to 80.

To do this, edit the varnish.params configuration file with the command:

sudo nano /etc/varnish/varnish.params

Scroll down to the line which reads:

VARNISH_LISTEN_PORT=6081

Change the port from 6081 to 80 so that it reads:

VARNISH_LISTEN_PORT=80

Save and exit the file. Restart Varnish with the command:

sudo systemctl restart varnish

Test the Varnish Installation

After Apache has been reconfigured to listen on port 8080 and Varnish has been installed, you can check the installation by using curl to visit your website from the command line, and read the headers.

To do this, use the command:

curl -I http://localhost

Among all the other information, you will see the following headers:

X-Varnish: 5
Age: 0
Via: 1.1 varnish-v4

These confirm that Varnish served the page for your request.