Howto Install and Configure FEMP Stack (FreeBSD 10.2, Nginx, MariaDB, PHP)

September 21, 2015 | By
| Reply More

FreeBSD is free and open source Unix-like operating system from Berkeley Software Distribution (BSD) and it is one of the most popular server platform. Designed with advanced networking, security and server storage. Until now freebsd has been ported to a variety of processor architectures, include PowerPC, Arm etc.

Nginx (pronounced engine-x) is a free and open-source high-performance HTTP server. Nginx focus on high concurrency, performance and low memory usage. And it is make nginx a one of the most popular a web server. You can use nginx also as reverse proxy for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer.

MariaDB is one of the relational database management system (RDBMS) forked from MySQL and it is drop-in replacement for MySQL and developed by some of the original authors of MySQL. MariaDB strives to be logical choice for database professionals looking for a robust, scalable, and reliable SQL server.

FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation. come with some additional features, like a process management, stdout and stderr logging, accelerated upload support, ability to start workers with different uid or gid and listening on different port. It is very useful for heavy-loaded sites.

Basic Software Installation in FreeBSD

FreeBSD bundled with many collection of application and system tools. In addition, you need to 3rd application for your system. FreeBSD provide 2 way to install 3rd party application :

  • Ports collection - For installing packages from source code.
  • Binary packages - For installing pre-built binary packages.

And in this tutorial, I will use "Binary packages" method with pkg command to install FEMP Stack.

Prerequisites

  • FreeBSD 10.2 with IP Address : 192.168.1.110
  • Root privileges

Step 1 - Update system

To get started with installation, log in to your freebsd server with SSH/Console. And make sure your system is up to date, you can use command below to update your system :

freebsd-update fetch
freebsd-update install

And please install nano editor to complete the preparation with pkg command :

pkg install nano

Step 2 - Install Nginx

Before you start nginx installation, you can search the packages and nginx version with "pkg search" command, this is example :

pkg search nginx

nginx-1.8.0_3,2
nginx-devel-1.9.2_2

and you will get all two version of nginx, and in this tutorial we will install stable version 1.8.

Now begin installing nginx with the following command :

pkg install nginx-1.8.0_3,2

Step 3 - Configure Nginx

Now go to the directory "/usr/local/etc/nginx/" to edit nginx file configuration.

cd /usr/local/etc/nginx/

and please rename a file "nginx.conf" to other filename to make it as backup.

mv /usr/local/etc/nginx/nginx.conf /usr/local/etc/nginx/.conf.original

and now create new file for nginx configuration "nginx.conf" with nano :

nano nginx.conf

And paste paste following code for nginx configuration :

# Define user that run nginx
user  www;
worker_processes  2;

# Define error log
error_log /var/log/nginx/error.log info;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;

# Define access log
access_log /var/log/nginx/access.log;

sendfile        on;
keepalive_timeout  65;

server {
listen       80;
server_name  localhost;

# Define web data
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;
}

# Configuration for PHP-FPM
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 exit.

Next, create new directory "nginx" and create two file "error.log" and "access.log" for log configuration in "/var/log/" directory :

mkdir -p /var/log/nginx/
touch /var/log/nginx/{error,access}.log

And now go to the web data directory "/usr/local/www/", and remove "nginx" directory. It is symlink from "nginx-dist" directory, and then copy that directory :

cd /usr/local/www/
rm -rf nginx/
cp -r nginx-dist/ nginx/

Once that is complete, add nginx to the system of startup script "/etc/rc.conf" file. You can add nginx with nano editor or "echo" command as manual method, but here we will use "sysrq" command to do it :

sysrc nginx_enable=yes

Before you start nginx, make sure the configuration is correct with this command :

nginx -t

You can see following results, if there is no mistake in the nginx configuration file.

nginx result

And now you start nginx :

service nginx start

Just open in the browser your server IP : 192.168.1.100

Nginx started

Step 4 - Install and Configure MariaDB

In this step you will be guide to installing mariaDB 10.0, but you can install too for other version, let's search all version mariaDB that available in the repositories :

pkg search mariadb

mariadb100-client-10.0.21
mariadb100-server-10.0.21
mariadb53-client-5.3.12_7
mariadb53-scripts-5.3.12_6
mariadb53-server-5.3.12_6
mariadb55-client-5.5.44
mariadb55-server-5.5.44

There is three version of mariadb, now lets install 10.0 version.

pkg install mariadb100-server-10.0.21 mariadb100-client-10.0.21

And you will get a message about mariadb configuration in freebsd.

MaraiDB Message

Step 5 - Configure MariaDB

Now please change to the directory "/usr/local/share/mysql/". There is three mysql configuration file.

cd /usr/loca/share/mysql/
ls lah my*.cnf

and you need to copy one configuration file "my-medium.cnf" to "/usr/local/etc/" directory.

cp my-medium.cnf /usr/local/etc/my.cnf

Add mariaDB to the startup application with "sysrc" command :

sysrc mysql_enable=yes

So let's start mariaDB :

service mysql-server start

Once mariadb is started, please configure the username and password for mariadb with command below :

mysql_secure_installation

Enter current password for root (enter for none):
#Just press Enter here
Change the root password? [Y/n] Y
#Type your password for mariadb here
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

And if that is complete, now try connect and login to your mariadb shell :

mysql -u root -p
#type your root pasword

MariaDB Login

Step 6 - Install PHP-FPM

In this tutorial we will install php with version php56-5.6.13. If you want to install other version, you can search it with "pkg search" command. And now lets install it :

pkg install php56-5.6.13 php56-mysqli-5.6.13

An then configure it.

Step 7 - Configure PHP-FPM

Next, you must configure php-fpm service, please go to "/usr/local/etc" directory, there php-fpm file configuration stored.

cd /usr/local/etc/

edit "php-fpm.conf" file with nano editor :

nano php-fpm.com

And now please move cursor to the line 164, this is the line to handle a FastCGI request. By default, php-fpm will handle the request under localhost in port 9000 "listen = 127.0.0.1:9000", but we will change this configuration, so php-fpm will handle all request to the "unix socket".

;listen = 127.0.0.1:9000
listen = /var/run/php-fpm.sock

And let's move to the line 175, you need to set permissions for unix socket, php-fpm on freebsd running under user "www", and for security reason you need to make sure that the unix socket permission is same as a user running nginx.

#Just uncomment the line
listen.owner = www
listen.group = www
listen.mode = 0660

And please now configure "php.ini" in the "/usr/local/etc/" director :

cd /usr/local/etc/
nano php.ini

Search for the line "cgi.fix_pathinfo", uncomment and set the value to "0".

cgi.fix_pathinfo=0

Next, add php-fpm to running at the boot time :

sysrc php_fpm_enable=yes

and start php-fpm :

service php-fpm start

Step 8 - Testing Nginx and PHP-FPM Configuration

You need to test that configuration for nginx and php-fpm is correct, so you can use all of it. Just create new file "info.php" in the web data directory "/usr/loca/www/nginx/".

cd /usr/local/www/nginx/
nano info.php

paste following php code :

<?php phpinfo(); ?>

and go to your browser and access the server IP : 192.168.1.100/info.php

Nginx and PHP-FPM

Now you can see "Nginx and PHP-FPM" is running in the freebsd.

Conclusion

Freebsd is an operating system that is reliable. FreeBSD provides two ways to facilitate the user to install a program, and the easiest is the "Binary Package" with the command pkg. FAMP on freebsd almost the same as LEMP on Linux, making it easier for linux users to configure it, maybe different in its configuration file layout, because Freebsd possessed a different file system structure with linux.

Filed Under : LINUX HOWTO

Tagged With : ,

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.