Introduction
FreeBSD, Nginx, Mariadb, and PHP (FEMP stack) is a collection of open-source applications that enable the development, hosting, and delivery of dynamic web applications on a server. FreeBSD works as the deployment operating system, Nginx as the web server, MySQL or MariaDB as the database backend, and PHP as the server-side scripting processor.
Install Nginx
Nginx is available in the default package repositories on FreeBSD 14.0 with the latest version information. Follow the steps below to install Nginx and manage the application service to run on your server.
1. Update the pkg repository catalog.
$ sudo pkg update
2. Install Nginx.
$ sudo pkg install nginx
3. Enable Nginx to automatically start at boot time.
$ sudo sysrc nginx_enable=yes
4. Start the Nginx service.
$ sudo service nginx start
5. View the Nginx service status and verify that it’s running.
$ sudo service nginx status
nginx is running as pid 1711.
Install Mariadb
MySQL is available in the default package repositories on FreeBSD 14.0 with multiple package versions. Follow the steps below to install the latest MySQL database server package on your server using the default pkg package manager.
- Search all Mariadb packages available in the default pkg repositories.
$ pkg search ^mariadb
mariadb-connector-c-3.4.3 MariaDB database connector for C
mariadb-connector-odbc-3.1.20 MariaDB database connector for odbc
mariadb1011-client-10.11.11 Multithreaded SQL database (client)
mariadb1011-server-10.11.11 Multithreaded SQL database (server)
mariadb105-client-10.5.28 Multithreaded SQL database (client)
mariadb105-server-10.5.28 Multithreaded SQL database (server)
mariadb106-client-10.6.21 Multithreaded SQL database (client)
mariadb106-server-10.6.21 Multithreaded SQL database (server)
mariadb114-client-11.4.5_1 Multithreaded SQL database (client)
mariadb114-server-11.4.5_1 Multithreaded SQL database (server)
2. Install the latest Mariadb database server package.
$ sudo pkg install mariadb114-server
3. Enable the Mariadb service to automatically start at boot.
$ sudo sysrc mysql_enable=yes
4. Start the Mariadb service.
$ sudo service mysql-server start
5. View the Mariadb service status and verify that it’s running.
$ sudo service mysql-server status
mysql is running as pid 2426.
6. Start the Mariadb secure installation script. Select all the default values and set a password when prompted for one the second time around.
$ sudo mysql_secure_installation
7. Restart the MySQL database server to apply your configuration changes.
$ sudo service mysql-server restart
Install PHP and Configure PHP-FPM
PHP is available in the default repositories on your FreeBSD server and includes the PHP-FPM package that manages connections to the PHP service. Follow the steps below to install the latest PHP version and configure PHP-FPM on your server.
1. Search all available PHP packages in the FreeBSD pkg catalog.
pkg search ^php[0-9]
2. Install the latest PHP version and PHP-FPM. For example, PHP version 8.4.
$ sudo pkg install -y php84
3. View the installed PHP version on your server.
$ php -v
PHP 8.4.5 (cli) (built: Mar 16 2025 11:53:39) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.5, Copyright (c) Zend Technologies
4. Enable PHP-FPM to automatically start at boot time.
$ sudo sysrc php_fpm_enable=yes
5. Start the PHP-FPM service.
$ sudo service php_fpm start
6. View the PHP-FPM and verify that it’s running.
$ sudo service php_fpm status
php_fpm is running as pid 1716.
7. Install common PHP modules required by most web applications.
$ sudo pkg install php84-mysqli php84-curl php84-gd php84-intl php84-mbstring php84-xml php84-zip
8. Open the default PHP-FPM pool configuration www.conf using a text editor such as vi.
$ sudo vi /usr/local/etc/php-fpm.d
9. Ensure the following variables are set and save/close the file.
[www]
user = www
group = www
listen = /var/run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
10. Restart the PHP-FPM service to apply your configuration changes.
$ sudo service php_fpm restart
Performing sanity check on php-fpm configuration:
[26-Mar-2025 22:46:50] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful
Stopping php_fpm.
Waiting for PIDS: 1716.
Starting php_fpm.
Don’t forget finally added a symlink from /var/run/mysql/mysql.sock to /tmp/mysql.sock
$ sudo ln -sf /var/run/mysql/mysql.sock /tmp/mysql.sock
References
https://docs.vultr.com/how-to-install-nginx-mysql-php-femp-stack-on-freebsd-14-0