How to Install Nginx, MySQL and PHP (FEMP Stack) on FreeBSD 15
Build a FEMP stack on FreeBSD 15 from binary packages: Nginx, MySQL 8.4 LTS and PHP 8.5 running through PHP-FPM, with a working server block.
A FEMP stack is the FreeBSD counterpart of LEMP on Linux: FreeBSD, Nginx (the "E" is for "engine-x"), MySQL and PHP. This guide builds one on FreeBSD 15.1-RELEASE from binary packages: Nginx, MySQL 8.4 LTS and PHP 8.5 running through PHP-FPM. At the end, Nginx serves a site from its own directory, passes .php requests to PHP-FPM, and PHP can query a MySQL database. The same commands work on FreeBSD 14.5, the other supported release line. FreeBSD 12 has been end-of-life since the end of 2023, and the PHP 7 and MySQL 8.0 packages of that time are no longer maintained.
Requirements
- A server running FreeBSD 15.1-RELEASE or 14.5-RELEASE.
- Root access. All commands here are run as
root; from a regular account such asdeploy, become root withsu -, or prefix the commands withsudoordoasif you have installed one of them. - Ports 80 and 443 allowed in your provider's firewall, if it has one.
Update the system
freebsd-version
freebsd-update fetch install
pkg update
pkg upgrade -y
freebsd-update applies to systems installed from the traditional distribution sets. On a FreeBSD 15 system installed with base packages (pkgbase), pkg upgrade updates the base system as well. Reboot if a new kernel was installed.
Step 1: Install Nginx
pkg install -y nginx
Enable the service at boot and start it:
sysrc nginx_enable=YES
service nginx start
service nginx status
Open http://203.0.113.10/ (your server's address) in a browser; you should see the "Welcome to nginx!" page. It is served from /usr/local/www/nginx, a symlink to /usr/local/www/nginx-dist. That directory belongs to the package and is overwritten on upgrades, so your own site gets a separate directory in step 4.
Step 2: Install MySQL 8.4
MySQL 8.4 is the long-term support series and the default MySQL version in the FreeBSD ports tree. The server package pulls in the matching client.
pkg install -y mysql84-server
sysrc mysql_enable=YES
service mysql-server start
service mysql-server status
The first start initializes the data directory in /var/db/mysql with a root account that has no password. Run the hardening script straight away; it sets the root password, removes anonymous accounts and the test database, and disables remote root logins:
mysql_secure_installation
Then create a database and a dedicated account for your application rather than letting it connect as root:
mysql -u root -p
CREATE DATABASE appdb;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'ChangeThisPassword';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
EXIT;
Step 3: Install PHP 8.5 and PHP-FPM
Nginx cannot run PHP itself. It hands PHP requests to PHP-FPM, a separate daemon that is part of the main php85 package. Install PHP with the MySQL extension:
pkg install -y php85 php85-mysqli
PHP runs without a configuration file until you provide one. Copy the production template into place:
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
Most applications need more extensions. Each is a separate package named php85-<extension>; search for them and install what your application's documentation lists, for example:
pkg search '^php85-'
pkg install -y php85-pdo_mysql php85-mbstring php85-curl php85-gd php85-zip php85-xml php85-intl php85-session php85-ctype
Enable and start PHP-FPM. Note the underscore in the service name:
sysrc php_fpm_enable=YES
service php_fpm start
service php_fpm status
The pool is defined in /usr/local/etc/php-fpm.d/www.conf. By default it runs its workers as the user www and listens on 127.0.0.1:9000, which is what the Nginx configuration below connects to. Restart php_fpm whenever you install extensions or change php.ini.
Step 4: Configure a site in Nginx
Create a document root for the site and give it to the account that maintains the files:
mkdir -p /usr/local/www/example.com
chown deploy:deploy /usr/local/www/example.com
If you have no regular user yet, skip the chown; root-owned files are fine as long as they are world-readable. Only directories that PHP must write to, such as an upload folder, need to belong to www.
Open /usr/local/etc/nginx/nginx.conf and replace the existing server { ... } block inside http { ... } with this one:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /usr/local/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
What matters in this block:
location ~ \.php$matches request paths ending in.php. The backslash before the dot is required; a pattern written as/.php$, as some older tutorials show, does not do the same thing.try_files $uri =404inside it makes Nginx answer 404 for PHP files that do not exist, instead of passing arbitrary paths to PHP-FPM.SCRIPT_FILENAMEtells PHP-FPM which file to execute; the stockfastcgi_paramsfile does not set it.
Test the syntax and reload:
nginx -t
service nginx reload
To host several sites, add one server block per site with its own server_name and root. Many administrators keep each block in its own file and add include /usr/local/etc/nginx/conf.d/*.conf; inside http; the FreeBSD package does not set that up for you.
Step 5: Test PHP and the database connection
echo '<?php phpinfo();' > /usr/local/www/example.com/info.php
Browse to http://203.0.113.10/info.php. The PHP information page should appear with "FPM/FastCGI" as the Server API and a mysqli section further down. Test the database account from PHP on the command line:
php -r '$db = new mysqli("localhost", "appuser", "ChangeThisPassword", "appdb"); echo $db->server_info, PHP_EOL;'
It prints the MySQL server version when the connection succeeds. Remove the information page afterwards, because it exposes details of your configuration:
rm /usr/local/www/example.com/info.php
Where things are
| Item | Location |
|---|---|
| Nginx configuration | /usr/local/etc/nginx/nginx.conf |
| Nginx logs | /var/log/nginx/access.log, /var/log/nginx/error.log |
| Nginx dynamic modules | /usr/local/libexec/nginx |
| PHP configuration | /usr/local/etc/php.ini, /usr/local/etc/php/*.ini |
| PHP-FPM configuration | /usr/local/etc/php-fpm.conf, /usr/local/etc/php-fpm.d/www.conf |
| MySQL configuration | /usr/local/etc/mysql/my.cnf |
| MySQL data | /var/db/mysql |
Keep the stack updated
pkg update
pkg upgrade
pkg audit -F
pkg audit -F lists installed packages with known vulnerabilities. After upgrades, restart php_fpm and reload nginx so the new binaries are in use. Add HTTPS before putting a site into production; the site has separate guides for TLS certificates and for TLS settings in Nginx on FreeBSD.
Troubleshooting
- 502 Bad Gateway: PHP-FPM is not running or not listening where
fastcgi_passpoints. Checkservice php_fpm statusand thelistenline inwww.conf. - "File not found." or a blank page:
SCRIPT_FILENAMEis missing or therootpath is wrong, so PHP-FPM cannot find the script. Look at/var/log/nginx/error.log. - The browser downloads the .php file: the request did not match the PHP location. Check the regular expression and that the block is in the
serverthat answers for this host name.
Frequently asked questions
Should PHP-FPM listen on a TCP port or a Unix socket?
Both work. The default TCP listener on 127.0.0.1:9000 needs no changes. For a Unix socket, set listen = /var/run/php-fpm.sock in www.conf together with listen.owner = www, listen.group = www and listen.mode = 0660, and change the Nginx line to fastcgi_pass unix:/var/run/php-fpm.sock;. Never bind the TCP listener to a public address.
Can I use MariaDB instead of MySQL?
Yes. MariaDB is packaged for FreeBSD as well; run pkg search mariadb to see the versions on offer and install one -server package. Install either MySQL or MariaDB, not both, because their files conflict. The PHP side (mysqli, pdo_mysql) is the same for both.
Is there a mainline Nginx package?
The nginx package follows the stable branch, which receives the security and important bug fixes and is the right choice for most servers. Run pkg search '^nginx' to see which other builds, such as a mainline or a reduced "lite" variant, your package repository currently offers.
How is this different from the Apache (FAMP) setup?
With Apache, PHP can run inside the web server as mod_php. Nginx has no such module, so PHP always runs in the separate PHP-FPM service, and there are no .htaccess files: rewrite rules and access restrictions go into the server block.