PHP-FPM 8.1 on OpenBSD 7.3

Table of contents

Summary Summary

To my belief, OpenBSD (opens in a new tab) 🐡 and their community support PHP (opens in a new tab) Web services well thankfully.

The core package is offered as pre-compiled binary through Ports packages system. In addition, important softwares such as extensions (opens in a new tab), Composer (opens in a new tab) and PECL (opens in a new tab) libraries are available. So are frameworks such as NextCloud (opens in a new tab) and Zabbix (opens in a new tab).

Moreover, when it is integrated with OpenBSD httpd (opens in a new tab), its chroot helps to keep servers secure. Of course, it is also able to additionally integrate them with relayd (opens in a new tab).

Well, PHP-FPM (opens in a new tab), PHP FastCGI Process Manager, is a part of PHP package in OpenBSD packages. Installing PHP (php-?.? due to the version), therefore, comes with php??_fpm automatically 🙌

This post shows how to install it and configure as a server.

Environment Environment

✿ ✿ ✿

Tutorial Tutorial

Install PHP Install PHP

First, install the main package:

console
$ doas pkg_add php

You will be asked:

quirks-6.121 signed on 2023-04-26T08:37:06Z
Ambiguous: choose package for php
a	0: <None>
	1: php-8.0.28p1
	2: php-8.1.18
	3: php-8.2.5
Your choice: 2

I chose PHP 8.1 this moment. Besides, you can check the lifetime of each version here (opens in a new tab).

The result was:

console
php-8.1.18:argon2-20190702: ok
php-8.1.18:libsodium-1.0.18p1: ok
php-8.1.18:oniguruma-6.9.8: ok
php-8.1.18:femail-1.0p1: ok
php-8.1.18:femail-chroot-1.0p3: ok
php-8.1.18:capstone-4.0.2: ok
php-8.1.18: ok
The following new rcscripts were installed: /etc/rc.d/php81_fpm
See rcctl(8) for details.
New and changed readme(s):
	/usr/local/share/doc/pkg-readmes/femail-chroot
	/usr/local/share/doc/pkg-readmes/php-8.1

You should see php81_fpm come along with php-8.1 !!

Configure PHP Configure PHP

These directories/files are generated:

console
$ ls /etc/php*
/etc/php-8.1.ini    /etc/php-fpm.conf

/etc/php-8.1:

/etc/php-8.1.sample:
opcache.ini

/etc/php-fpm.d:

Edit .ini or .conf file(s) as needed.

For examples, edit:

console
$ doas nvim /etc/php-8.1.ini

like:

diff
- post_max_size = 8M
+ post_max_size = 30M
  (...)
- upload_max_filesize = 2M
+ upload_max_filesize = 24M
  (...)
- allow_url_fopen = Off
+ ; for composer, disabled in php-fpm
+ allow_url_fopen = On

Also, edit:

console
$ doas nvim /etc/php-fpm.conf

to append to the bottom:

diff
+ ; set On in php.ini for composer, therefore:
+ php_admin_value[allow_url_fopen] = Off

Activate extensions (Optional) Activate extensions (Optional)

The file(s) in php-8.1.sample are PHP extensions. According to your necessity, create symbolic link to each of them in /etc/php-8.1/, which will activate the extensions:

console
$ doas ln -sf /etc/php-8.1.sample/${ini} /etc/php-8.1/

For small reference, with more files which have to be dealt with, you can use loop-processing with your shell 😉 For examples:

console
$ # case ksh:
$ for x in $(ls /etc/php-8.1.sample/*); do doas ln -sf $x /etc/php-8.1/; done
$ # case fish:
$ for x in /etc/php-8.1.sample/*; doas ln -sf $x /etc/php-8.1/; end

Configure PHP-FPM Configure PHP-FPM

OK. We’re ready.

The pkg-readme of PHP 8.1 was obtanined in installation of PHP as /usr/local/share/doc/pkg-readmes/php-8.1, which says:

The main OpenBSD php packages include php-fpm, FastCGI Process Manager. This manages pools of FastCGI processes: starts/restarts them and maintains a minimum and maximum number of spare processes as configured. You can use rcctl(8) to enable php-fpm at boot, and start it at runtime:

rcctl enable php81_fpm
rcctl start php81_fpm

Let’s activate the daemon:

console
$ doas rcctl enable php81_fpm

For another small reference, it appends or modifies the line in /etc/rc.conf.local:

diff
+ pkg_scripts=php81_fpm

Now it’s time to start the daemon:

console
$ doas rcctl start php81_fpm
console
php81_fpm(ok)

OK 😄

Usage Usage

Next, we have to set up a web server for them.

Only if you haven’t configured httpd, copy the .conf file from the examples OpenBSD offers as below:

console
$ doas cp -p /etc/examples/httpd.conf /etc/

Well, edit /etc/httpd.conf (opens in a new tab) to add fastcgi socket definitions in some SERVERS (opens in a new tab) section like this:

apache
server "default" {
    listen on * port 80
    #listen on * port 443

    root "/htdocs"
    directory index index.php

    location "/*.php" { 
        fastcgi socket "/run/php-fpm.sock"
    }
    location "/*.php[/?]*" { 
        fastcgi socket "/run/php-fpm.sock"
    } 
}

Note that chroot (opens in a new tab) works in this context 💡
Therefore, fastcgi socket "/run/php-fpm.sock" in /etc/httpd.conf actually means fastcgi socket "/var/www/run/php-fpm.sock". This is the same to that root "/htdocs" means "/var/www/htdocs".

Concolusion Concolusion

Let’s make /var/www/htdocs/index.php for testing:

console
$ echo "<?php phpinfo(1); ?>" | \
      doas tee "/var/www/htdocs/index.php"
$ # delete it afterwards:
$ #doas rm /var/www/htdocs/index.php

Connecting to your host with browser will show the general information !!

php-fpm-works

PHP 8.1 on OpenBSD 7.3 🌻

✿ ✿ ✿

Happy serving ✨☕✨

More on: Run PHP applications

OBSD Solutions

OBSD Solutions: OpenBSD consulting and support by Scqr, Reflerd Inc. (opens in a new tab)

Comments and feedback are welcome.