This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. It is an open source unix like software which is actually descended from Berkeley Software distribution (BSD). It is used to power modern servers, desktops and embedded platform. It possesses advanced security, networking and storage features.

In order to install FEMP stack on FreeBSD 12 you will have to go through following steps

Update System

Its a good practice to update the system before installing any software so to check the system for updates, you have to run the following commands

freebsd-update fetch
freebsd-update install

Installing Nginx

The Nginx can be installed on FreeBSD 12 using pkg package manager command, as it is already available on the default FreeBSD repositories

pkg install nginx

Start And Enable Nginx

To run Nginx web server at system startup, you have to add the line nginx_enable="yes" at the end of the /etc/rc.conf configuration file. Run the Command below to automatically append the Nginx configuration to the file, then run the service

sysrc nginx_enable=yes
service nginx start

You can check the status of Nginx service by using the following command

service nginx status
nginx is running as pid 2959

Nginx Configuration

Find the configuration file for Nginx located at /usr/local/etc/nginx which is named as nginx.conf and edit it using a text editor such as vim or nano.

nano /usr/local/etc/nginx/nginx.conf

Make the following changes to the file

  • Set the user that Nginx uses for normal operations to www by uncommenting the line #user nobody and making required changes.
  • Find the worker_processes directive and set it to the value of number of system cores of your server, whicb in my case is 4.
  • Uncomment the error log file directive and set the logging level to information.
  • Uncomment the access log file directive under http block and set its value.
  • Set the value of the server_name directive under server block to the hostname or IP address of your server, which in my case is freebsd12.codesposts.com.

After the changes the file should look like:

/usr/local/etc/nginx/nginx.conf
user www;
worker_processes 4;
error_log /var/log/nginx/error.log info;
access_log /var/log/nginx/access.log;
sever_name freebsd12.codesposts.com;

Add index.php as the first value of index directive to make Nginx server cater the PHP content. Delete the document root and index directives under the location block and define them under the server block context.

/usr/local/etc/nginx/nginx.conf
server {
        listen       80;
        server_name  freebsd12.codesposts.com;
        ... 
        root   /usr/local/www/nginx-dist;
        index  index.php index.html index.htm;

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

Alter the Nginx configuration to make it work with PHP by uncommenting the lines under the location block with PHP configurations. Find the TCP socket, 127.0.0.1:9000; and chnage its value with unix socket unix:/var/run/php-fpm.sock;

/usr/local/etc/nginx/nginx.conf
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

The default repository on FreeBSD can be used to install MySQL. To install MySQL run the following command

pkg install mysql80-server

To enable it on system boot enable MySQL as service

sysrc mysql_enable=yes

Start MySQL

service mysql-server start

Run the following script

mysql_secure_installation

The following script will enforce strong password enforcement

...
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

Press Y to remove anonymous users, disallow remote root login , remove test databases and reload privilege tables to make the changes take effect.

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
environment.

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


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
Success.

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
environment.


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

 - Removing privileges on test database...
Success.

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
Success.

All done!

Installing PHP

PHP is a server side language used in conjunction with HTML to create dynamic web content. It also connects to MySQL database to retrieve and upload content.

The following command installs PHP along with its most commonly used modules.

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

PHP-FPM Configuration

Copy the sample configuration file into the appropriate folder using

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

Open the file /usr/local/etc/php.ini using a text editor such as nano or vim and uncomment the line ;cgi.fix_pathinfo=1 and change its value to cgi.fix_pathinfo=0 so that PHP intepreter cannot process those files which are not found

nano /usr/local/etc/php.ini
/usr/local/etc/php.ini
...
cgi.fix_pathinfo=0
...

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

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

Replace the TCP- socket with Unix-socket

/usr/local/etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm.sock;

Add PHP-FHM on startup and run the service.

sysrc php_fpm_enable=YES
service php-fpm start

PHP Test Configuration

Make a PHP test configuration file under default document root directory to check weather PHP is working perfectly.

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

Check for any syntax error in the file and then restart the service

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

Open up your favourite internet browser and navigate to the server IP address

http://IP_address_of_the_server/test.php

If it works, 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

You are done installing Nginx, MySQL and PHP on your FreeBSD System!