Creating a VPS Template with Drupal 9 on Centos 8

We continue to expand our marketplace. We recently talked about how made a Gitlab image, and this week Drupal appeared in our marketplace.

We tell why we chose it and how the image was created.

Creating a VPS Template with Drupal 9 on Centos 8

Drupal is a convenient and powerful platform for creating any type of sites: from microsites and blogs to large social projects, which is also used as the basis for web applications, written in PHP and using relational databases as data storage.

Drupal 9 includes all the features introduced in version 8.9. The key difference between version 9 and version 8 is that the platform will continue to receive updates and security fixes after November 2021. Also in version 9, the upgrade process has been simplified, making the upgrade process from version 8 even easier.

Server Requirements

To use Drupal, it is recommended to use 2 GB of RAM and 2 CPU cores.

The main Drupal files are about 100 MB, additionally you will need space to store images, database, themes, add-ons and backups, which will depend on the size of your site.

Drupal 9 requires PHP 7.4 or higher with a minimum limit (memory_limit) to 64 MB memory, in case of using additional modules, it is recommended to install 128 MB.

Drupal can use Apache or Nginx as a web server, and MySQL, PostgreSQL or SQLite as a database.

We will install Drupal using Nginx and MySQL.

Installation

Update the installed packages to the latest version:

sudo dnf update -y

Let's add a permanent permission for incoming traffic to http/80 and https/443 ports:

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

Apply the new firewall rules:

sudo systemctl reload firewalld

Install Nginx:

sudo dnf install nginx -y

Let's start and enable the Nginx server:

sudo systemctl start nginx
sudo systemctl enable nginx

Since PHP 7.2 is currently used in the main Centos repository, let's add the REMI repository with PHP 7.4 (the minimum version for Drupal 9).
To do this, add the EPEL repository (required by the REMI repository):

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Let's add the REMI repository:

sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Enable the php:remi-7.4 module to install php 7.4:

sudo dnf module enable php:remi-7.4 -y

Install php-fpm and php-cli:

sudo dnf install -y php-fpm php-cli

Install the PHP modules required for Drupal to work:

sudo dnf install -y php-mysqlnd php-date php-dom php-filter php-gd php-hash php-json php-pcre php-pdo php-session php-simplexml php-spl php-tokenizer php-xml

We will also install the recommended PHP modules mbstring opcache:

sudo dnf install -y php-mbstring php-opcache

Install MySQL server:

sudo dnf install mysql-server -y

Enable and start the MySQL server:

sudo systemctl start mysqld
sudo systemctl enable mysqld

Since we are making a template for VDS, and they can be slow, we will add a mysqld start delay of 30 seconds, otherwise there may be problems starting the server during the initial system boot:

sudo sed -i '/Group=mysql/a 
ExecStartPre=/bin/sleep 30
' /usr/lib/systemd/system/mysqld.service

Let's change the group and user under which nginx will run by making changes to /etc/php-fpm.d/www.conf:

sudo sed -i --follow-symlinks 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf
sudo sed -i --follow-symlinks 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf

Change the owner of the PHP session directory to nginx accordingly:

sudo chown -R nginx. /var/lib/php/session

Let's remove the comment lines from the /etc/nginx/nginx.conf configuration file (so that there are no double hits for sed):

sudo sed -i -e '/^[ t]*#/d'  /etc/nginx/nginx.conf

Add gzip compression settings to /etc/nginx/nginx.conf

sudo sed -i '/types_hash_max_size 2048;/a 

    gzip on;
    gzip_static on;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/x-icon image/svg+xml application/x-font-ttf;
    gzip_comp_level 9;
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_disable "msie6";
    gzip_vary on; 
' /etc/nginx/nginx.conf

Let's add the settings of the index.php index file to /etc/nginx/nginx.conf:

sudo sed -i '/        root         /usr/share/nginx/html;/a 
        index index.php index.html index.htm;
' /etc/nginx/nginx.conf

Let's add settings for the default server processing php through the php-fpm socket, disable the log for static files, increase the expire time, disable the access and error log for favicon.ico and robots.txt and deny access to .ht files for everyone:

sudo sed -i '/        location / {/a 
		try_files $uri $uri/ /index.php?q=$uri&$args;
        }
    
        location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico|woff)$ {
        access_log off;
        expires max;
        }
    
        location ~ .php$ {
        try_files  $uri =404;
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_ignore_client_abort off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        }
    
        location = /favicon.ico {
        log_not_found off;
        access_log off;
        }
    
        location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
        }
    
        location ~ /.ht {
        deny all;' /etc/nginx/nginx.conf

Install wget required to install certbot:

sudo dnf install wget -y

Download the certbot executable file from offsite:

cd ~
wget https://dl.eff.org/certbot-auto

Move certbot to /usr/local/bin/:

mv certbot-auto /usr/local/bin/certbot-auto

And assign rights and ownership to root:

chown root /usr/local/bin/certbot-auto
chmod 0755 /usr/local/bin/certbot-auto

Install the dependencies of certbot and stop it at this stage (Answers: Y, c):

certbot-auto

Download the archive with the latest version of Drupal 9 from offsite:

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

Install tar to unpack the archive:

sudo dnf install tar -y

Delete the default files in the /usr/share/nginx/html/ directory:

rm -rf /usr/share/nginx/html/*

Unzip the files to the web server directory:

tar xf tar.gz -C /usr/share/nginx/html/

Move the files from the subdirectory to the root directory of the web server:

mv /usr/share/nginx/html/drupal-9.0.7/* /usr/share/nginx/html/

Delete subdirectory:

rm -rf /usr/share/nginx/html/drupal-9.0.7

Delete the archive with the installation files:

rm -f ./tar.gz

Set the owner of the nginx files:

chown -R nginx. /usr/share/nginx/html

At this stage, we will turn off the server and take a snapshot:

shutdown -h now

After starting the VDS from the snapshot, we will perform the initial setup of the MySQL server by running the script:

mysql_secure_installation

Enable the password validator:

Would you like to setup VALIDATE PASSWORD component? : y

Set the MySQL root user password:

New password:
Re-enter new password:

Remove anonymous users:

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Let's prevent root from connecting remotely:

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

Let's remove the test database:

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

Reload the privilege tables:

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

After that, to complete the installation, we can go to vps_ip_address
At this address we will see a page with the installation of Drupal.

Select the language to be used. For example: Russian. Click "Save and Continue"

Let's choose an installation profile (demo is used only for familiarization with the system). In our case, let it be "standard".

On the next page, give the database a name, such as "drupal". Specify the database username root and the password given to him when starting mysql_secure_installation. Click "Save and Continue".

Let's wait for the installation and updating of translations to complete (the process may take several minutes).

Specify the name of the site, set the email of the site (on whose behalf site notifications will come), login, password and email of the Drupal administrator account. We will also set the country and time zone in the regional settings. And complete the installation by clicking "Save and Continue".

After that, you can go to the control panel with the created Drupal administrator login and password.

HTTPS setting (optional)

To configure HTTPS, the VDS must have a valid DNS name, specify in

/etc/nginx/nginx.conf

in the server section the server name (for example):

server_name  domainname.ru;

Restart nginx:

service nginx restart

Let's start certbot:

sudo /usr/local/bin/certbot-auto --nginx

Enter your e-mail, agree to the terms of service (A), Subscribe to the newsletter (optional) (N), select the domain names for which you want to issue a certificate (Enter for all).

If everything went without errors, we will see a message about the successful issuance of certificates and server setup:

Congratulations! You have successfully enabled ...

After that, connections on port 80 will be redirected to 443 (https).

Add to /etc/crontab to automatically renew certificates:

# Cert Renewal
30 2 * * * root /usr/local/bin/certbot-auto renew --post-hook "nginx -s reload"

Setting up Trusted Host Security (recommended)

This setting is intended as a solution to the problem of dynamic base_url detection, and is intended to prevent HTTP HOST Header attacks (when your site thinks it is someone else).

To do this, you need to specify the trusted domain names of the site in the settings file.

In file

/usr/share/nginx/html/sites/default/settings.php uncomment or add a setting with patterns of actual site names, for example:

$settings['trusted_host_patterns'] = [
  '^www.mydomain.ru$',
];

Installing PHP APCu (RECOMMENDED)

Drupal supports APCu - Alternative PHP User Cache, versions 8 and 9 use APCu more intensively as a short-term local cache than previous versions. The default cache size (32 MB) will suit most sites, and cannot exceed 512 MB.

To activate, install the PHP APCu module:

dnf -y install php-pecl-apcu

Restart nginx and php-fpm:

service nginx restart
service php-fpm restart

In the case of using the Russian language and APCu with the recommended cache memory size, you can see a warning in the control panel that the size of the allocated cache memory differs from the recommended one, but in fact everything works correctly, and the incorrect warning will most likely be fixed in the next updates.

Or if the warning hurts the eye, you can use corresponding patch from offsite.

We want to remind you that you can also make an image for us

There are three options for how to participate.

Prepare the image yourself and get 3000 rubles on your balance

If you are ready to immediately rush into battle and create the image that you lack yourself, we will credit you with 3000 rubles to your internal balance - you can spend it on servers.

How to create your image:

  1. Create an account with us Online
  2. Tell support that you are going to create and test images
  3. We will credit you 3000 rubles and enable the ability to create snapshots
  4. Order a virtual server with a clean operating system
  5. Install the software on this VPS and set it up
  6. Write instructions or script for software deployment
  7. Create a snapshot for the configured server
  8. Order a new virtual server by selecting the previously created snapshot in the "Server template" drop-down list
  9. In case of successful creation of the server, transfer the materials received at step 6 to technical support
  10. In case of an error, you can check with support for the reason and repeat the setup

For business owners: offer your software

If you are a software developer that is deployed and used on a VPS, then we can include you in the marketplace. This is how we can help you bring in new customers, traffic and visibility. Write to us

Let us know in the comments what image do you miss?

And we will prepare it ourselves

Creating a VPS Template with Drupal 9 on Centos 8

Creating a VPS Template with Drupal 9 on Centos 8

Source: habr.com