Install Drupal 9 with Nginx on Ubuntu 20.04

Updated on July 26, 2021
Install Drupal 9 with Nginx on Ubuntu 20.04 header image

Introduction

Drupal is an open-source PHP-based CMS (Content Management System) platform used to build websites and personal blogs. It's popular and has many free templates and plugins. In this article, you'll learn how to install Drupal 9 with Nginx and a LEMP stack on Ubuntu 20.04.

Prerequisites

1. Configure Nginx for Drupal

After installing the LEMP stack, you'll need to set up a few things to get Drupal running.

Edit Nginx server default configuration block to use PHP Processor. This is because we'll be using the IP address to access the portal.

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

Replace all the contents of the file with the bellow code. Next, modify the example IP address (192.0.2.12) to your server IP address.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/drupal;

    index index.html index.htm index.nginx-debian.html index.php;

    server_name 192.0.2.12;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Test Nginx configuration.

$ sudo nginx -t

Restart the Nginx service to apply the changes.

$ sudo systemctl restart nginx

2. Configure MariaDB for Drupal

Connect to MariaDB as root.

$ sudo mariadb

Create a database named drupal.

CREATE DATABASE drupal;

Grant all privileges to database drupal.

GRANT ALL ON drupal.* TO 'drupal'@'localhost' IDENTIFIED BY 'StrongPassword';

Reload the changes.

FLUSH PRIVILEGES;

Exit MariaDB.

\q

3. Install Drupal

Install unzip.

$ sudo apt install unzip

Download the latest version of the Drupal from Drupal download page.

$ wget https://ftp.drupal.org/files/projects/drupal-9.1.4.zip

Extract the compressed file and browse to it.

$ sudo unzip drupal-9.1.4.zip
$ cd drupal-9.1.4

Create directory /var/www/html/drupal.

$ sudo mkdir /var/www/html/drupal

Move all the files within the current directory to /var/www/html/drupal directory.

$ sudo mv * /var/www/html/drupal

Go to /var/www/html/drupal/sites/default directory and copy the default.settings.php configuration file to settings.php.

$ cd /var/www/html/drupal/sites/default
$ sudo cp default.settings.php settings.php

Set the ownership of the drupal directory to the current user and group.

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

Access the Drupal page through your browser using your IP address. For example:

http://192.0.2.12

Conclusion

You have installed Drupal on your server. You can now configure your database credentials.

Use drupal as the database name. Use drupal as the database user name Use the strong password you chose for the database password.