How to Install Drupal with LEMP on CentOS Stream 9

Drupal is a commonly used PHP-based CMS (Content Management System). For small businesses, personal blogs & websites, Drupal can be a good fit.  In this article, we are going to learn how to install Drupal on CentOS Stream 9. So, let’s get started.

Pre-requisites :

  • A system with CentOS Stream 9 installed and running.

  • root access to the system.

  • LEMP Stack installed and running, for this, you can refer to one of our guides on installing the LEMP Stack (Nginx, MariaDB and PHP).

Once you're all set, we'll proceed with Drupal installation and configuration.

Install Wget & Developer Tools

yum groupinstall "Development tools"

yum install wget

Download Drupal

First, navigate to the /var/www/html directory as follows:

cd /var/www/html

Now, download the latest Drupal archive from the official website of Drupal with the following command:

wget https://ftp.drupal.org/files/projects/drupal-9.2.10.tar.gz

Check for the latest version here : Drupal Official.

Check for files with the following command.

ls

Now, extract the Drupal archive file drupal-9.2.10.tar.gz with the following command:

tar xvzf drupal-9.2.10.tar.gz

Once the Drupal archive file drupal-9.2.10.tar.gz is extracted, a new directory drupal-9.2.10 should be created in the folder. Now, you can remove the drupal-9.2.10.tar.gz file as follows:

rm -v drupal-9.2.10.tar.gz

Rename the folder to drupal

mv drupal-9.2.10 drupal

Setting up FilePermission & Ownership

Create a directory to store website files and rename the default.settings.php file as shown below:

mkdir /var/www/html/drupal/sites/default/files

cp /var/www/html/drupal/sites/default/default.settings.php /var/www/html/drupal/sites/default/settings.php

Now, change the owner and group of the drupal/ directory and its contents to Nginx as follows:

chown -R nginx:nginx /var/www/html/drupal/

Now, change the permission or the drupal/ directory and its content to 775 as follows:

chmod -Rf 775 drupal/sites/

For, some update needed 777.

chmod -Rf 777 drupal/sites/files

chcon -R -t httpd_sys_content_rw_t /var/www/html/drupal/sites/

Enable Firewall if not already done.

sudo firewall-cmd --add-service={http,https} --permanent

sudo firewall-cmd --reload

Configuring Nginx vHost

First, create a php-fpm configuration file for Drupal with the following command:

vi /etc/php-fpm.d/drupal.conf

Add the following codes:

[drupal]
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen = /run/php-fpm/drupal.sock
pm = ondemand
pm.max_children =  50
pm.process_idle_timeout = 10s
pm.max_requests = 500
chdir = /

Now, press i to go to INSERT mode and type in the following lines of codes in the drupal.conf file.

Create a new Nginx configuration file drupal.conf for Drupal with the following command:

vi /etc/nginx/conf.d/drupal.conf

Add the following codes:

server {
listen 80;
server_name drupal.crowncloud.com;

root /var/www/html/drupal;

access_log /var/log/nginx/drupal.crowncloud.com.access.log;
error_log /var/log/nginx/drupal.crowncloud.com.error.log;

location = /favicon.ico {
    log_not_found off;
    access_log off;
}

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

location ~ \..*/.*\.php$ {
    return 403;
}

location ~ ^/sites/.*/private/ {
    return 403;
}

# Block access to scripts in site files directory
location ~ ^/sites/[^/]+/files/.*\.php$ {
    deny all;
}
location ~ (^|/)\. {
    return 403;
}

location / {
    try_files $uri /index.php?$query_string;
}

location @rewrite {
    rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ /vendor/.*\.php$ {
    deny all;
    return 404;
}

location ~ '\.php$|^/update.php' {
    fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
    include fastcgi_params;
    # Block httpoxy attacks. See https://httpoxy.org/.
    fastcgi_param HTTP_PROXY "";
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php-fpm/drupal.sock;
}
location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
    try_files $uri @rewrite;
}

# Handle private files through Drupal. Private file's path can come
# with a language prefix.
location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
    try_files $uri /index.php?$query_string;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
    try_files $uri @rewrite;
    expires max;
    log_not_found off;
}
}

Change drupal.crowncloud.com with Your Domain Name. If you are using with IP, Please remove the entire server_name line.

Now, press the Esc key, and type :wq! and press the Enter key to save and exit the file.

Now, restart & check the Nginx & PHP-FPM service with the following commands:

systemctl restart php-fpm

systemctl restart nginx.service

Configure SELinux and Firewall

Use the following codes if you enabled SELinux already:

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/drupal(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/drupal/sites/default/settings.php'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/drupal/sites/default/files'
restorecon -Rv /var/www/html/drupal
restorecon -v /var/www/html/drupal/sites/default/settings.php
restorecon -Rv /var/www/html/drupal/sites/default/files

Next, allows Drupal to send outbound emails with the following command:

setsebool -P httpd_can_sendmail on

Enable Firewall:

firewall-cmd --permanent --add-service=http

firewall-cmd --permanent --add-service=https

firewall-cmd --reload

Configuring Database

Now create a Database, User & Password for Drupal:

mysql -u root

CREATE DATABASE drupal;

CREATE USER 'username'@'localhost' IDENTIFIED BY 'strong_password';

GRANT ALL PRIVILEGES ON drupal.* TO 'username'@'localhost';

FLUSH PRIVILEGES;

exit

Configuring Drupal

Now open the IP address from your browser, this will redirect you to configuring the final parts of Drupal installation.

http://IP_address

Replace the IP_address with the actual IP of the server.

Input the Database details which was configured earlier.

Now you have successfully installed Drupal with LEMP on your server.