Install Nginx, MariaDB and PHP (FEMP stack) on FreeBSD 12
A FEMP stack (FreeBSD, Nginx, MySQL/MariaDB, PHP) is a group of applications that can host dynamic websites and web applications, especially PHP-based applications. The FEMP Stack is similar to the LEMP Stack in the Linux world, based on the same applications: Nginx as a web server, MySQL/MariaDB as the database, and PHP as a processing language for applications.
This tutorial will show you how to set up the FEMP Stack on FreeBSD 12.0. We will install the Nginx web server, install the MariaDB database, configure its root password, and then install and configure the latest version of PHP-FPM 7.3.
Prerequisite
For this guide, we will be using the latest version of FreeBSD 12.0 with 1GB of RAM and 2 CPUs. You will need the root privileges for installing new packages and editing the system file configurations.
What we will do:
- Update and Upgrade Packages
- Install Nginx Web Server
- Install and Configure the MariaDB Database
- Install and Configure PHP-FPM 7.3
- Configure Nginx Web Server and PHP-FPM 7.3
- Testing
Step 1 - Update and Upgrade Packages
First of all, we will update the FreeBSD packages repository and upgrade all packages installed to the latest version.
Update the FreeBSD repository and upgrade all packages using the pkg command below.
pkg update
pkg upgrade
Once all the installation is complete, go to the next step.
Step 2 - Install Nginx Web Server
In this step, we're going to install the Nginx web server from the official FreeBSD repository. Then add the Nginx service to the startup boot time, and start the service.
Install Nginx using the pkg command below.
pkg install nginx
Once the installation is complete, add the Nginx service to the system boots.
sysrc nginx_enable=yes
Then start the Nginx web server and check the service.
service nginx start
service nginx status
The Nginx web server is up and running on FreeBSD 12.0 system.
Check the list of opened ports on the system and make sure you get the HTTP port 80 on it.
sockstat -4 -l -P tcp
Below is the result.
Step 3 - Install and Configure MariaDB
In this step, we're going to install the MariaDB database from the FreeBSD repository and configure the root password authentication for it.
The FreeBSD repository provides multiple of MariaDB version, you can check using the following command.
pkg search mariadb
Now you will get the multiple version of MariaDB database.
Now install the latest version package 'mariadb103' for both client and server packages using the pkg command below.
pkg install mariadb103-server mariadb103-client
Once the installation is complete, add the MariaDB/MySQL service to the system boots.
sysrc mysql_enable="yes"
Then start the MariaDB/MySQL service and check its status using the command below.
service mysql-server start
service mysql-server status
Now you will get the result as below.
The MariaDB database is up and running on the FreeBSD 12.0 system.
Next, we will configure the root password authentication for the MariaDB database using the bash command-line tool 'mysql_secure_installation'.
Run the 'mysql_secure_installation' command below.
/usr/local/bin/mysql_secure_installation
And you will be asked for some different question, just type 'Y' to yes for all and press the 'Enter' button.
Set root password? [Y/n] Y
TYPE YOUR ROOT PASSWORD
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
As a result, the root password authentication for the MariaDB database has been configured.
Next, we will log in to the MariaDB/MySQL shell using the password and check the database lists and users.
Login to the MySQL shell using the following command.
mysql -u root -p
TYPE PASSWORD:
Once you've logged in to the shell, check the database list and users using the MySQL queries below.
show databases;
select User, Host, Password from mysql.user;
And you will get the result as below.
As a result, you will be able to login to your MariaDB/MySQL server the password that has been configured.
Step 4 - Install and Configure PHP-FPM 7.3
In this step, we're going to install PHP and PHP-FPM 7.3 packages to the FreeBSD 12.0 system. And then configure the global PHP configuration 'php.ini' and the PHP-FPM configuration file 'www.conf'.
Install PHP and PHP-FPM packages using the pkg command below.
pkg install php73 php73-mysqli php73-zlib php73-gd php73-json php73-mbstring php73-curl
Once the installation is complete, go to the '/usr/local/etc' directory and copy the configuration file 'php.ini-production' to 'php.ini'.
cd /usr/local/etc/
cp php.ini-production php.ini
Now edit the 'php.ini' file using vim editor.
vim php.ini
Uncomment the 'cgi.fix_pathinfo' line and chnage the value to '0'.
cgi.fix_pathinfo=0
Save and close.
Next, we will edit the PHP-FPM configuration 'www.conf' file and configure the PHP-FPM for running on the UNIX file sockets.
Go to the 'cd /usr/local/etc/php-fpm.d' directory and edit the configuration file 'www.conf' using vim editor.
vim php-fpm.d/www.conf
Change the 'user' and 'group' lines with 'www'.
user = www
group = www
Change the 'listen' line value to the UNIX socket file '/var/run/php-fpm.sock'.
listen = /var/run/php-fpm.sock
Uncomment the listen line of the owner, group, and mode for the socket file. Leave the value as default.
listen.owner = www
listen.group = www
listen.mode = 0660
Now go to the bottom of the line and uncomment the PHP Environment configuration as below.
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
Save and close.
Next, add the PHP-FPM service to the system boots.
sysrc php_fpm_enable=yes
Then start the PHP-FPM service and check its status.
service php-fpm start
service php-fpm status
Now the PHP-FPM service is up and running the FreeBSD 12.0 system.
Check the PHP-FPM by checking its UNIX socket using the sockstat command below.
sockstat -u | grep php-fpm
And you will be shown the result as below.
As a result, the PHP-FPM Service is running on the UNIX Socket file under the 'www' user.
Step 5 - Configure Nginx and PHP-FPM
Now we will configure the Nginx web server and PHP-FPM.
Goto the '/usr/local/etc/nginx' directory and edit the 'nginx.conf' file using vim editor.
cd /usr/local/etc/nginx
vim nginx.conf
Change nginx 'user' running as a 'www' and the 'worker_processes' with the number of your server processor.
user www;
worker_processes 2;
Inside the 'http { ... }' bracket configuration, define Nginx logs for both access and error logs.
http {
...
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
...
}
Now move to the 'server { ... }' bracket configuration and hange the 'server_name' with your domain or server IP address.
....
server {
server_name SERVER-IP;
Define the web root directory and index file.
root /usr/local/www/nginx;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
Now paste the following configuration to make the PHP-FPM working with the Nginx web server.
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;
}
Save and close.
Below is the complete configuration of 'nginx.conf' file.
user www; worker_processes 2; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root /usr/local/www/nginx; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/www/nginx-dist; } 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; } } }
Next, we will create the Nginx document root directory '/usr/local/www/nginx' and create the index.html file.
Remove the default symlink directory '/usr/local/www/nginx' and create another one directory.
unlink /usr/local/www/nginx
mkdir -p /usr/local/www/nginx
Now go to the '/usr/local/www' directory and copy the default 'index.html' file from the 'nginx-dist' directory.
cd /usr/local/www/
cp nginx-dist/index.html nginx/
Then create a new PHP file 'info.php' on the new web root directory.
vim nginx/info.php
<?php
phpinfo();
?>
Save and close.
Now test the nginx configuration and make sure there is no error, then restart the Nginx service.
nginx -t
service nginx restart
And you will be shown the result as below.
As a result, the nginx configuration has no error and the service has been restarted. The Nginx configuration and PHP-FPM 7.3 has been completed.
Step 6 - Testing
Open your web browser and type the server IP address on the address bar.
http://10.5.5.25/
Now you will get the default Nginx inde.html page as below.
Next, we will test our Nginx and PHP-FPM configuration by accessing the 'info.php' file.
http://10.5.5.25/info.php
Now you will get pieces of information about your PHP installation and configurations.
As a result, the installation and configuration of the FEMP Stack on FreeBSD 12.0 has been completed successfully.