How to Install Nginx, PHP-FPM, and MySQL on OpenBSD 7

OpenBSD 7, Nginx, PHP-FPM (FastCGI Process Manager), and MySQL (OEMP) work as a collection to form a web stack that makes it possible to run dynamic applications on a server. Nginx runs as the web server, FastCGI process manager (PHP-FPM) adds dynamic processing for PHP scripts, and MySQL works as the database server.

In this article, you will install Nginx, PHP with PHP-FPM, and MariaDB as the drop-in replacement MySQL on OpenBSD 7.

Prerequisites

Deploy a Vultr OpenBSD 7 server and SSH to the server as root. Then, update the server.

# pkg_add -u

1. Install Nginx

By default, Nginx is available in the openBSD7 repository packages. Install it with the following command:

# pkg_add nginx

Enable Nginx to start at boot time.

# rcctl enable nginx

Start the Nginx web server.

# rcctl start nginx

2. Install PHP

Install PHP.

# pkg_add php

Select your preferred version based on applications you intend to run. For system-wide compatibility, install PHP 7.4 (option 2).

Ambiguous: choose package for php
    0: <None>
    1: php-7.3.33
    2: php-7.4.26
    3: php-8.0.13

PHP-FPM is automatically installed with the PHP package. Next, you have to start it up and allow it to run when the server starts.

Enable PHP-FPM to run at boot time.

# rcctl enable php74_fpm

Start PHP-FPM.

# rcctl start php74_fpm 

Install the necessary modules for PHP to connect to the MySQL server.

# pkg_add php-mysqli php-pdo_mysql

Output:

Ambiguous: choose package for php-mysqli
    0: <None>
    1: php-mysqli-7.3.33
    2: php-mysqli-7.4.26
    3: php-mysqli-8.0.13
Your choice: 2

You can also install other PHP modules commonly required by most web applications.

# pkg_add php-gd php-intl php-xmlrpc

Enable PHP modules.

# cp /etc/php-7.4.sample/* /etc/php-7.4

3. Configure Nginx for PHP-FPM

Install Nano or your favorite text editor.

# rcctl pkg_add nano

Edit the main Nginx configuration file.

# nano /etc/nginx/nginx.conf

Within the server { block, add the following lines of code for Nginx to pass all PHP processing to the PHP-FPM socket.

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

Save and close the file.

Test the Nginx configuration.

# nginx -t

Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Edit the main PHP-FPM configuration file.

# nano /etc/php74-fpm

Confirm that PHP-FPM connects through the socket /var/www/run/php-fpm.sock, and also runs with the user, group www.

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www
group = www

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
; If using a TCP port, never expose this to a public network.
listen = /var/www/run/php-fpm.sock

4. Install MariaDB

Install the MariaDB service.

# pkg_add mariadb_server

Initialize the database server to create necessary binaries and system tables.

 # mysql_install_db

Start the MySQL daemon.

# rcctl start mysqld

Secure MySQL by setting a new root password and removing insecure defaults.

# mysql_secure_installation 

You will receive multiple prompts, accept decisively to tighten your database server security.

Switch to unix_socket authentication [Y/n] 

Change the root password? [Y/n] 

Remove anonymous users? [Y/n] 

Disallow root login remotely? [Y/n] 

Remove test database and access to it? [Y/n] 

Reload privilege tables now? [Y/n] 

Enable MySQL to start at boot time.

# rcctl enable mysqld

5. Test the Installation

By default, Nginx uses the /var/www/htdocs directory as webroot. Create a new PHP sample file in that directory to test the web server.

# nano /var/www/htdocs/test.php

Paste the following PHP code:

<?php

phpinfo();

?>

Enter your server’s public IP address in a web browser and load the test.php file. Your output should be similar to:

PHP Output

Congratulations, you have successfully installed Nginx, MySQL, and PHP-FPM on your OpenBSD 7 server.

Leave a Comment