How to Install Drupal with Nginx and Varnish on Ubuntu Linux

This article describes steps one can take to install Drupal CMS with Nginx and Varnish proxy support on Ubuntu Linux.

Drupal performs great with caching proxy server like Varnish. When looking for an easy way to speed up your Drupal websites the steps below is a good place to start.

When dealing with high traffic Drupal websites or blogs you may want to implement some kind of caching mechanism to speed up the sites.

One of the popular proxy servers that’s also a caching server is Varnish. If you want to build a fast Drupal site with lots of web traffic, setting up Varnish as a proxy caching server will go a long way.

How to install Drupal CMS with Nginx and Varnish support on Ubuntu Linux

As described above, Drupal performs great with caching proxy server like Varnish. When looking for an easy way to speed up your Drupal websites the steps below is a good place to start.

Below is how to do that on Ubuntu Linux.

Install Nginx

To install Nginx, run the commands below

sudo apt update
sudo apt install nginx

Next, run the commands below to stop, start and enable Nginx service to always start up with the server boots.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Install Varnish

Now that Nginx is installed, run the commands below to install Varnish

sudo apt-get install varnish

After installing Varnish, the commands below can be used to start, stop and enable Varnish to always start up when the server boots

sudo systemctl stop varnish.service
sudo systemctl start varnish.service
sudo systemctl enable varnish.service

Switch port to 8080

Nginx listens on port 80 by default. Since we want Varnish to listen for all traffic coming to port 80, let’s configure Nginx to use another port number.

sudo nano /etc/nginx/sites-available/default

Then make the highlighted change below.

server {
    listen 8080 default_server;
    listen [::]:8080 default_server;
    root /var/www/html/wordpress;

Save then file and exit.

After that, run the commands below to disable Nginx default site.

sudo rm /etc/nginx/sites-enabled/default

Then restart Nginx

sudo systemctl restart nginx.service

Now Nginx default site is disabled.

Install MariaDB

To install MariaDB run the commands below

sudo apt-get install mariadb-server mariadb-client

After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

After that, run the commands below to secure MariaDB server by creating a root password and disallowing remote root access.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

Install PHP 7.1-FPM and Related Modules

PHP 7.1 isn’t available on Ubuntu default repositories. In order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third-party repository to upgrade to PHP 7.1

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.1

sudo apt update

Run the commands below to install PHP 7.1 and related modules.

sudo apt install php7.1-fpm php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-zip php7.1-curl

After install PHP 7.1, run the commands below to open FPM PHP default file.

sudo nano /etc/php/7.1/fpm/php.ini

Then make the changes on the following lines below in the file and save. The value below are great settings to apply in your environments.

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
cgi.fix_pathinfo = 0
date.timezone = America/Chicago

Create Drupal Database

Now that you’ve installed all the packages are installed, run the commands below to create a blank Drupal database.

Logon to MariaDB database server

sudo mysql -u root -p

Then create a database called drupal

CREATE DATABASE drupal;

Create a database user called drupaluser with new password

CREATE USER 'drupaluser'@'localhost' IDENTIFIED BY 'new_password_here';

Then grant the user full access to the database.

GRANT ALL ON drupal.* TO 'drupaluser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

Download Drupal Latest Release

Next, visit Drupal site and download the latest package. or run the commands below to download and extract Drupal content.

After downloading, run the commands below to extract the downloaded file and move it into a new Drupal root directory.

cd /tmp && cd /tmp && wget 
tar -zxvf drupal*.gz
sudo mv drupal-8.4.2 /var/www/html/drupal

Then run the commands below to set the correct permissions for Drupal to function properly.

sudo chown -R www-data:www-data /var/www/html/drupal/
sudo chmod -R 755 /var/www/html/drupal/

Configure Nginx server block

Finally, configure Nginx server block configuration file for Drupal. This file will control how users access Drupal content.

Run the commands below to create a new configuration file called drupal

sudo nano /etc/nginx/sites-available/drupal

Then copy and paste the content below into the file and save it. Replace the highlighted line with your own domain name and directory root location.

server {
    listen 8080;
    listen [::]:8080;
    root /var/www/html/drupal;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    location / {
    try_files $uri /index.php?$query_string;        
    }

    location @rewrite {
               rewrite ^/(.*)$ /index.php?q=$1;
        }

    location ~ [^/]\.php(/|$) {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
               try_files $uri @rewrite;
        }

    location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
        }
}

Save the file and exit.

Enable the server block

After configuring the server block above, enable it by running the commands below

sudo ln -s /etc/nginx/sites-available/drupal /etc/nginx/sites-enabled/

To load all the settings above, restart Nginx by running the commands below.

sudo systemctl restart nginx.service

Configure Varnish to use port 80

Now that port 80 is free, let’s configure Varnish to use that post instead. To assign port 80 to Varnish, run the commands below.

Varnish default configure file is location at /etc/default/varnish

Open it by running the commands below:

sudo nano /etc/default/varnish

Then look for the config block under Alternative 2 and make the highlighted changes as shown below.

## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.
#
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"

Save the file when you’re done.

Next, run the commands below to open the default.vcl file

sudo nano /etc/varnish/default.vcl

Then add the highlighted lines shown below under sub vcl_recv and sub vcl_backend_response

# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8080";
}

sub vcl_recv {
if (req.url ~ "^/status\.php$" ||
      req.url ~ "^/update\.php" ||
      req.url ~ "^/install\.php" ||
      req.url ~ "^/apc\.php$" ||
      req.url ~ "^/admin" ||
      req.url ~ "^/admin/.*$" ||
      req.url ~ "^/user" ||
      req.url ~ "^/user/.*$" ||
      req.url ~ "^/users/.*$" ||
      req.url ~ "^/info/.*$" ||
      req.url ~ "^/flag/.*$" ||
      req.url ~ "^.*/ajax/.*$" ||
      req.url ~ "^.*/ahah/.*$" ||
      req.url ~ "^/system/files/.*$") {

    return (pass);
}

if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") {
    unset req.http.Cookie;
   }

if (req.http.Cookie) {
   
    set req.http.Cookie = ";" + req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|SSESS[a-z0-9]+|NO_CACHE)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
      unset req.http.Cookie;
}
    else {
      return (pass);
    }
}

sub vcl_backend_response { 
if (bereq.url ~ "(?i)\.(css|js|jpg|jpeg|gif|png|ico)(\?.*)?$") {
        unset beresp.http.set-cookie;
    }
} 

Save the file and close out.

When you’re done, restart Varnish and Nginx

sudo systemctl start nginx.service
sudo systemctl start varnish.service

Next, run the commands below to start Varnish if it won’t start.

sudo /usr/sbin/varnishd -a :80 -b localhost:8080

After that, open your browser and browse to your domain name to launch WordPress configuration wizard.


And you should see WordPress setup wizard

varnish drupal ubuntu

Follow the wizard until you’ve successfully setup WordPress.

That’s it!