Installing Drupal using Composer and NGinx
published on
In this article we will show how to install Drupal using Composer and NGinx. This is the preferred method now rather than simply downloading the Drupal files, putting them in your web root and setting Drupal up.
Install PHP
First install PHP with the following commands:
sudo apt update && sudo apt upgrade -y
sudo apt install curl gpg gnupg2 software-properties-common ca-certificates apt-transport-https lsb-release
sudo add-apt-repository ppa:ondrej/php
sudo apt -y install php8.3
sudo apt install php8.3-{common, cli, bz2, zip, curl, intl, mysql, snmp, memcached, imagick, gd, fileinfo, imap, ldap, soap, tidy, xml, gd, gmp, pspell, mbstring, opcache, fpm, ssh2, imap, redis, apcu, mcrypt} openssl
Now configure PHP by editing /etc/php/8.3/fpm/php.ini
and changing it to mirror the below lines:
output_buffering = Off
expose_php = off
max_execution_time = 30
max_input_time = 1000
max_input_vars = 3000
memory_limit = 256M
display_errors = Off
display_startup_errors = Off
log_errors = On
html_errors = Off
post_max_size = 128M
file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
date.timezone = Europe/London ;change this to your own timezone
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200
opcache.jit_buffer_size=100M
opcache.jit=1255
Next edit /etc/php/8.3/fpm/pool.d/www.conf
and make sure the file is set to use sockets and points to the socket at /run/php/php8.3-fpm.sock
.
Now set up Composer
Run the following commands to download and install Composer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/bin --filename=composer
Next install MariaDB
Install MariaDB using the following commands:
sudo apt-get install mariadb-server mariadb-backup
sudo mysql_secure_installation
Then edit /etc/mysql/mariadb.conf.d/50-server.cnf
and make sure the two lines below exist:
bind-address = 127.0.0.1
local-infile=0
Next create the database Drupal will use
Run mysql -u root -p
and then enter the following commands at the SQL prompt:
CREATE DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER username@localhost IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON databasename.* TO 'username'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
Make sure to use your own databasename, username and password instead of those above.
Now install Drupal
Use the following commands to install Drupal (change [site_name]
to whatever is appropriate for you):
cd ~
composer create-project drupal/recommended-project [site_name]
cd ~/[site_name]
composer install --no-dev
Now install NGinx
Run the following commands to remove Apache (if it is already installed) and install NGinx:
sudo apt remove apache2 -y
sudo apt install nginx -y
Next edit /etc/nginx/sites-available/drupal.conf and add the following lines:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/[user]/[sitename]/web;
index index.php;
server_name [domain_name] ;
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 ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Make sure to change the root and server_name lines to match those that you will be using.
Finish off installing NGinx by running the following three commands:
sudo ln -s /etc/nginx/sites-available/drupal.conf /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart nginx
Now install Certbot to get a SSL / TLS certificate
Run the following commands to get your certificate for your site:
sudo apt-get install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot -d [sitename] --nginx
If you want to get a certificate for [sitename]
and www.[sitename] then run sudo certbot -d [sitename] -d www.[sitename] --nginx
instead of the last line above.
Finish installing and setting up Drupal
Run the following commands to make your site accessible:
mkdir -p ~/[site name]/web/sites/default/files/translations
chown -R www-data:www-data ~/[site name]/web
chmod -R 755 ~/[site name]/web
Now visit your site in your browser.
After completing the installation in your browser edit ~/[site name]/web/sites/default/settings.php
and, where you see something like the below lines update them with your own domain name:
$settings['trusted_host_patterns'] = [
'^www\.example\.com$',
];
Conclusion
You should now have a working Drupal installation. You will now need to add modules, templates etc and set up your site the way you would like it to be.
As Composer has been used to install Drupal you will need to add any additional modules using Composer.
This can be done on the command line with the following commands:
cd ~/[site name]
composer require drupal/module_name