Drupal 8 on OpenBSD
Tags: drupal, cms, installation, openbsd
Published: 2019-06-01 10:26:06

---
title: Drupal 8 on OpenBSD
published: true
description: 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. This post shows how to build a Drupal server on OpenBSD, my favorite OS, as well as EC-CUBE and Kimai 2 I have done.
tags: drupal, cms, installation, openbsd
cover_image: https://thepracticaldev.s3.amazonaws.com/i/cga86suj1mz5dtioohyw.jpg
series: Drupal on OpenBSD
---

*\* The cover image is originally by [jplenio](https://pixabay.com/users/jplenio-7645255/) and edited with great appreciation.*

---

## Introduction

[Drupal](https://www.drupal.org) is a secure and powerful CMS aka [content management system](https://en.wikipedia.org/wiki/Content_management_system).
It's an open source platform, which is based on [Symfony](https://symfony.com/), a fast and robust [PHP](https://www.php.net/) web framework, nowadays.

[EC-CUBE](https://www.ec-cube.net/) and [Kimai 2](https://www.kimai.org/) are also based on the same, Symfony.
I have built their servers on [OpenBSD](https://www.openbsd.org/).
My post about EC-CUBE's installation is [here](https://dev.to/nabbisen/ec-cube-4--openbsd-64--30pd) and Kimai 2's is [here](https://dev.to/nabbisen/time-tracking-kimai2-on-openbsd-2mk9).

Thus, I believed I could build a Drupal server on OpenBSD, my favorite OS, as well as them.
And I did it :)

The official document about Drupal installation is:
https://www.drupal.org/docs/8/install
Also, the system requirements is:
https://www.drupal.org/docs/8/install/before-a-drupal-8-installation

#### Environment

- __OS__: OpenBSD 6.5
- __Database__: [MariaDB](https://mariadb.org/) 10.0
- __Web Server__: [httpd](https://man.openbsd.org/httpd.8)
- __App Server__: PHP 7.2
    - __CGI__: [PHP-FPM](https://php-fpm.org/)
    - __Package Manager__: [Composer](https://getcomposer.org/)
- __CMS__: Drupal 8.7

---

## Tutorial of Installation

#### Create Database

Create database and user:

```sql
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;
```

#### Get The Code

The latest package is available here:
https://www.drupal.org/download

First, download it and set it up:

```console
$ ftp https://www.drupal.org/download-latest/zip
$ unzip zip
$ mv drupal-* drupal
$ # optional:
$ rm ./zip

$ cd drupal
```

---

##### Note: Simpler installation

There is an alternative installation.
"QUICKSTART" is written about in `site/core/INSTALL.txt` in the package.
Following it, you can have much fewer steps with missing the web installation and without the detailed configuration.

---

Next, install the dependencies via PHP Composer.
In OpenBSD, the package manager has it but the `composer` command is linked to the lower version PHP than 7.2 today.
Therefore, [a small trick is required](https://dev.to/nabbisen/almost-php-72-composer-with-openbsd-64-100o).
After the installation of Composer, run this command:

```console
$ /usr/local/bin/php-7.2 /usr/local/libexec/composer.phar install --no-dev
```

The result is:

```
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update
Generating autoload files
> Drupal\Core\Composer\Composer::preAutoloadDump
> Drupal\Core\Composer\Composer::ensureHtaccess
```

#### Build the server

You have to [build a web server](https://dev.to/nabbisen/setting-up-openbsds-httpd-web-server-4p9f) with OpenBSD's httpd and [get a certificate](https://dev.to/nabbisen/lets-encrypt-certbot-for-openbsds-httpd-3ofd) of [Let's Encrypt](https://letsencrypt.org/) beforehand.

Configure [httpd.conf](https://man.openbsd.org/httpd.conf) like this.
`fastcgi socket` brings requests to Drupal via PHP-FPM :)

```apache
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"
        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"
         }
}
```

Restart httpd:

```console
# rcctl restart httpd
```

Change permissions:

```console
# # files
# chown -R :www sites/default
# chmod -R g+w sites/default
# # settings.php
$ cp sites/default/default.settings.php sites/default/settings.php
# chmod g+w sites/default/settings.php
```

Here we're ready.

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

![web installer](https://thepracticaldev.s3.amazonaws.com/i/cm5nkg100z6n6qaugnvj.png)
![web installer](https://thepracticaldev.s3.amazonaws.com/i/0yxt18bnbbmvieyxkwi4.png)

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](https://thepracticaldev.s3.amazonaws.com/i/6usbhq03cgod9h71xavh.png)
![web installer](https://thepracticaldev.s3.amazonaws.com/i/zmgy8ofzc4wexel23b7m.png)

![web installer](https://thepracticaldev.s3.amazonaws.com/i/sb54vei14uevo3740ue3.png)

Wait seconds.

![web installer](https://thepracticaldev.s3.amazonaws.com/i/ecg1tclkpdhg2vx65ljh.png)

Site configuration will start.

![web installer](https://thepracticaldev.s3.amazonaws.com/i/q9rvrncettv6ooyb7kp8.png)
![web installer](https://thepracticaldev.s3.amazonaws.com/i/72m6qh6lx4q2tvd5x31w.png)
![web installer](https://thepracticaldev.s3.amazonaws.com/i/dylva4d83q2v25z1fj7s.png)

Finished :)

![finished](https://thepracticaldev.s3.amazonaws.com/i/yfw8386m5ndoqv0c58jl.png)

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

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

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

```diff
+ $settings['trusted_host_patterns'] = array(   
+   '^<fqdn>$',   
+ );
```

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

---

## Conclusion

Thank you for your reading :)
Happy computing.