Setup Drupal 10 On Ubuntu In 20 Minutes!

Points To Remember before you start

  • See our article HERE to setup your Drupal(linux) server
  • Check the composer requirements for the version of Drupal that you will install. 
  • For this article we will install Drupal 10 in the /srv/ directory
  • Install any package with sudo if you are not a root user
  • Don't run composer with sudo
  • This document was created December, 2022, but is updated by Simple Information staff regularly. 
  • If you get stuck, see the IN A JAM section at the bottom

Apache Web Server

How to install Apache web server

The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features including dynamically loadable modules, robust media support, and extensive integration with other popular software.

To install apache we need to run two commands given below:-

sudo apt-get update
sudo apt-get install apache2

At the end of the installation process, Ubuntu 22.04 starts Apache. The web server should already be up and running.

We can check with the systemd init system to make sure the service is running by typing:

sudo systemctl status apache2

Manage the Apache Process

Now that you have your web server up and running, we can go over some basic management commands.

To stop your web server, you can type:

sudo systemctl stop apache2

To start the web server when it is stopped, type:

sudo systemctl start apache2

To stop and then start the service again, type:

sudo systemctl restart apache2

If you are simply making configuration changes, Apache can often reload without dropping connections. To do this, you can use this command:

sudo systemctl reload apache2

By default, Apache is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:

sudo systemctl disable apache2

To re-enable the service to start up at boot, you can type:

sudo systemctl enable apache2

Apache should now start automatically when the server boots again.

Enable modrewrite Module

sudo a2enmod rewrite

References

https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04-quickstart

https://tutorials.ubuntu.com/tutorial/install-and-configure-apache

PHP

How to install PHP 8.1

In this article, we will show you how to install PHP 8.1 on Ubuntu 22.04. PHP (Hypertext Preprocessor) is an open-source server-side scripting language designed primarily for creating dynamic interactive websites. PHP is one of the most popular languages and it is freely available for redistribution and modifications. PHP can be run on almost any web server ( e.g. Nginx, Apache) and every OS platform (Linux, Mac OS, Windows). PHP 8.1 has been officially released last year and it is available for all RoseHosting clients. The latest PHP 8.1 release has new and improved features and functions that will allow developers to write better code. To install PHP 8.1 in our system we need to run followings set of command in our server

sudo apt-get install software-properties-common
sudo apt-get update
sudo apt-get install -y php8.1

Now use the following command to check installed php version on your system.

php -v

You may also need to install modules based on your application requirements. Use the following command to search available PHP 8 modules in the package repository.

sudo apt-cache search php8.1*

You may want to install the most commonly used PHP modules for Drupal. Below is the command to do so. Make sure to install packages for correct PHP version by specifying the version with the package name. Without defining the package version, it will install the latest package.

sudo apt-get install mysql-client php8.1-dev php8.1-cli php8.1-common php8.1-curl php8.1-gd php8.1-memcached php-pear php8.1-json php8.1-mbstring php8.1-intl php8.1-mysql php8.1-xml php8.1-zip php8.1-apcu php8.1-ctype php8.1-dom php8.1-iconv php8.1-imagick php8.1-opcache php8.1-pdo php8.1-mysqli php8.1-xml php8.1-tokenizer php8.1-simplexml php8.1-bcmath

References

https://www.digitalocean.com/community/tutorials/how-to-install-php-8-1-and-set-up-a-local-development-environment-on-ubuntu-22-04

Mariadb Database

How to install Mariadb 10.4

Here are the commands to run to install MariaDB 10.4 from the MariaDB repository on your Ubuntu system:

sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://ftp.heanet.ie/mirrors/mariadb/repo/10.4/ubuntu bionic main'

Once the key is imported and the repository added you can install MariaDB 10.4 from the MariaDB repository with:

sudo apt update
sudo apt install mariadb-server

Now at this stage your mariadb install successfully

How to set Root Password for mariadb

To do this, you need to stop the database from loading the grant tables, which store user privilege information. Because this is a bit of a security risk, you should also skip networking as well to prevent other clients from connecting. Start the database without loading the grant tables or enabling networking:

sudo mysqld_safe --skip-grant-tables --skip-networking &

This command will make this process run in the background so you can continue to use your terminal

Now you can connect to the database as the root user, which should not ask for a password.

mysql -u root

Now that you have root access, you can change the root password.

FLUSH PRIVILEGES;

Now run this sql command to set root password

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

note:Make sure to replace new_password with your new password of choice.

Now kill the mysql process

sudo kill `/var/run/mariadb/mariadb.pid`

Now restart mariadb

sudo systemctl start mariadb

Root password successfully updated now

Now you can confirm that the new password has been applied correctly by running:

mysql -u root -p

References

https://downloads.mariadb.org/mariadb/repositories/#distro=Ubuntu&distro_release=bionic--ubuntu_bionic&mirror=heanet-ltd&version=10.4

Composer

Installing PHP Composer

sudo apt-get update
sudo apt-get install curl

*Here's where life can get a little complicated. The latest version of composer doesn't always work with the latest version of Drupal 8/9. Make sure the composer version you install works with the Drupal 8 version that you will install. At the time of writing this article, Drupal 8.7 only supports composer version 1.7.0 although the latest version is 1.9.0 (https://getcomposer.org/download/)

At the time of writing this article Drupal 8.7 supports composer version 1.7.0

curl -sS -o composer-setup.php https://getcomposer.org/installer && php composer-setup.php --version=1.7.0

Now we make composer globally available to all users

mv composer.phar /usr/local/bin/composer

To check your composer version

composer -V

Drupal Setup

Downloading Drupal core using composer

If you've been using the root user, now is the time to switch to a regular user that can run composer.  At this point you'll probably want to set your domain (ex. MyDomain.com) to point to your new server/Drupal site. Do do this, you'll need to create a Virtual host in apache. The details are beyond the scope of this article, so please refer to: 

https://tecadmin.net/create-virtual-hosts-in-apache-on-ubuntu/

note: Make sure you point to your docroot directory (see below) in your virtual host

Run the following command from the directory for the Drupal root. If you want to use the Apache default, you can use /var/www/html/ as your site root. However, we generally use /srv/     (This command may take a few minutes to run; good time for a coffee?)

composer create-project drupal/recommended-project:10.0.0 docroot --no-interaction

This will download the current dev version of the 'drupal-composer/drupal-project' project into a folder named 'docroot' and then it automatically executes composer install which will download the latest stable version of Drupal 10 and all its dependencies.
* if you get an error similar to: Your requirements could not be resolved to an installable set of packages....see our IN A JAM section. 

Now run following bellow command from docroot

composer install

*if you get a memory swap error, see our IN A JAM section. 
Now create a config directory in docroot

mkdir config
cd config
mkdir sync

To check if drupal installed or not, check for the core directory in web folder (ex. /var/www/html/docroot/web or /srv/docroot/web)

Note: There is  no need to install drush explicitly , Its comes with the drupal 10 installation in the “vendor/bin/drush” directory.

You may want to download contributed modules and themes using Composer

Run below command from docroot

composer require 'drupal/*modulename*:*version*'
Examples:-
composer require 'drupal/token:^1.5'
composer require 'drupal/simple_fb_connect:~3.0'
composer require 'drupal/ctools:3.0.0-alpha26'
composer require 'drupal/token:1.x-dev'

References

To know about composer version notation see below link

https://getcomposer.org/doc/articles/versions.md

To know about drupal installation in details see below link

https://www.drupal.org/docs/develop/using-composer/using-composer-to-install-drupal-and-manage-dependencies

Run Drupal

Now you should be able to see the fruits of your hard work by running your instance. 

http://172.105.xxx.xxx/core/install.php

Here, substitute your IP address for 172.105.xxx.xxx. If you have already configured apache to use your own domain, you can use that instead of the IP address. 

IN A JAM

Composer Issues:

Issue : webflo/drupal-core-require-dev 10.x-dev requires behat/mink-selenium2-drive requires behat/mink-selenium2-driver 1.3.x

  • Just go to /srv/docroot and remove the following lines from composer.json below
  • "require-dev": { "webflo/drupal-core-require-dev": "^10.0" },
  • DO NOT RUN composer create-project again

Refer :

https://www.drupal.org/forum/support/upgrading-drupal/2019-01-20/composer-update-issue-867

Issue : If youre getting [ErrorException] proc_open(): fork failed - Cannot allocate memory. Run the below command one after another can fix your issue

Run this command one after another

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

Did we miss something or not get something right? Contact Us