How to Install Drupal on Ubuntu 18.04

In this post, we will explain how to install Drupal on an Ubuntu 18.04 VPS, together with MySQL, PHP-FPM 7.2, and Nginx.

Drupal is a free and open-source content management system (CMS) written in PHP. It’s designed for publishing web content in all sorts of formats and styles.  Drupal is one of the most popular CMSes – it is used all over the world to power thousands of websites, ranging from personal blogs, to corporate, political, and government sites.

Installing Drupal on Ubuntu 18.04 is really an easy task – just carefully follow the steps below and you should have it installed in less than 10 minutes. Let’s get started.


This guide should work on other Linux VPS systems as well but was tested and written for an Ubuntu 18.04 VPS.

Prerequisites

  • Ubuntu 18.04 VPS
  • Administrative sudo user with root privileges

Step 1: Before you Start

Run the following command to update the packages list and upgrade the system packages:

sudo apt update && sudo apt upgrade

Install the necessary packages:

sudo apt install unzip

Step 2: Install MySQL and Create a Database

If you already have MySQL or MariaDB installed you can skip this step and move to the next one.

The following command will install the latest MySQL 5.7 server from the official Ubuntu repositories:

sudo apt-get install mysql-server

Once the installation is complete, issue the following command to secure your installation. This is optional, but strongly recommended:

mysql_secure_installation

You will be prompted to answer several questions:

  • Setup VALIDATE PASSWORD plugin? (Press y|Y for Yes, any other key for No) N
  • Change root password? (Press y|Y for Yes, any other key for No) (this is optional, you can set a MySQL root password if desired)
  • Remove anonymous users? (Press y|Y for Yes, any other key for No) Y
  • Disallow root login remotely? (Press y|Y for Yes, any other key for No) Y
  • Remove test database and access to it? (Press y|Y for Yes, any other key for No) Y
  • Reload privilege tables now? (Press y|Y for Yes, any other key for No) Y

Next, we need to create a MySQL database and user for the new Magento installation.

Log in to the MySQL console:

sudo mysql

Run the following commands to create a new database and user and to grant privileges to the user:

mysql> CREATE DATABASE drupal;

mysql> GRANT ALL PRIVILEGES ON drupal.* TO 'drupal'@'localhost' IDENTIFIED BY 'my_strong_password';
mysql> FLUSH PRIVILEGES;
mysql> \q

Make sure to replace “my_strong_password” with an actual strong password.

Step 3: Install PHP 7.2 and Required PHP Modules

The newer Drupal versions are fully compatible with the default Ubuntu PHP version 7.2.

Install PHP 7.2 and all necessary PHP modules using the following command:

sudo apt install php7.2-cli php7.2-fpm php7.2-mysql php7.2-json php7.2-opcache php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

Set the recommended PHP settings for Drupal:

sudo sed -i "s/memory_limit = .*/memory_limit = 256/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/post_max_size = .*/post_max_size = 128M/" /etc/php/7.2/fpm/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 3000/" /etc/php/7.2/fpm/php.ini

Step 4: Install Drupal

Installing Drupal is pretty easy and straightforward, first download the Drupal zip archive from the Drupal download page:

wget https://ftp.drupal.org/files/projects/drupal-8.6.7.zip -O drupal.zip

Once the download is completed, unzip the archive and move the extracted files to the /var/www/mydrupalsite.com directory, which will be the root directory of your new Drupal site:

sudo unzip drupal.zip
sudo mv drupal-8.6.7/ /var/www/mydrupalsite.com

Change the ownership of the /var/www/mydrupalsite.com directory to the www-data user so that PHP and Nginx can upload and edit files:

sudo chown -R www-data: /var/www/mydrupalsite.com

Step 5: Install and Configure Nginx

If you don’t have Nginx installed on your server, you can install the latest stable version from the official Ubuntu repositories:

sudo apt install nginx

Create a new Nginx server block for your new Drupal site:

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
sudo nano /etc/nginx/sites-available/mydrupalsite.com
server {
    listen 80;
    server_name mydrupalsite.com www.mydrupalsite.com;
    root /var/www/mydrupalsite.com;

    index index.html index.htm index.php;

    charset utf-8;

    access_log /var/log/nginx/mydrupalsite.com.access.log;
    error_log /var/log/nginx/mydrupalsite.com.error.log info;

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    location ~ ^/sites/[^/]+/files/.*\.php$ {
        deny all;
    }

    location ~ (^|/)\. {
        return 403;
    }

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

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

    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location ~ '\.php$|^/update.php' {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi.conf;
    }


    location ~ ^/sites/.*/files/styles/ {
        try_files $uri @rewrite;
    }

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

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        try_files $uri @rewrite;
        expires max;
        log_not_found off;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }
}

After you have saved and closed the file, activate the server block by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/mydrupalsite.com /etc/nginx/sites-enabled/mydrupalsite.com

Make sure to replace ALL instances of “mydrupalsite.com” with your unique and registered domain name.

Once the symbolic link has been created, verify the Nginx configuration and restart the Nginx service:

sudo nginx -t
sudo systemctl restart nginx

Open http://mydrupalsite.com/ in your favorite web browser and follow the on-screen instructions to complete the Drupal installation.

 

That’s it. You have successfully installed Drupal on Ubuntu 18.04. For more information about how to manage your Drupal installation, please refer to the official Drupal documentation.


Of course, you don’t have to install Drupal on Ubuntu 18.04 if you use one of our Managed Drupal VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install Drupal on Ubuntu 18.04, or if you found it helpful, please share it with your friends on the social networks using the share shortcuts, or simply leave a reply below. Thanks.

 

Leave a Comment