Creating a VPS template with Drupal 9 on Centos 8

We continue to expand our marketplace. Recently, we talked about how we created an image of Gitlab, and this week Drupal has appeared in our marketplace.

Here's why we chose it and how the image was created.

Creating a VPS template with Drupal 9 on Centos 8

Drupal — a convenient and powerful platform for building all types of websites: from microsites and blogs to large social projects, also used as a foundation for web applications, written in PHP and utilizing relational databases for data storage.

Drupal 9 includes all features implemented in version 8.9. The key difference between version 9 and version 8 is that updates and security patches will be released for the platform after November 2021. Additionally, version 9 simplifies the update process, making the upgrade from version 8 even easier.

Server Requirements

It is recommended to use 2 GB of RAM and 2 CPU cores for Drupal.

The core files of Drupal take up about 100 MB; you will additionally need space to store images, databases, themes, additional modules, and backups, which will depend on the size of your website.

Drupal 9 requires PHP 7.4 or higher with a minimum limit of (memory_limit) set to 64 MB; if using additional modules, it is recommended to set it to 128 MB.

For the web server, Drupal can use Apache or Nginx, and for the database, MySQL, PostgreSQL, or SQLite.

We will be installing Drupal using Nginx and MySQL.

Installation

Let's update the installed packages to the latest version:

sudo dnf update -y

We'll add a permanent rule for incoming traffic on 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

Start and enable the Nginx server:

sudo systemctl start nginx
sudo systemctl enable nginx

Since the main Centos repository currently uses PHP 7.2, we will add the REMI repository with PHP 7.4 (the minimum version for Drupal 9).
To do this, we'll add the EPEL repository (required by the REMI repository):

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

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

We will install the PHP modules required for Drupal to function:

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 and opcache:

sudo dnf install -y php-mbstring php-opcache

We will install the MySQL server:

sudo dnf install mysql-server -y

We will enable and start the MySQL server:

sudo systemctl start mysqld
sudo systemctl enable mysqld

Since we are creating a template for VDS, which may be slow, we will add a 30-second delay to the mysqld start. Otherwise, there may be server startup issues during the initial system boot:

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

We will change the group and user under which nginx will run by modifying /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

We will also change the owner of the PHP sessions directory to nginx:

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

We will remove commented lines from the configuration file /etc/nginx/nginx.conf (to avoid double triggers for sed):

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

We will 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

We will add the index file settings for index.php 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

We will add settings for the default server to handle PHP through the php-fpm socket, disable logging for static files, increase the expire time, disable access and error logging 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

We will install wget needed for certbot installation:

sudo dnf install wget -y

We will download the certbot executable file from the official website:

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

We will move certbot to /usr/local/bin/:

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

And set ownership and permissions to root:

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

We will install certbot dependencies and at this stage, we will pause its operation (Answers: Y, c):

certbot-auto

We will download the archive with the latest version of Drupal 9 from the official website:

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

We will install tar to unpack the archive:

sudo dnf install tar -y

We will remove the default files in the directory /usr/share/nginx/html/:

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

We will unpack the files into the web server directory:

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

We will 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/

We will remove the subdirectory:

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

We will delete the archive with the installation files:

rm -f ./tar.gz

We will set ownership of the files to nginx:

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

At this stage, we will shut down the server and take a snapshot:

shutdown -h now

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

mysql_secure_installation

We will enable the password validator:

Would you like to setup VALIDATE PASSWORD component? : y

We will set the password for the MySQL user root:

New password:
Re-enter new password:

We will remove anonymous users:

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

We will disallow root login remotely:

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

We will remove the test database:

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

We will reload the privilege tables:

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

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

We will select the language to use. For example: Russian. Click 'Save and continue'

We will choose the installation profile (demo is used solely for familiarizing with the system). In our case, let’s use 'standard'.

On the next page, we will set the name of the database, for example 'drupal'. We will specify the database user root and the password set for it when running mysql_secure_installation. Click 'Save and continue'.

We will wait for the installation and the updates of translations to complete (this process may take a few minutes).

We will specify the website name, set the website's email address (from which website notifications will be sent), login, password, and the email address of the Drupal admin account. We will also set the country and time zone in the regional settings. Finally, we will complete the installation by clicking 'Save and Continue.'

After this, you can go to the control panel with the created admin login and password for Drupal.

HTTPS Setup (optional)

To set up HTTPS, your VDS must have a valid DNS name; specify it in

/etc/nginx/nginx.conf

the server name section (for example):

server_name domainname.ru;

Let's restart nginx:

service nginx restart

Let's run certbot:

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

We will enter our email, agree to the terms of service (A), subscribe to the newsletter (optional) (N), and select the domain names for which we need to issue the certificate (Enter for all).

If everything goes without errors, we will see a message about successful certificate issuance and server configuration:

Congratulations! You have successfully enabled ...

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

We will add to /etc/crontab for automatic certificate updates:

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

Configuring Trusted Host Security (recommended)

This setting is designed to address issues related to the dynamic determination of base_url 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 the file

/usr/share/nginx/html/sites/default/settings.php uncomment or add a setting with patterns for current domain 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 enable it, we will install the PHP APCu module:

dnf -y install php-pecl-apcu

Let's restart nginx and php-fpm:

service nginx restart
service php-fpm restart

In case of using the Russian language and APCu with the recommended memory size for the cache, you may see a warning in the control panel that the allocated cache memory size differs from the recommended value, but everything works correctly, and the incorrect warning is likely to be fixed in future updates.

Or if the warning is too noticeable, you can use the corresponding patch from the offsite.

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

There are three ways to participate.

Prepare the image yourself and receive 3000 rubles to your balance

If you are ready to dive in and create the image you’re missing, we will credit you 3000 rubles to your internal balance — you can spend it on servers.

How to create your image:

  1. Create an account with us at the website
  2. Inform support that you are going to create and test images
  3. We will credit you 3000 rubles and enable snapshot creation
  4. Order a virtual server with a clean operating system
  5. Install the software on this VPS and configure it
  6. Prepare instructions or a script for deploying the software
  7. Create a snapshot for the configured server
  8. Order a new virtual server, selecting the previously created snapshot in the ‘Server Template’ dropdown
  9. In case of successful server creation, pass the materials gathered in step 6 to technical support
  10. In case of an error, you can check with support about the reason and repeat the configuration

For business owners: offer your software

If you are a software developer whose applications are deployed and used on VPS, we can include you in the marketplace. This way, we can help you attract new customers, traffic, and recognition. Contact us

Tell us in the comments what image you are missing?

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

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster