Drupal 9 with Composer on OpenBSD

nabbisen - May 25 '21 - - Dev Community

Summary

Symfony is one of PHP web frameworks, and my favorite one, because it is clearly classified and functional, is designed with security in mind, is accompanied with useful helpers like MakeBundle, and also provides great official documentation. Drupal, one of content management systems aka CMS, is based on Symfony.
When I try to build some web service, they are usually good candidates. Symfony gives primitive possibility of creating flexible data structure and user interface. Conversely, Drupal offers robust components to build service containing authentication, user roles and functional fields.
This post shows how I built a server of Drupal 9, the latest stable, as a trial.
Besides, Drupal Association wrote about "Why We Use Symfony"

Environment

Tutorial

Database

Connect to a database server.

$ mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Create a database and a user.

CREATE DATABASE <database> \
    CHARACTER SET utf8mb4 \
    COLLATE utf8mb4_unicode_ci;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES \
    ON <database>.* TO \
    <dbuser>@'localhost' \
    IDENTIFIED BY '<dbpassword>';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

App

Environment preparation

Get PHP from Ports collection.

$ doas pkg_add php
$ # choose php-7.4
Enter fullscreen mode Exit fullscreen mode

Also required PHP extensions.

$ doas pkg_add php-curl php-gd php-intl php-zip php-mysqli php-pdo_mysql # + php-[...] if necessary
$ # choose those of php-7.4
Enter fullscreen mode Exit fullscreen mode

Then install other packages if necessary.

$ # install other packages if necessary
$ doas pkg_add unzip composer git
Enter fullscreen mode Exit fullscreen mode

Composer task

First, verify composer is bound to php 7.4, the target version.

$ composer -vvv about
Failed to initialize global composer: Composer could not find the config file: /.../.composer/composer.json
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
Running 2.0.12 (2021-04-01 10:14:59) with PHP 7.4.19 on OpenBSD / 6.9
Failed to initialize global composer: Composer could not find the config file: /.../.composer/composer.json
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
Composer - Dependency Manager for PHP
Composer is a dependency manager tracking local dependencies of your projects and libraries.
See https://getcomposer.org/ for more information.
Enter fullscreen mode Exit fullscreen mode

OK. Output is "Running 2.0.12 (2021-04-01 10:14:59) with PHP 7.4.19 on OpenBSD / 6.9".
Then run composer to create a Drupal project.

$ composer create-project drupal/recommended-project <drupal-dir>
Enter fullscreen mode Exit fullscreen mode

Besides, in case composer is not bound to php-7.4, composer.phar is available:

$ # alternatively
$ #php-7.4 /usr/local/libexec/composer.phar create-project drupal/recommended-project <drupal-dir>
Enter fullscreen mode Exit fullscreen mode

The output was like this (partially omitted):

Creating a "drupal/recommended-project" project at "./drupal"
Installing drupal/recommended-project (9.1.8)
  - Downloading drupal/recommended-project (9.1.8)
  - Installing drupal/recommended-project (9.1.8): Extracting archive
Created project in /var/www/.../<drupal-dir>
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Package operations: 61 installs, 0 updates, 0 removals
  - Downloading composer/installers (v1.11.0)
  - Downloading symfony/...
  ...
  - Downloading pear/archive_tar (1.4.13)
  - Downloading drupal/core (9.1.8)
  - Installing composer/installers (v1.11.0): Extracting archive
  - Installing drupal/core-composer-scaffold (9.1.8): Extracting archive
  - Installing drupal/core-project-message (9.1.8): Extracting archive
  - Installing typo3/phar-stream-wrapper (v3.1.6): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.20.0): Extracting archive
  - Installing symfony/polyfill-ctype (v1.20.0): Extracting archive
  - Installing twig/twig (v2.14.1): Extracting archive
  ...
  - Installing psr/http-message (1.0.1): Extracting archive
  ...
  - Installing doctrine/annotations (1.11.1): Extracting archive
  - Installing doctrine/reflection (1.2.2): Extracting archive
  - Installing composer/semver (3.2.2): Extracting archive
  - Installing asm89/stack-cors (1.3.0): Extracting archive
  - Installing drupal/core (9.1.8): Extracting archive
  - Installing drupal/core-recommended (9.1.8)
Package doctrine/reflection is abandoned, you should avoid using it. Use roave/better-reflection instead.
Generating autoload files
38 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Scaffolding files for drupal/core:
  - Copy [project-root]/.editorconfig from assets/scaffold/files/editorconfig
  - Copy [project-root]/.gitattributes from assets/scaffold/files/gitattributes
  - Copy [web-root]/.csslintrc from assets/scaffold/files/csslintrc
  - Copy [web-root]/.eslintignore from assets/scaffold/files/eslintignore
  - Copy [web-root]/.eslintrc.json from assets/scaffold/files/eslintrc.json
  - Copy [web-root]/.ht.router.php from assets/scaffold/files/ht.router.php
  - Copy [web-root]/.htaccess from assets/scaffold/files/htaccess
  - Copy [web-root]/example.gitignore from assets/scaffold/files/example.gitignore
  - Copy [web-root]/index.php from assets/scaffold/files/index.php
  - Copy [web-root]/INSTALL.txt from assets/scaffold/files/drupal.INSTALL.txt
  - Copy [web-root]/README.txt from assets/scaffold/files/drupal.README.txt
  - Copy [web-root]/robots.txt from assets/scaffold/files/robots.txt
  - Copy [web-root]/update.php from assets/scaffold/files/update.php
  - Copy [web-root]/web.config from assets/scaffold/files/web.config
  - Copy [web-root]/sites/README.txt from assets/scaffold/files/sites.README.txt
  - Copy [web-root]/sites/development.services.yml from assets/scaffold/files/development.services.yml
  - Copy [web-root]/sites/example.settings.local.php from assets/scaffold/files/example.settings.local.php
  - Copy [web-root]/sites/example.sites.php from assets/scaffold/files/example.sites.php
  - Copy [web-root]/sites/default/default.services.yml from assets/scaffold/files/default.services.yml
  - Copy [web-root]/sites/default/default.settings.php from assets/scaffold/files/default.settings.php
  - Copy [web-root]/modules/README.txt from assets/scaffold/files/modules.README.txt
  - Copy [web-root]/profiles/README.txt from assets/scaffold/files/profiles.README.txt
  - Copy [web-root]/themes/README.txt from assets/scaffold/files/themes.README.txt
  * Homepage: https://www.drupal.org/project/drupal
  * Support:
    * docs: https://www.drupal.org/docs/user_guide/en/index.html
    * chat: https://www.drupal.org/node/314178

  Congratulations, you’ve installed the Drupal codebase  
  from the drupal/recommended-project template!          


Next steps:
  * Install the site: https://www.drupal.org/docs/8/install
  * Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html
  * Get support: https://www.drupal.org/support
  * Get involved with the Drupal community:
      https://www.drupal.org/getting-involved
  * Remove the plugin that prints this message:
      composer remove drupal/core-project-message
  * Homepage: https://www.drupal.org/project/drupal
  * Support:
    * docs: https://www.drupal.org/docs/user_guide/en/index.html
    * chat: https://www.drupal.org/node/314178
Enter fullscreen mode Exit fullscreen mode

Additional configuration of directory and permission

A bit more work in command line.

$ cd <drupal-dir>

$ doas chown -R www: ./web/{sites/default, modules, themes}

$ mkdir ./config
$ doas chown -R www: ./config
Enter fullscreen mode Exit fullscreen mode

PHP-FPM

php74_fpm daemon is running? If not, use rcctl enable/start php74_fpm. My past post:

Web

Set up web server to enable the flow:

Client --(request)--> Web (httpd) --> Gateway (PHP-FPM) --> App = Drupal
Enter fullscreen mode Exit fullscreen mode

The configuration file of OpenBSD httpd is /etc/httpd.conf:

$ doas nvim /etc/httpd.conf
Enter fullscreen mode Exit fullscreen mode

Add the definitions below (and more as needed):

server "<fqdn>" {
        listen on $ext_addr tls port 443
        tls {
                certificate     "/etc/ssl/<fqdn>.pem"
                key             "/etc/ssl/private/<fqdn>.key"
        }
        log {
                access  "<fqdn>-access.log"
                error   "<fqdn>-error.log"
        }

        root "/<drupal-dir>/web" # (with chroot)
        directory index index.php

        location "/*.php" { 
                fastcgi socket "/run/php-fpm.sock"
        }
        location "/*.php[/?]*" { 
                fastcgi socket "/run/php-fpm.sock"
        } 
        location match "/[^\.]+/?$" {
                fastcgi socket "/run/php-fpm.sock"
                request rewrite "/index.php/%1"
        }
}
Enter fullscreen mode Exit fullscreen mode

Remember root has to end with /web.
Load the settings.

$ doas rcctl restart httpd
Enter fullscreen mode Exit fullscreen mode

Web installer

With web browser, connect to https://<fqdn>, which will be redirected to https://<fqdn>/core/install.php.
Follow the messages as below.

Language

Language

Installation profile

Installation profile

Database configuration

Database configuration 1
Database configuration 2

Site configuration

Site configuration 1
Site configuration 2
Site configuration 3

Completed

Completed

I have make trials to manage the site since then. I feel I have got along with Drupal 9. Thus, so far, so good🙂

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .