PHP-FPM 8.0 on OpenBSD 7.0
Tags: php, phpfpm, openbsd, httpd
Published: 2022-02-18 15:30:53

## Summary

[PHP-FPM](https://php-fpm.org/), PHP FastCGI Process Manager, is a part of [PHP](https://www.php.net/) package in OpenBSD packages nowadays.
So installing PHP (`php-?.?` due to the version) comes with `php??_fpm` automatically 💃
This post will show you how to set it up.

### Environment

- OS: OpenBSD 7.0 amd64
- PHP: 8.0

<center>✿ ✿ ✿</center>

## Tutorial

### Installation

The first step is to install the PHP package:

```console
$ doas pkg_add php
Ambiguous: choose package for php
a	0: <None>
	1: php-7.3.33
	2: php-7.4.27
	3: php-8.0.14
Your choice:
```

I chose "3" because it's longer supported term due to [each support term of the PHP versions](https://www.php.net/supported-versions.php).

The output was:

```console
php-8.0.14:femail-1.0p1: ok
php-8.0.14:femail-chroot-1.0p3: ok
php-8.0.14:argon2-20190702: ok
php-8.0.14:libsodium-1.0.18p1: ok
php-8.0.14:libxml-2.9.12: ok
php-8.0.14:oniguruma-6.9.7.1: ok
php-8.0.14: ok
Running tags: ok
The following new rcscripts were installed: /etc/rc.d/php80_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.0
```

Then these directories/files were made:

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

/etc/php-8.0:

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

/etc/php-fpm.d:
```

Well, the `.ini` file(s) in `/etc/php-8.0.sample` are PHP extensions.
According to necessity, copy each of them to `/etc/php-8.0/` to activate the extensions:

```console
$ doas ln -sf /etc/php-8.0.sample/%target-ini-files% /etc/php-8.0/
```

Also edit `/etc/php-8.0.ini` as needed.
For example:

```diff
  ; Maximum allowed size for uploaded files.
  ; http://php.net/upload-max-filesize
- upload_max_filesize = 2M
+ upload_max_filesize = 5M
```

Besides, the manual is also installed as `/usr/local/share/doc/pkg-readmes/php-8.0` which declares:

> 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 php80_fpm
>   rcctl start php80_fpm

OK. We're ready.
Let's start daemon:

```console
$ doas rcctl enable php80_fpm
```

It appended a line to /etc/rc.conf.local:

```diff
+ pkg_scripts=php80_fpm
```

OK. It's time to start the daemon:

```console
$ doas rcctl start php80_fpm
php80_fpm(ok)
```

### Usage

Next, we need to prepare a web server.
Let's edit [`/etc/httpd.conf`](https://man.openbsd.org/httpd.conf.5) to add `fastcgi socket` in [SERVERS](https://man.openbsd.org/httpd.conf.5#SERVERS) sections like this:

```apache
types {
    include "/usr/share/misc/mime.types"
}

ext_addr="egress"

server "default" {
    listen on $ext_addr port 80

    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`](https://man.openbsd.org/httpd.conf.5#chroot) 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"`.

### Testing

Let's make `/var/www/htdocs/index.php` for testing like this:

```console
# echo "<?php phpinfo(); ?>" > /var/www/htdocs/index.php
# # delete the file above afterwards as needed
```

Then browsing your host will show you the whole phpinfo!

<center>✿ ✿ ✿</center>

Happy serving 💃