Drupal Installation and Configuration

Drupal is a powerful, open-source content management system used to build complex websites and applications. Known for its flexibility, extensibility, and robust security features, Drupal serves millions of websites worldwide. This comprehensive guide covers the complete installation process, including setting up the LAMP/LEMP stack, installing Drupal with Composer, configuring the database, enabling essential modules, and implementing security hardening techniques.

Table of Contents

Prerequisites

Ensure you have the following before beginning:

  • Root or sudo access on Ubuntu 20.04 LTS or later
  • A valid domain name configured in DNS
  • Minimum 2GB RAM (4GB recommended)
  • 20GB available disk space
  • Basic Linux command line proficiency

Update system packages:

sudo apt update && sudo apt upgrade -y

System Requirements

Drupal requires specific software components to run properly. Verify compatibility with your server specifications.

Check current OS version:

lsb_release -a
uname -a

Install wget and curl for downloading resources:

sudo apt install -y wget curl git

Web Server Installation

Install either Apache or Nginx. Apache is recommended for Drupal beginners due to simpler configuration.

Apache Installation

Install Apache and required modules:

sudo apt install -y apache2 apache2-utils

Enable necessary Apache modules:

sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod ssl
sudo a2enmod proxy_fcgi
sudo a2enmod setenvif

Start and enable Apache:

sudo systemctl start apache2
sudo systemctl enable apache2

Nginx Installation

Alternatively, install Nginx for better performance:

sudo apt install -y nginx

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

PHP and Extensions Setup

Install PHP 8.1 or later with required extensions for Drupal:

sudo apt install -y php-fpm php-cli php-common php-mysql php-xml php-gd php-curl php-pdo php-mbstring php-zip php-bcmath php-json php-xmlrpc php-opcache

Check PHP version:

php --version

Verify installed extensions:

php -m

Configure PHP for production use:

sudo nano /etc/php/8.1/fpm/php.ini

Update critical PHP settings:

max_execution_time = 300
memory_limit = 256M
upload_max_filesize = 100M
post_max_size = 100M
max_input_vars = 5000

Restart PHP-FPM:

sudo systemctl restart php8.1-fpm

Database Configuration

Install MariaDB database server:

sudo apt install -y mariadb-server

Start and secure MariaDB:

sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation

Create Drupal database and user:

sudo mysql -u root -p << EOF
CREATE DATABASE drupal;
CREATE USER 'drupal'@'localhost' IDENTIFIED BY 'SecurePassword123!';
GRANT ALL PRIVILEGES ON drupal.* TO 'drupal'@'localhost';
FLUSH PRIVILEGES;
EXIT;
EOF

Test database access:

mysql -u drupal -p -h localhost drupal
show tables;
exit;

Composer Installation

Composer is required to manage Drupal and its dependencies:

sudo apt install -y composer

Verify Composer installation:

composer --version

Drupal Installation

Create web root directory:

sudo mkdir -p /var/www/drupal
sudo chown -R www-data:www-data /var/www/drupal
cd /var/www/drupal

Download Drupal with Composer:

composer create-project drupal/recommended-project .

Install additional useful packages:

cd /var/www/drupal
composer require drupal/admin_toolbar drupal/pathauto drupal/redirect drupal/metatag drupal/yoast_seo

Set proper permissions:

sudo chown -R www-data:www-data /var/www/drupal
sudo find /var/www/drupal -type f -exec chmod 644 {} \;
sudo find /var/www/drupal -type d -exec chmod 755 {} \;

Create settings.php from template:

sudo cp /var/www/drupal/web/sites/default/default.settings.php /var/www/drupal/web/sites/default/settings.php
sudo chmod 644 /var/www/drupal/web/sites/default/settings.php
sudo chown www-data:www-data /var/www/drupal/web/sites/default/settings.php

Create files directory:

sudo mkdir -p /var/www/drupal/web/sites/default/files
sudo chown -R www-data:www-data /var/www/drupal/web/sites/default/files
sudo chmod -R 755 /var/www/drupal/web/sites/default/files

Initial Configuration

Apache Configuration

Create Apache virtual host:

sudo nano /etc/apache2/sites-available/drupal.conf

Add configuration:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/drupal/web

    <Directory /var/www/drupal/web>
        AllowOverride All
        Require all granted
        
        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^ index.php [QSA,L]
        </IfModule>
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/drupal_error.log
    CustomLog ${APACHE_LOG_DIR}/drupal_access.log combined
</VirtualHost>

Enable the site:

sudo a2ensite drupal.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

Nginx Configuration

Create Nginx configuration:

sudo nano /etc/nginx/sites-available/drupal.conf

Add configuration:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/drupal/web;
    index index.php;

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

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\. {
        deny all;
    }

    location ~ ~$ {
        deny all;
    }
}

Enable the site:

sudo ln -s /etc/nginx/sites-available/drupal.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Module Installation

Access Drupal installation wizard:

Visit https://example.com and follow the installation wizard. Configure the database settings when prompted:

Database type: MySQL, MariaDB, or equivalent
Database name: drupal
Database username: drupal
Database password: SecurePassword123!
Database host: localhost

Complete the installation to create an admin account.

Install essential modules via Drupal UI or command line:

cd /var/www/drupal
drush module:install admin_toolbar pathauto redirect metatag

Verify installed modules:

drush module:list

Security Hardening

Implement security best practices:

Update settings.php with security settings:

sudo nano /var/www/drupal/web/sites/default/settings.php

Add at the end of settings.php:

$settings['update_fetch_url'] = FALSE;
$settings['allow_authorize_operations'] = FALSE;
$settings['file_chmod_directory'] = 0755;
$settings['file_chmod_file'] = 0644;

Set proper file permissions:

sudo chmod 444 /var/www/drupal/web/sites/default/settings.php
sudo chmod 444 /var/www/drupal/web/sites/default/settings.local.php
sudo chmod 555 /var/www/drupal/web/sites/default

Configure robots.txt:

sudo nano /var/www/drupal/web/robots.txt

Disable unused modules:

cd /var/www/drupal
drush module:uninstall comment rdf

Enable and configure security module:

cd /var/www/drupal
drush module:install security_review

Configure HTTPS and SSL certificate:

sudo apt install -y certbot python3-certbot-apache
sudo certbot certonly --apache -d example.com -d www.example.com

Performance Optimization

Enable caching:

cd /var/www/drupal
drush config:set system.performance cache.page.max_age 3600
drush config:set system.performance cache.page.use_internal 1

Enable Twig caching:

sudo nano /var/www/drupal/web/sites/default/settings.php

Add configuration:

$settings['twig_cache'] = TRUE;
$settings['twig_auto_reload'] = FALSE;

Configure Drush for better performance:

cd /var/www/drupal
drush core:cron

Set up cron job:

sudo crontab -e

Add:

*/15 * * * * cd /var/www/drupal && drush core:cron >> /var/log/drupal_cron.log 2>&1

Backup and Updates

Create backup script:

sudo nano /usr/local/bin/drupal-backup.sh

Add:

#!/bin/bash

BACKUP_DIR="/backups/drupal"
DRUPAL_DIR="/var/www/drupal"
DATE=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR

# Database backup
mysqldump -u drupal -p'SecurePassword123!' drupal | gzip > "$BACKUP_DIR/drupal-db-$DATE.sql.gz"

# Files backup
tar -czf "$BACKUP_DIR/drupal-files-$DATE.tar.gz" "$DRUPAL_DIR"

# Keep only 30 days of backups
find $BACKUP_DIR -type f -mtime +30 -delete

echo "Backup completed at $DATE"

Make executable:

sudo chmod +x /usr/local/bin/drupal-backup.sh

Schedule backups:

sudo crontab -e

Add:

0 2 * * * /usr/local/bin/drupal-backup.sh >> /var/log/drupal-backup.log 2>&1

Update Drupal:

cd /var/www/drupal
composer update
drush updatedb
drush cache:rebuild

Conclusion

Your Drupal installation is now fully configured with a robust web server, database backend, and security hardening measures in place. Regular backups, security updates, and performance optimization ensure a stable, secure, and fast content management system. Monitor system resources, keep modules updated, and follow Drupal security guidelines to maintain your site's integrity and performance.