Operating System BSD

Nginx, PHP-FPM and MariaDB (MySQL) Setup on OpenBSD 8.0

Set up Nginx, PHP-FPM and MariaDB from packages on OpenBSD 8.0, including the chroot details that make PHP and the database socket work together.

This guide sets up Nginx, PHP-FPM and a MySQL-compatible database on OpenBSD 8.0, the current release (October 2026), using packages only. At the end, Nginx serves a site from /var/www/htdocs, passes .php requests to PHP 8.5 over a Unix socket, and PHP can query a MariaDB database. It assumes you are comfortable with a Unix shell and an editor such as vi.

Two things differ from what older tutorials for OpenBSD 5.x describe. First, OpenBSD no longer packages MySQL itself; the MySQL-compatible server in packages is MariaDB, which speaks the same protocol and is used by PHP through the same mysqli and pdo_mysql extensions. Second, both Nginx and PHP-FPM run chrooted to /var/www by default, which affects every path and socket you configure. OpenBSD also has its own web server, httpd, in the base system; use this guide when you specifically want Nginx.

Requirements

  • A server running OpenBSD 8.0 with a public IP address.
  • A user that can run commands through doas, or a root shell.
  • A working /etc/installurl, which the installer writes for you. There is no need to set PKG_PATH by hand.

Apply the base system patches before installing anything:

doas syspatch

Step 1: Install the packages

doas pkg_add nginx mariadb-server
doas pkg_add php%8.5 php-mysqli%8.5 php-pdo_mysql%8.5

OpenBSD 8.0 carries three PHP branches (8.3, 8.4 and 8.5). A plain pkg_add php stops and asks which one you want; the %8.5 suffix selects the branch directly. The main php package already contains PHP-FPM. The release ships Nginx 1.30 and MariaDB 12.3.

Each package installs notes for OpenBSD in /usr/local/share/doc/pkg-readmes/. They are short and worth reading.

Step 2: Understand the chroot

The packaged Nginx changes its root directory to /var/www when it starts, and the default PHP-FPM pool has chroot = /var/www set. Once running, neither process can see anything outside that directory. The consequences:

  • Web content, the PHP-FPM socket and anything PHP needs to read must live under /var/www.
  • Inside PHP, paths have no /var/www prefix: the file /var/www/htdocs/index.php is /htdocs/index.php to the script.
  • In nginx.conf you may write full paths such as /var/www/htdocs; the OpenBSD build strips the prefix itself.
  • A database socket in /var/run is invisible to PHP, so it has to be moved into the chroot (step 3).

Step 3: Set up MariaDB

Create the initial database files:

doas mariadb-install-db

Create a directory for the server socket inside the web chroot, as the package notes describe:

doas install -d -m 0711 -o _mysql -g _mysql /var/www/var/run/mysql

Edit /etc/my.cnf and set the socket path in the [client-server] section, which applies to both the server and the command-line clients:

[client-server]
socket = /var/www/var/run/mysql/mysql.sock

The same file already contains bind-address=localhost, so the server is not reachable from the network. Enable and start it; the service is named mysqld:

doas rcctl enable mysqld
doas rcctl start mysqld

Run the hardening script, which removes anonymous accounts and the test database:

doas mariadb-secure-installation

Then create a database and an account for your application:

doas mariadb
CREATE DATABASE appdb;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'ChangeThisPassword';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
EXIT;

Step 4: Configure PHP and PHP-FPM

PHP's configuration is /etc/php-8.5.ini. Extensions installed as separate packages are not active until you link their sample .ini file into /etc/php-8.5:

doas ln -sf ../php-8.5.sample/mysqli.ini /etc/php-8.5/
doas ln -sf ../php-8.5.sample/pdo_mysql.ini /etc/php-8.5/

PHP-FPM reads /etc/php-fpm.conf. The packaged defaults already fit Nginx, so you do not need to edit it:

user = www
group = www
listen = /var/www/run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660
chroot = /var/www

Enable and start the service. Its name contains the PHP branch without the dot:

doas rcctl enable php85_fpm
doas rcctl start php85_fpm

Step 5: Configure Nginx

Create a directory for the site and a test page that also checks the database connection:

doas mkdir -p /var/www/htdocs/example.com
doas vi /var/www/htdocs/example.com/index.php
<?php
$db = new mysqli('localhost', 'appuser', 'ChangeThisPassword', 'appdb');
echo 'Connected to MariaDB ', $db->server_info;

PHP on OpenBSD looks for the database socket at /var/run/mysql/mysql.sock. Seen from inside the chroot, that is exactly the file you configured in step 3, so localhost works without further settings.

Open /etc/nginx/nginx.conf and replace the existing server { ... } block inside http { ... } with:

    server {
        listen       80;
        listen       [::]:80;
        server_name  example.com www.example.com;
        root         /var/www/htdocs/example.com;
        index        index.php index.html;

        location ~ \.php$ {
            try_files      $uri $uri/ =404;
            fastcgi_pass   unix:run/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

The PHP location is the one that the packaged sample configuration provides in commented form. unix:run/php-fpm.sock is relative to Nginx's prefix, /var/www, so it points to the socket PHP-FPM created. The backslash in \.php$ is required; a pattern written as /.php$, as some older tutorials show, does not match the same requests.

Test the configuration, then enable and start Nginx:

doas nginx -t
doas rcctl enable nginx
doas rcctl start nginx

rcctl enable records the service in /etc/rc.conf.local for you, including the pkg_scripts entry, so there is no need to edit /etc/rc.conf or rc.conf.local by hand. If you had enabled the base httpd earlier, stop and disable it first, because both servers want port 80.

Step 6: Verify

doas rcctl check nginx php85_fpm mysqld
ftp -o - http://203.0.113.10/

Replace the address with your server's. The output should be "Connected to MariaDB" followed by the server version. Replace the test page with your application afterwards; it contains a database password.

The default /etc/pf.conf does not block incoming connections. If you run a stricter ruleset, allow web traffic with a rule such as pass in on egress proto tcp to port { 80 443 } and load it with doas pfctl -f /etc/pf.conf.

Logs and maintenance

  • Nginx writes to /var/www/logs/access.log and /var/www/logs/error.log. These are the file names that /etc/newsyslog.conf already rotates for httpd; the Nginx package notes give replacement lines that signal Nginx (/var/run/nginx.pid, SIGUSR1) after rotation.
  • After changing nginx.conf: doas nginx -t && doas rcctl reload nginx. After changing PHP settings or enabling an extension: doas rcctl restart php85_fpm.
  • Package updates for the release are installed with doas pkg_add -u, base system fixes with doas syspatch. After a MariaDB update, run doas mariadb-upgrade, as the package notes advise.
  • For HTTPS, obtain a certificate with acme-client from the base system and add a listen 443 ssl; server with ssl_certificate and ssl_certificate_key. This site has a separate guide for acme-client on OpenBSD.

Frequently asked questions

Why can my PHP script not resolve host names or make HTTPS requests?

Inside the chroot there is no /etc/resolv.conf and no CA bundle. The PHP package notes tell you to copy them in: create /var/www/etc/ssl, then install /etc/resolv.conf into /var/www/etc/ and /etc/ssl/cert.pem and /etc/ssl/openssl.cnf into /var/www/etc/ssl/. Repeat this when the originals change.

Why does PHP's mail() function do nothing?

mail() runs an external sendmail program through a shell, and the chroot contains no /bin/sh unless you copy one in (cp /bin/sh /var/www/bin) and keep it up to date. The same applies to exec() and similar functions. The package notes recommend sending mail over SMTP with a library such as PHPMailer instead.

Can I run Nginx without the chroot?

The OpenBSD build accepts a -u flag that disables it, which you would set with doas rcctl set nginx flags -u. Paths then behave as on other systems, but you give up a protection that costs little once the socket paths are right, so keep the chroot unless an application truly cannot work in it.

How do I use a different PHP version?

Install the other branch with pkg_add php%8.4, link its extensions under /etc/php-8.4, then disable and stop php85_fpm and enable and start php84_fpm. Both read /etc/php-fpm.conf and use the same socket by default, so only run one at a time unless you give the second its own configuration file with rcctl set php84_fpm flags -y /etc/php-fpm-8.4.conf.

Share story:

Continue reading other guides on How VPS.