How to Speed Up Drupal Using Varnish on Ubuntu 16.04

speed up drupal using varnish

In this tutorial, we are going to provide you with step by step instructions on how to speed Up Drupal using Varnish on Ubuntu 16.04. Drupal is one of the leading open source content management platforms. At the time of writing this tutorial, the latest stable version of Drupal is 8.3.2 and it requires:

  • PHP 5.5.9 or higher (preferably the latest), with XML, openssl, JSON, cURL, mysqli and GD PHP extensions enabled.
  • MySQL 5.0.15, MariaDB 5.1.44, SQLite 3.3.7 or PostgreSQL 8.3 or higher.
  • Nginx, or Apache web server 2.0 or higher with proper PHP support and mod_rewrite module enabled.

This install guide assumes that Apache is already installed and configured on your virtual server.

Let’s start with the installation.

1. Update the system

Make sure your server Ubuntu OS packages are fully up-to-date:

apt-get update 
apt-get upgrade

2. Install PHP packages

Install the required PHP packages:

apt-get install php7.0 php7.0-mbstring php7.0-curl php7.0-gd php7.0-xml php7.0-mysql

3. Enable Apache rewrite module

Since clean URLs are enabled by default, mod_rewrite needs to be installed and enabled for Drupal 8 to work, so enable Apache rewrite module if it is not already done so:

a2enmod rewrite

Restart the Apache service for the changes to take effect:

service apache2 restart

4. Install Varnish

apt-get install apt-transport-https
apt-get install varnish

Once installed, Varnish ships with a default configuration file at ‘/etc/varnish/default.vcl’ that should get you up and running, but in order to take advantage of Varnish cache, you may want to do some Drupal specific tuning. These should always be adapted to fit your specific Varnish version and website’s needs.

5. Install Drupal

Download the latest version of Drupal available at https://www.drupal.org to the /opt/ directory on the server:

cd /opt/
wget https://ftp.drupal.org/files/projects/drupal-8.3.2.tar.gz
tar -xvzf drupal-8.3.2.tar.gz
mv /opt/drupal-8.3.2/ /var/www/html/drupal

All files have to be readable by the web server, so set a proper ownership:

chown www-data:www-data -R /var/www/html/drupal

6. Create new database

Create a new MySQL database and user:

mysql -u root -p
mysql> SET GLOBAL sql_mode='';
mysql> CREATE DATABASE drupaldb;
mysql> CREATE USER 'drupaluser'@'localhost' IDENTIFIED BY 'y0uR-pa5sW0rd';
mysql> GRANT ALL PRIVILEGES ON drupaldb.* TO 'drupaluser'@'localhost';d
mysql> FLUSH PRIVILEGES;
mysql> quit

Do not forget to replace ‘y0uR-pa5sW0rd’ with a strong password.

7. Configure Apache web server

Edit the /etc/apache2/ports.conf Apache configuration file and change the Apache listening port:

Listen 80

to:

Listen 8080

Create a new virtual host in Apache. Make sure the new Apache virtual host configuration contains the ‘AllowOverride All’ directive to allow Drupal’s .htaccess file to be used. For example, create a new Apache configuration file named ‘your-domain.conf’ on your virtual server:

touch /etc/apache2/sites-available/your-domain.conf
ln -s /etc/apache2/sites-available/your-domain.conf /etc/apache2/sites-enabled/your-domain.conf
vi /etc/apache2/sites-available/your-domain.conf

Then, add the following lines:

<VirtualHost *:8080>
ServerAdmin admin@your-domain.com
DocumentRoot /var/www/html/drupal/
ServerName your-domain.com
ServerAlias www.your-domain.com
<Directory /var/www/html/drupal/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/your-domain.com-error_log
CustomLog /var/log/apache2/your-domain.com-access_log common
</VirtualHost>

Remove the 000-default.conf file:

rm /etc/apache2/sites-enabled/000-default.conf

8. Configure Varnish

Stop Varnish:

service varnish stop

Edit the ‘/etc/default/varnish’ file :

vi /etc/default/varnish

Change port 6081:

DAEMON_OPTS="-a :6081 \

to 80:

DAEMON_OPTS="-a :80 \

Edit the systemd unit script:

vi /etc/systemd/system/multi-user.target.wants/varnish.service

Change port 6081:

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

to port 80:

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Reload systemd using the following command:

systemctl daemon-reload

Edit the ‘/etc/varnish/default.vcl’ file and add/modify the following lines:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
vi /etc/varnish/default.vcl
backend default {
.host = "127.0.0.1";
.port = "8080";
}

Enable varnish service to start automatically on server boot using the following command:

systemctl enable varnish

9. Restart Apache

Restart the Apache web server for the changes to take effect:

service apache2 restart

10. Start Varnish

Start the Varnish service:

service varnish start

Open http://your-domain.com in your favorite web browser, and follow the simple on-screen instructions.

Then, log in to your Drupal administration back-end and configure it according to your needs.

drupal 8 back-end
That is it. Drupal 8 and Varnish have been installed on your Ubuntu server.


Of course, you don’t have to do any of this if you use one of our Drupal VPS Hosting services, in which case you can simply ask our expert Linux admins to speed Up Drupal using Varnish for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post please share it with your friends on the social networks using the buttons below or simply leave a comment in the comments section. Thanks.

2 thoughts on “How to Speed Up Drupal Using Varnish on Ubuntu 16.04”

Leave a Comment