HowtoForge

How to Install Drupal CMS with Apache on Debian 12

Drupal is a free and open-source web content management system written in PHP and distributed under GNU General Public License. Drupal provides a robust content management tool with sophisticated APIs for multichannel publishing.

Drupal is one of the most widely used CMS on the internet, used by at least 14% of the top 10,000 websites on the internet, and it's used for global enterprise industries, governments, education, and institutions sites. Drupal provides a high-scalable system, integrated with digital applications, and can be used to create multisite for different organizations with multilingual support.

In this guide, I will show you how to install Drupal on a Debian 12 server. We'll install Drupal on the LAMP Stack (Apache2, MariaDB, and PHP) and secure Drupal with SSL/TLS certificates from Letsencrypt.

Prerequisites

To begin, check that you have the following:

Installing Dependencies

Drupal is an open-source content management system written in PHP with MySQL/MariaDB as the database. To install Drupal, you must install both PHP and MySQL/MariaDB packages to your system.

In the following step, you will install package dependencies for Drupal, including the LAMP Stack (Apache2, MariaDB, and PHP), Composer PHP dependency manager, and some additional PHP extensions.

First, run the following apt command to update and refresh your Debian package index.

sudo apt update

Once updated, install package dependencies by executing the command below. With the following command, you will install the LAMP Stack package (Apache2, MariaDB, and PHP), Composer PHP dependency manager, and additional PHP extensions that are required by Drupal.

sudo apt install apache2 mariadb-server composer php php-apcu php-dev libapache2-mod-php libcurl4-openssl-dev php-cli php-mysql php-zip php-gd php-fpm php-json php-common php-intl php-mbstring php-curl php-xml php-pear php-tidy php-soap php-bcmath php-xmlrpc

Type y to proceed with the installation.

After dependencies are installed, verify each dependency to ensure that the installation is successful.

Verify the apache2 service using the following command. This will ensure that the apache2 service is running and enabled.

sudo systemctl is-enabled apache2
sudo systemctl status apache2

The displayed output will reveal that the apache2 service is running and enabled.

Now verify the mariadb service by executing the following command.

sudo systemctl is-enabled mariadb
sudo systemctl status mariadb

The output should be similar to this:

Next, verify the location of the Composer binary file and the installed version using the command below.

which composer
sudo -u www-data composer -v

The output confirms that Composer 2.5 is installed at /usr/bin/composer.

Lastly, verify the PHP version and PHP-enabled modules using the command below.

php -v
php -m

PHP 8.2 should be installed on your Debian machine and default PHP extensions should be enabled.

Configuring MariaDB Server

With all dependencies installed, the next step is to secure your MariaDB server installation and create a new database and user that will be used by Drupal. You will secure MariaDB via the mariadb-secure-installation utility, then you will create a new database and user via the mariadb client command line.

Execute the mariadb-secure-installation command below to secure your MariaDB server.

sudo mariadb-secure-installation

During the process, you will be asked about the following configurations:

Next, log in to the MariaDB server by executing the mariadb command below. Input your mariaDB root password when prompted.

sudo mariadb -u root -p

Once logged in, execute the following queries to create a new database drupaldb and user drupal on your MariaDB server. Be sure to change the password with your password.

CREATE DATABASE drupaldb;
CREATE USER drupal@localhost IDENTIFIED BY 'password';
GRANT ALL ON drupaldb.* TO drupal@localhost WITH GRANT OPTION;
FLUSH PRIVILEGES;

Lastly, run the following query to verify and ensure that the user drupal can access the database drupaldb.

SHOW GRANTS FOR drupal@localhost;

The displayed output below confirms that user drupal can access the database drupaldb for Drupal installation.

Configuring PHP

After configuring the MariaDB server, you will configure PHP for Drupal installation by:

Let's begin.

Installing Uploadprogress Extension via PECL

The uploadprogress extension is used to show and track the upload progress of files, which includes upload speed and estimated time remaining. The uploadprogress extension is available on PECL, which needs to be installed manually via the pecl command line.

Execute the following pecl command to install uploadprogress to your Debian system.

sudo pecl install uploadprogress

The uploadprogress installation should begin.

After the installation is finished, execute the command below to uploadprogress the extension to your PHP installation. This will create a new PHP extension configuration /etc/php/8.2/mods-available/uploadprogress.ini.

cat <<EOF | sudo tee /etc/php/8.2/mods-available/uploadprogress.ini
; configuration for php uploadprogress module
; priority 15
extension=uploadprogress.so
EOF

Lastly, execute the following command to enable the uploadprogress extension on your PHP installation.

sudo ln -s /etc/php/8.2/mods-available/uploadprogress.ini /etc/php/8.2/apache2/conf.d/15-uploadprogress.ini

Editing php.ini File

Open the default php.ini configuration /etc/php/8.2/apache2/php.ini using the following nano editor command.

sudo nano /etc/php/8.2/apache2/php.ini

Change the default PHP configuration with the following. Be sure to adjust the memory_limit and date.timezone options with your server environment.

memory_limit = 512M
upload_max_filesize = 60M
max_execution_time = 300
date.timezone = Europe/Amsterdam

Save and exit the file when you're done.

Next, run the systemctl command below to restart the apache2 service and apply the changes that you've made.

sudo systemctl restart apache2

Then, run the following command to create a new PHPINFO file /var/www/html/info.php and verify your PHP configuration.

cat <<EOF | sudo tee /var/www/html/info.php
<?php
phpinfo();
?>
EOF

Launch your web browser and visit your server IP address, such as http://192.168.10.15/info.php. If your PHP configuration is successful, you should see the PHPINFO page like the following:

Downloading Drupal Source Code

After configuring the MariaDB server and PHP installation, you will download the Drupal source code and install its PHP dependencies via Composer. In this case, you will download the latest version of Drupal.

Move your working directory /var/www and download Drupal via the wget command below. The Drupal source code will be available at drupal.tar.gz file.

cd /var/www/
wget https://www.drupal.org/download-latest/tar.gz -O drupal.tar.gz

Extract the file drupal.tar.gz via the tar command below. Then, rename the extracted directory to drupal, which means that the web root directory for Drupal will be /var/www/drupal.

tar -xvf drupal.tar.gz
mv drupal-* /var/www/drupal

Lastly, run the following command to change the permission and ownership of the Drupal web root directory /var/www/drupal. This directory will be owned by user www-data with permission 755.

sudo chown -R www-data:www-data /var/www/drupal/
sudo chmod -R 755 /var/www/drupal/

Configuring Apache2 Virtual Host

Now that you've downloaded Drupal and configured the web root directory to /var/www/drupal, the next step is to create a new Apache2 virtual host configuration that will be used to run Drupal. Before going further, ensure that you have the domain name pointed to your server IP address.

First, run the following command to enable some Apache2 modules that are required for Drupal.

sudo a2enmod rewrite ssl headers deflate

Then, create a new virtual host configuration /etc/apache2/sites-available/drupal.conf using the following nano editor command.

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

Insert the following configuration and be sure to change the ServerName option with your domain name.

<VirtualHost *:80>

    ServerName howtoforge.local
    ServerAdmin admin@howtoforge.local
    DocumentRoot /var/www/drupal

    # Add security
    php_flag register_globals off

    ErrorLog ${APACHE_LOG_DIR}/howtoforge.local.error.log
    CustomLog ${APACHE_LOG_DIR}/howtoforge.local.access.log combined

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
      SSLOptions +StdEnvVars
    </FilesMatch>

    <Directory /var/www/drupal>
        Options FollowSymlinks
        #Allow .htaccess
        AllowOverride All
        Require all granted
        <IfModule security2_module>
            SecRuleEngine Off
            # or disable only problematic rules
        </IfModule>
    </Directory>

    <Directory /var/www/drupal/>
        RewriteEngine on
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    </Directory>

</VirtualHost>

When finished, save and close the file.

Next, run the following command to activate the virtual host file drupal.conf and verify your Apache2 syntax.

sudo a2ensite drupal.conf
sudo apachectl configtest

If you have proper Apache2 syntax, the output Syntax OK will be displayed.

Now restart the apache2 service using the below command to apply the changes that you've made.

sudo systemctl restart apache2

Generating SSL/TLS Certificates Letsencrypt for Drupal

In the following step, you will secure your Drupal installation with SSL/TLS certificates from Letsencrypt. To achieve that, you must install Certbot and Certbot Apache plugin to your Debian system, then generate SSl/TLS certificates for your Drupal domain name.

Install Certbot and Certbot APache plugin using the following apt install command. Type y to proceed with the installation.

sudo apt install certbot python3-certbot-apache

Next, run the certbot command below to generate SSL/TLS certificates for your Drupal site. Be sure to change the email address and domain name in the following command.

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email alice@hwdoamin.io -d howtoforge.local

Once the process is finished, your SSL/TLS certificates will be available in the /etc/letsencrypt/live/domain.com directory. Your Drupal installation is automatically configured with HTTPS, which is configured via the Certbot Apache plugin.

Installing Drupal via Web Installer

Launch your web browser and visit the domain name of your Drupal site, such as http://howtoforge.local/. If everything goes well, you should be redirected to HTTPS connections and the Drupal installation page will be shown.

Select the default language that will be used for Drupal installation and click Save and continue.

Select the Drupal installation profile that you want to use. The Standard profile is recommended for the new site owner, while Minimal requires an in-depth understanding of Drupal. Or you can also choose a profile with Demo.

The following example will be using the Standard profile, then click Save and continue.

Now the Drupal web installer will verify your system environment for Drupal installation. You should be redirected to the database configuration if your server meets Drupal requirements.

Input details of your MariaDB database name, user, and password that Drupal will use. Then, click Save and continue again.

After configuring the database, the Drupal installation should begin.

Once the installation process is finished, configure your Drupal site. Input details of the Site name, Site email address, administrator username, password, default country, and timezone. Then, click Save and continue.

If your Drupal installation is successful, the output "Congratulations, you installed Drupal!" should be displayed on your screen.

From there, you can manage your Drupal content, site structures, and Drupal administration, and check the status of your Drupal installation. Click Manage and select the Configuration menu to configure your Drupal installation.

Additional Configuration for Drupal

To complete this guide, you will add some additional configuration to your Drupal.

First, back to your terminal server and run the following command to change the permission of the settings.php file and open it with the nano editor.

sudo chmod 644 /var/www/drupal/sites/default/settings.php
sudo nano /var/www/drupal/sites/default/settings.php

Find the "trusted_host_patterns" section and input the domain name of your Drupal installation the following.

$settings['trusted_host_patterns'] = [
  '^hwdomain\.io$',
];

Save and close the file when you're done.

Now run the following command to revert back the permission of the settings.php file to 444.

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

Next, select the Reports menu and click Status Reports.

You should see detailed information about your Drupal installation. In the following screenshot, Drupal 10 is installed with Apache2 web server 2.4, MariaDB server 10.11, and PHP 8.2.

Now click the Details link on the status report, and you will be shown the details report of your Drupal server environment.

Conclusion

Following this step-by-step guide, you've installed the Drupal Content Management System on the Debian 12 server with the LAMP Stack and SSL/TLS Letsencrypt. From here, you can now use Drupal as your website and install new themes and extensions for your Drupal CMS.

How to Install Drupal CMS with Apache on Debian 12