Drupal 8 with Composer on OpenBSD

nabbisen - Oct 8 '19 - - Dev Community

* The cover image is originally by Little_Foxy and edited with great appreciation.


Introduction

Drupal is a secure and powerful CMS aka content management system.
It's an open source platform, which is based on Symfony, a fast and robust PHP web framework, nowadays.

I wrote about installing it by building source code:

However, using composer is recommended officially.
Additionally, I found building the environment under composer would be helpful at updating the system later.

Therefore, I tried to install it via composer.

* Note: There is another way to "Add Composer to an existing site".

Environment

  • OS: OpenBSD 6.5
  • Database: Database: MariaDB 10.0
  • Web Server: httpd
  • App Server: PHP 7.3
  • CMS: Drupal 8.7

The system requirements of Drupal is:
https://www.drupal.org/docs/8/install/before-a-drupal-8-installation


Tutorial of Installation

Prepare database

Create database and 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

Install App via Composer

<drupal-dir> means your Drupal's root directory.

$ php-7.3 /usr/local/libexec/composer.phar create-project drupal-composer/drupal-project:8.x-dev <drupal-dir> --no-interaction
Enter fullscreen mode Exit fullscreen mode

* Caution: It is NOT necessary to modify the part "drupal-composer/drupal-project:8.x".

It takes minutes.
The result is:

Installing drupal-composer/drupal-project (8.x-dev bdaa8fd53b120fa556e44cbfaa42f4c56831b9bd)
  - Installing drupal-composer/drupal-project (8.x-dev bdaa8fd): Cloning bdaa8fd53b from cache
Created project in drupal
> DrupalProject\composer\ScriptHandler::checkComposerVersion
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 140 installs, 0 updates, 0 removals
  - Installing cweagans/composer-patches (1.6.7): Loading from cache
...
  - Installing drupal/core (8.7.7): Loading from cache
...
  - Installing drush/drush (9.7.1): Loading from cache
  - Installing vlucas/phpdotenv (v2.6.1): Loading from cache
paragonie/random_compat suggests installing ext-libsodium (Provides a modern crypto API that can be used to generate random bytes.)
...
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating autoload files
  - .csslintrc (https://git.drupalcode.org/project/drupal/raw/8.7.7/.csslintrc): Downloading (connecting.Downloading (100%)         
...
  - ../.gitattributes (https://git.drupalcode.org/project/drupal/raw/8.7.7/.gitattributes): Downloading (Downloading (100%)         
> DrupalProject\composer\ScriptHandler::createRequiredFiles
Created a sites/default/settings.php file with chmod 0666
Created a sites/default/files directory with chmod 0777
Enter fullscreen mode Exit fullscreen mode

Next, change the permissions:

$ cd <drupal-dir>
# # files
# chown -R :www ./web/sites/default
# chmod -R g+w ./web/sites/default
Enter fullscreen mode Exit fullscreen mode

* Caution: There is no need to configure "setting.php" by doing such as cp ./web/sites/default/default.settings.php sites/default/settings.php.

Well, Drupal wants to create config dir out of the public directory called "web".
So let's prepare it:

$ mkdir ./config
# chown -R :www ./config
# chmod -R g+w ./config
Enter fullscreen mode Exit fullscreen mode

If this preparation is not completed, the error "create the directory ../config/sync failed" will occur in "Verify requirements" page of the web installer.

Build the server

You have to build a web server with OpenBSD's httpd and get a certificate of Let's Encrypt beforehand.

server "<fqdn>" {
        listen on $ext_addr port 80
        block return 301 "https://$SERVER_NAME$REQUEST_URI"
}                                                  
server "<fqdn>" {
        listen on $ext_addr tls port 443
        tls {
                certificate     "/etc/letsencrypt/live/<fqdn>/fullchain.pem"
                key             "/etc/letsencrypt/live/<fqdn>/privkey.pem"
        }
        # optional:
        log {
                access  "<fqdn>-access.log"
                error   "<fqdn>-error.log"
        }

        root "/<drupal-dir>/web"
        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

Configure httpd.conf like this.
fastcgi socket brings requests to Drupal via PHP-FPM :)

Besides, in comparison with my past post, the root directory is different:

- root "/<drupal-dir>"
+ root "/<drupal-dir>/web"
Enter fullscreen mode Exit fullscreen mode

Well, restart the web server:

# rcctl restart httpd
Enter fullscreen mode Exit fullscreen mode

Here we're ready.

Web installer

Let's access to https://<fqdn> .
You will see the web installer running.
Just follow it :)

web installer
web installer

Database Configuration:

web installer

If you use "localhost" as database host, you might have to replace it with "127.0.0.1".
Also, I recommend using "Table name prefix" also from the point of view of security.

web installer
web installer

The installation will start:

web installer

Wait seconds.

Site configuration will start:

web installer
web installer
web installer

Finished :)

finished

Security verification

Reduce write permissions from a file, sites/default/settings.php, and a directory, sites/default:

# chmod g-w ./web/sites/default/settings.php
# # The target is just a directory and therefore the `-R` option is unnecessary:
# chmod g-w ./web/sites/default
Enter fullscreen mode Exit fullscreen mode

"Health check" of the system will perhaps want you to register trusted hosts.
Edit sites/default/settings.php like this:

  /**
   * Trusted host configuration.
  ...
   */
+ $settings['trusted_host_patterns'] = array(   
+   '^<fqdn>$',   
+ );
Enter fullscreen mode Exit fullscreen mode

Here, don't forget escape sequence to use period because it means patterns.
For example, "^your.sub.domain$" should be "^your\.sub\.domain$".


References

The official documents about Drupal installation is:
https://www.drupal.org/docs/8/install
Also, about composer:

The end of support of Drupal 8 is announced as Nov 2021.

Conclusion

Thank you for your reading :)
Happy computing.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player