Introduction

In this article, you’ll learn the process of installing and setting up FEMP stack on FreeBSD 11. The acronym FEMP stands for a group of softwares that are mostly used to deploy dynamic web applications and web sites:

FreeBSD as an operational system
Nginx: as web server
MariaDB as database
PHP as dynamic programming language interpreter

Prerequisites

To follow this tutorial you will need:

Step 1 - Install MariaDB Database

MariaDB is the component you’ll use to store and manage all the dynamic data of your website. To summarize, MariaDB is one of the most used open source databases in the world.

If you look for MariaDB on FreeBSD ports, you’ll certainly find many releases of MariaDB. But in this guide we’ll use the latest one.

To search for MariaDB releases, type the following command:

##

ls -al /usr/ports/databases/ | grep mariadb pkg search mariadb

See the output as

         

... mariadb102-client-10.2.25 Multithreaded SQL database (client) mariadb102-server-10.2.25 Multithreaded SQL database (server) mariadb103-client-10.3.16 Multithreaded SQL database (client) mariadb103-server-10.3.16 Multithreaded SQL database (server) mariadb104-client-10.4.6 Multithreaded SQL database (client) mariadb104-server-10.4.6 Multithreaded SQL database (server) mariadb55-client-5.5.64 Multithreaded SQL database (client) mariadb55-server-5.5.64 Multithreaded SQL database (server)

Then, use this command to install the latest version of MariaDB:

#

pkg install mariadb104-server mariadb104-client

After the installation, use the following command to enable the MySQL server system-wide.

##

sysrc mysql_enable="YES" service mysql-server start

The next step is to secure your database by running mysql_secure_installation script. When the script runs, some questions will be asked to provide the security level to the database. Choose a strong password and create a root user, then answer yes to all the questions just as shown below:

#

/usr/local/bin/mysql_secure_installation

      

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

Enter current password for root (enter for none): OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n]y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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? [Y/n]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? [Y/n] y ... Success!

By default, MariaDB 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? [Y/n]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? [Y/n] y ... Success! Cleaning up...

All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!

After this, the installation is completed. Next, try your user and password by issuing the following commands:

#

mysql -u root -p

MariaDB>

quit

Use the sockstat command to reveal that MariaDB is opened to external network connections and can be accessed from any network remotely.

#

sockstat -4 -6

To disable remote network connections, use the following command in order to force MySQL network socket to bind to the loopback interface:

#

sysrc mysql_args="--bind-address=127.0.0.1"

Finally, restart MariaDB to apply the changes:

##

service mysql-server restart sockstat -4 -6|grep mysql

Step 2 - Install Nginx Web Server on FreeBSD 11

Next step is to install the web server. In this tutorial we’re using Nginx, which has a very simple installation process. You can search for it over the FreeBSD ports repositories by issuing the following command:

#

ls /usr/ports/www/ | grep nginx

Then use the package management command:

#

pkg search -o nginx

To install the most used version of Nginx in FreeBSD, use the following command. Again, you’ll be asked some questions for which you should answer yes to proceed with the installation:

#

pkg install nginx

Once Nginx installation is complete, enable and run the service with the commands below:

##

sysrc nginx_enable="yes" service nginx start

Just like you did to MariaDB, run the sockstat command to check if Nginx service is working and on what sockets it binds on:

#

sockstat -4 -6 | grep nginx

Access your IP using a web browser to visit Nginx default page. You should see the page as below:

nginx_default_page

If you want to deploy a website, copy the html or php script into the Nginx directory. This location is /usr/local/www/nginx/. After copying, update root statement line just like the following example:

#

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

This is the new root path for Nginx:

#

root /usr/local/www/new_html_directory;

Step 3 - Install PHP

Naturally, Nginx can’t directly analyse PHP scripts, it has a specific way of processing through the FastCGI gateway to the PHP-FPM daemon, which will interprete and execute the PHP scripts. To install PHP-FPM daemon, search for it by using the following commands:

##

ls /usr/ports/lang/ | grep php pkg search -o php

Choose the latest version of PHP with the following command:

#

pkg install php73

You can install some PHP extensions to enable the deployment of complex applications by using the following command:

#

pkg install mod_php73 php73-mbstring php73-curl php73-zlib php73-gd php73-json

Also, install the PHP database driver extension, once you are running a database server in your setup. Such extension is used by PHP interpreter to connect to MariaDB:

#

pkg install php73-mysqli

Next step is to update PHP-FPM user and group to match the Nginx runtime user. For this, use the following commands. Don’t forget to replace the group lines variables with www as shown in the example:

#

cp /usr/local/etc/php-fpm.d/www.conf{,.backup}

Enable and run the service with the commands below:

##

sysrc php_fpm_enable="yes" service php-fpm start

PHP-FPM daemon will also open a network socket on localhost:9000 TCP port in listening state. To see this socket, use the following command as shown above:

#

sockstat -4 -6| grep php-fpm

Open Nginx setup file and update the PHP-FPM block as the example:

#

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

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

	location ~ .php$ {
	root           	/usr/local/www/nginx;
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param SCRIPT_FILENAME $request_filename;	
	include        fastcgi_params;
    	}

After you’ve made these changes, create a setup file for PHP based on the default production file with the following command:

#

ln -s /usr/local/etc/php.ini-production /usr/local/etc/php.ini

You’ll be able to change the PHP runtime settings by editing the variables in the php.ini file.

Next, restart both Nginx and PHP-FPM services, using the following command:

#

service php-fpm restart

Also, test its settings for any error:

##

nginx -t service nginx restart

To make the current PHP information available for the FEMP stack in FreeBSD, create a phpinfo.php file by using this command:

#

echo "<?php phpinfo(); ?>" | tee /usr/local/www/nginx/phpinfo.php

As a final step, open your web browser and navigate to the phpinfo.php page. You can do that by entering your domain’s name or your IP address followed by /phpinfo.php:

Conclusion

Now you have your FEMP stack installed and set up in FreeBSD 11. You can start working on your projects, but if you have any questions regarding this tutorial, just leave a comment in the session below.

Read more about: Operating SystemFreeBSD