Install Nginx, MySQL, PHP (FEMP) Stack on FreeBSD 12


Install Nginx, MySQL, PHP (FEMP) Stack on FreeBSD 12

In this tutorial, we are going to learn how to install Nginx, MySQL, PHP (FEMP) Stack on FreeBSD 12. Just like FAMP Stack, FEMB Stack is an acronym for FreeBSD, the Operating System, Engine-X (Nginx) the web server, MySQL the database server and PHP the server side scripting language.

You may also want to check our previous article on how to Install Apache, MySQL, PHP (FAMP) Stack on FreeBSD 12.

In our previous tutorials, we have described the installation of LEMP Stack on Linux systems. You can check them on the links below;

Install Nginx, MySQL, PHP (FEMP) Stack on FreeBSD 12

To install FEMP stack on FreeBSD 12, step through the following procedure;

Ensure your system is up-to-date by running the commands below;

freebsd-update fetch
freebsd-update install

Install Nginx on FreeBSD 12

Nginx Web server is available on the default FreeBSD repositories and can be installed using the pkg package manager command.

pkg install nginx

Start and Enable Nginx

To enable Nginx web server to run on system boot, run the commands below;

sysrc nginx_enable=yes

This will add the line nginx_enable="yes" at the end of the /etc/rc.conf configuration file.

Start Nginx

service nginx start

You can check the status of Apache as shown below;

service nginx status
nginx is running as pid 2959.

To verify that you can actually access you web server from the browser, navigate to the browser and type the IP address of your web server. If everything is working fine, you should be able to see the default FreeBSD Nginx welcome page headed, "Welcome to nginx!".

Configure Nginx

The default configuration file for Nginx resides under /usr/local/etc/nginx as nginx.conf.

Open the configuration file for editing and proceed as follows;

vim /usr/local/etc/nginx/nginx.conf

Set the user that Nginx uses for normal operations to www by uncommenting the line #user nobody and making appropriate substitution.

user www;

Set the value of the worker_processes directive to the number same as the number of CPU cores of your server. By default, it is set to 1.

worker_processes 2;

Define the error as well as access log files for Nginx.

  • Uncomment the error log file directive and set the logging level to information.
    error_log /var/log/nginx/error.log info;
  • Uncomment the access log file directive undet http block and set the value as follows;
    access_log /var/log/nginx/access.log;

Set the value of the server_name directive under server block to the hostname or IP address of your server.


Configure Nginx to server PHP content by adding index.php as the first value of index directive. Remove the document root and index directives under the location block and define them under the server block context.

    server {
        listen       80;
        root   /usr/local/www/nginx-dist;
        index  index.php index.html index.htm;

        location / {
		try_files $uri $uri/ =404;

Configure Nginx to work with PHP processor by uncommenting the lines under the location block with PHP configurations as shown below. Replace the TCP socket,; with unix socket unix:/var/run/php-fpm.sock;

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

Install MySQL on FreeBSD 12

Just like Nginx, MySQL can be installed directly from default FreeBSD 12 default repositories using the package manager. To install MySQL 8.0, run the command below;

pkg install mysql80-server

Enable MySQL as a service so it can start on system boot.

sysrc mysql_enable=yes

Start MySQL

service mysql-server start

Run the normal MySQL security script to remove some default configurations.


The script may prompt you whether to enforce strong password creation. If you need to enforce secure password creation, then press y to accept and choose the level of password validation policy.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

Next, remove anonymous users, disallow remote root login , remove test databases and reload privilege tables to effect the changes.

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...

 - Removing privileges on test database...

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

All done!

Install PHP on FreeBSD 12

PHP works with HTML to generate dynamic web content. In order for PHP to connect to MySQL database to retrieve information for serving to the web server, you need to install PHP Apache and MySQL extensions. The following command installs the most common PHP modules.

pkg install php72 php72-mysqli php72-mbstring php72-zlib php72-curl php72-gd php72-json

If you require other PHP extensions, you can simply search and install them as shown above.

Configure PHP-FPM

Copy the sample PHP configuration file into place.

cp /usr/local/etc/php.ini{-production,}

Open the file /usr/local/etc/php.ini for editing the uncomment the line ;cgi.fix_pathinfo=1 and set its value to 0 so that PHP intepreter cannot processing files not found.


Open the file /usr/local/etc/php-fpm.d/www.conf and uncomment the following lines;

vim /usr/local/etc/php-fpm.d/www.conf
listen.owner = www = www
listen.mode = 0660

Replace the TCP socket with unix socket.

;listen =
listen = /var/run/php-fpm.sock;

Set php-fpm to start on boot and start it.

sysrc php_fpm_enable=YES
service php-fpm start

Create PHP test configuration file under the Nginx default document root directory to verify whether PHP is working well with Nginx web server. The default document root directory is /usr/local/www/nginx-dist.

vim /usr/local/www/nginx-dist/test.php
<?php phpinfo(); ?>

Verify that Nginx has not syntactical error and restart it.

nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
service nginx restart

Navigate to the browser and the address in the format, http://server_IP_address/test.php.

Install Nginx, MySQL, PHP (FEMP) Stack on FreeBSD 12

Beautiful. Now remove the test file from your server to avoid exposing the information about server to the public.

rm -rf /usr/local/www/nginx-dist/test.php

That is all it takes to Install Nginx, MySQL, PHP (FEMP) Stack on FreeBSD 12. Thank you for reading. We hope this was informative.



Please enter your comment!
Please enter your name here