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 -ysudo apt install curl gpg gnupg2 software-properties-common ca-certificates apt-transport-https lsb-release sudo add-apt-repository ppa:ondrej/phpsudo apt -y install php8.3sudo 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 = Offexpose_php = offmax_execution_time = 30max_input_time = 1000max_input_vars = 3000memory_limit = 256M display_errors = Offdisplay_startup_errors = Offlog_errors = Onhtml_errors = Offpost_max_size = 128Mfile_uploads = Onallow_url_fopen = Onshort_open_tag = Onmemory_limit = 256Mcgi.fix_pathinfo = 0upload_max_filesize = 100Mdate.timezone = Europe/London ;change this to your own timezoneopcache.enable=1opcache.memory_consumption=128opcache.max_accelerated_files=10000opcache.revalidate_freq=200opcache.jit_buffer_size=100Mopcache.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-backupsudo 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.1local-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 -ysudo 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/defaultsudo 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 coresudo snap install --classic certbotsudo ln -s /snap/bin/certbot /usr/bin/certbotsudo 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/translationschown -R www-data:www-data ~/[site name]/webchmod -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
