Skip to main content

Setting up a Drupal 9 web project from scratch on Ubuntu 2.04 (LTS)

Part of the fun on running your own website to me is hosting your own website, and I don't mean on a shared host.

I enjoy hosting on cloud servers where I can tweak the performance.

Today I wanted to write a tutorial on how to  live Drupal website on Ubuntu 12.04 in the shortest number of steps possible.

This this tutorial you will stand up a single Drupal 9 web application. I'll sprinkle in some Linux commands so you will feel me comfortable navigating your server.

For this tutorial I used Digital Ocean

Create a Droplet, select a plan and choose a data center

Select an Ubuntu 12.04 (LTS) container.

It is important for this to work exactly as written that you choose the same Linux distribution. I am going to use Ubuntu 12.04 (LTS). The reason this is important is that the commands and package names used in the tutorial will be different if you choose a different Linux distribution.

For this tutorial you can choose whatever price plan you want. I personally chose the Basic, shared CPU plan at $10 a month plan to do this tutorial. If you don't see all the price plans click the link to "See all plans." You might choose something different later depending on your needs to but this is all you need for now.

Choose a data center region closest to you.

It will affect how quickly you experience the loading of you web application.I am on the east coast so I chose New York. It shouldn't matter if you choose 1,2 or 3. When we use a remote database later, be sure to choose the same region or the latency between your server and database will be bad.

Access your Droplet using SSH

Use SSH to log in to your Droplet. DigitalOcean Droplets are Linux-based virtual machines (VMs). This is where we will install the software needed to host your Drupal web project.

ssh -p 22 root@157.111.22.333

Whether you log in with a password or with SSH keys is up to you and beyond the scope of this tutorial.

Apply update and patches to your Ubuntu virtual machine (optional)

Since this is a new box - another word for a Droplet or virtual machines - you might as well and apply any updates to your Ubuntu machine.

$ sudo apt update
$ sudo apt upgrade
$ sudo apt autoremove

Install PHP - FPM

$ apt install php7.4-fpm

You can check if it is running by using the following command

$ sudo service php7.4-fpm status

  php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-08-05 01:09:48 UTC; 8min ago
       Docs: man:php-fpm7.4(8)
    Process: 39556 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock
/etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited, status=0/SUCCESS)
   Main PID: 39552 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2345)
     Memory: 6.8M
     CGroup: /system.slice/php7.4-fpm.service
             ├─39552 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─39554 php-fpm: pool www
             └─39555 php-fpm: pool www

If for some reason the service is not running, use the following command

$ sudo service php7.4-fpm start

There are a few directories you need to be aware of to manage php-fpm

/etc/php/  - will contain all directories for all version of PHP you have installed

/etc/php/7.4/fpm/ - will contain you php fpm configuration files. This is where you can find you php.ini and add more php-fpm pools.

/var/log  - will contain your php fpm log,  php7.4-fpm.log

/var/run/php - will contain you socket that php-fpm listens to. php-fpm creates the Unix socket file after you started the process. This socket will be used for communication between php-fpm and Apache 2.4.

Install a HTTP server

For this tutorial we will install the Apache HTTP server. It is becoming popular to use NGINX; however most of Drupal's documentation is written for Apache so we will stick with it.

$ sudo apt install apache2

And let's check that it is running

$ sudo service apache2 status

You can also check in your browser by entering the IP address of your Digital Ocean droplet and adding port 80 to the end of the url.

It would look like this but you'd need to replace the ip address with the ip address if your virtual machine: 

http://157.111.22.333:80

Note this is the same ip address we used to log in earlier.

You can check the version of Apache you installed with the following command:

$ apache2 -v
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2020-04-13T17:19:17

Here are some important directories and files you need to manage Apache.

/etc/apache2 - this is where you will find your Apache http server configuration files

/etc/apache2/sites-available - this is where you will find the configuration files for Apache Virtual Hosts.

Virtual hosts are beyond the scope of this tutorial. Right now your Apache server is using the default configuration file.

Enable required Apache 2 modules

$ sudo a2enmod proxy proxy_fcgi

These are required for php-fpm and Apache 2.4 to talk to each other.

Modify the default virtual host

We are going to modify the default virtual host file, 000-default.conf.

This command will open the default virtual host in the nano editor

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

 Replace the contents of 000-default.conf with the following and save.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html
 
    <Directory /var/www/html>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
 
    <FilesMatch \.php$>
        #Reference to Unix socket
        SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
    </FilesMatch>
 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now we have to restart Apache so these changes take effect.

sudo service apache2 restart

Test that php-fpm is working

This may look a bit foreign so I will offer an explanation. We want to create a php file, and serve it from the server.

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

This command creates a php file, info.php with the contents 

<?php phpinfo(); ?>

 

You could also do this by navigating to /var/www/html and opening up the nano editor and pasting 

<?php phpinfo(); ?>

then saving the files as info.php

Navigate to your  server IP address and file name to verify php is working.

http://157.111.22.333/info.php

Prepare your server for Drupal

Install required php extensions

Drupal has required php extensions. This command should install the remaining ones. This assumes we will be using mysql for our database which we are for this tutorial.

$sudo apt install -y php7.4-{xml,gd,mbstring,json,curl,mysql}

There are other considerations like your php memory limit. This is likely taken care of in this setup.

Check or modify your php configuration file, php.ini

You can check the php memory limit setting with this command:

$ cat /etc/php/7.4/fpm/php.ini | grep "memory_limit"
memory_limit = 128M

The minimum requirement to run Drupal is 64MB. You can edit the memory_limit parameter in the loaded php.ini file. You can find it in the php-fpm directory for your version of PHP, /etc/php/7.4/fpm/php.ini.

Enable clean urls

You will likely want to use clean urls especially if you are building a website or blog. 

To this you need to enable the Apache mod_rewrite. Mod_rewrite can be used to translate human-readable URLs in dynamic websites. This makes the URL’s look cleaner and reader friendly

$ sudo a2enmod rewrite

Restart Apache for the change to take affect. 

sudo service apache2 restart

Installing Drupal 9

Navigate to the directory that is configured to serve your files. How do you know? Look at the default virtual host configuration files.

In this example the directory is /var/www/html.

$ cd /var/www/html

I am torn here because I really want to go the composer route; however I am going to stick to simplicity.

We will download Drupal the old fashion way.

$ wget https://www.drupal.org/download-latest/tar.gz

Next we need to extract the contents of tar.gz.

The biggest gotcha that is not obvious but will save you a lot of time is that you want you file permissions to be owned by the same owner and group as