How to Setup FreeBSD 11 Webserver with Nginx, Mysql, PHP 7.0 (PHP FPM)

Posted on : October 20, 2016 , Last Updated on : October 23, 2016 By in WEB SERVERS
| 2 Replies More

We have been running LEMP stack ie Nginx, MySQL and PHP in Linux based operating system but today we'll be installing FEMP stack on FreeBSD 11. FreeBSD is a free and open source Unix-like operating system based on BSD systems. Unlike Linux, FreeBSD is developed and maintained as an entire operating system from kernel, device drivers to the userland utilities whereas linux is a kernel with device drivers. Nginx is a free and open source web server which is known widely for its speed and ability of load balancing and caching. It is one of the most popular web server and proxy server used in large numbers of servers. MySQL is an open source relational database management system which is developed and maintained by Oracle whereas its free and open source version ie MariaDB is also available on the internet. PHP is a server-side scripting language which is written for web development as a backend language which is developed and maintained by Zend Technologies. And the combination of Nginx, MySQL and PHP running in FreeBSD 11 is known as FEMP stack.


First of all, we'll gonna update our FreeBSD 11 operating system to the latest packages available on the repositories. As the default package manager of FreeBSD 11 is pkg, we'll run the following pkg command to update our system using sudo privilege.

% sudo freebsd-update fetch install

Installing Nginx

Once our system is updated, we'll now install Nginx web server on our FreeBSD 11 machine. To do so, we'll need to run the following pkg command under sudo privilege.

% sudo pkg install nginx

Installing MySQL

Then, we'll install MySQL database system where we'll be storing and retrieving data for our web based application that we'll be running on the machine. To install MySQL server package, we'll need to run the following.

% sudo pkg install mysql56-server

Installing PHP 7.0 Modules

We'll now install all the required modules of PHP 7.0 from the FreeBSD official repository so that the PHP based web applications we wanna run can be executed. In order to install the required PHP modules, we'll need to run the following command.

% sudo pkg install php70 php70-mysqli php70-xml  php70-gd php70-curl php70-zlib php70-zip php70-hash php70-tokenizer

Note that the modules installed in this tutorial are the common php modules whereas depending upon the requirements you may require to install additional modules. Then, we'll copy the sample php configuration ie /usr/local/etc/php.ini-production to /usr/local/etc/php.ini by executing the following command.

% sudo cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

We'll open the file  /usr/local/etc/php.ini using a text editor as follows.

% sudo nano /usr/local/etc/php.ini

Then, we'll uncomment the cig.fix_pathinfo line in php.ini and set the value to 0 in order to prevent users to execute arbitrary PHP codes.


Once done editing, we'll save the file and exit the text editor. Next, we'll need to run rehash command in order to regenerate the system's cache information about our installed executable files.

% rehash

Enabling services

In order to enable nginx, php-fpm and mysql services, we'll run the following sysrc command as shown below. Enabling the services will run the daemons automatically on every system boot.

% sudo sysrc nginx_enable=yes mysql_enable=yes php_fpm_enable=yes

Once done, we'll start the daemons by running the following command.

% sudo service nginx start
% sudo service mysql-server start
% sudo service php-fpm start

Configuring PHP-FPM

Now, we'll need to configure PHP-FPM to use a Unix socket instead of a network port for communication as its more secure than network port. To do so, we'll need to make few changes to the php-fpm configuration file /usr/local/etc/php-fpm.d/www.conf using a text editor.

% sudo nano /usr/local/etc/php-fpm.d/www.conf

Then, we'll make comment to the line listen = by adding a semi-colon ie ; before the line. Once done, we'll add the following line just below it.

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

Next, we'll uncomment the following line by removing the semi-colon as.

listen.owner = www = www
listen.mode = 0660

Once done, we'll save the file and exit the text editor then restart the PHP-FPM by running the following command.

% sudo service php-fpm restart

Configuring MySQL

As we haven’t setup any password for the root user of our MySQL server yet, here we’ll gonna setup a root password for it. To do so, we'll need to run the following command.

% sudo mysql_secure_installation

It will ask us to enter the root password for our MySQL server but as we haven’t set any password before, we’ll simply hit enter button from keyboard. Then, we’ll be asked to setup a password for our MySQL root user, here we’ll hit Y and enter it. Then, we’ll simply strike enter button on keyboard to set the default values for the further settings.

Configuring Nginx with PHP-FPM

Once everything is setup, we'll now configure our Nginx web server to run php scripts using php-fpm. To do so, we'll need to configure the Nginx configuration located at /usr/local/etc/nginx/nginx.conf using a text editor.

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

Once the text editor is opened, we'll need to set the user value as www and worker_processes as the number of CPUs or cores that your system has. This can be checked by running sysctl hw.ncpu in the console. Then, we'll need to configure our server{} block as shown in the following nginx configuration.

user  www;
worker_processes  1; #No. of processors
error_log /var/log/nginx/error.log info;

events {
    worker_connections  1024;

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

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        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;

Once the configuration is made, we'll need to save and restart our nginx server. We can restart our nginx server by running the following command.

% sudo service nginx restart


Now, in order to test our FEMP setup in FreeBSD 11, we'll gonna create a test file named info.php under our webroot using a text editor.

% sudo nano /usr/local/www/nginx/info.php

Then, we'll write the following php test code which will print all the PHP information which will prove us that our FEMP stack is successfully setup.

<?php phpinfo(); ?>

Now, we'll open a web browser and try to access the info.php file by pointing our browser to http://ip-address/info.php or depending upon the configuration made. If everything goes as expected, we should be able to see the following screen on the web browser.

FreeBSD 11 php nginx


FEMP stack enables us to run many web applications on our machine running FreeBSD 11 operating system. This tutorial enables everyone to easily install Nginx, MySQL and PHP 7.0 in FreeBSD 11. We can run many applications like WordPress, Joomla, Drupal, PHP and MySQL based custom build sites and many more. This is a common setup in FreeBSD 11 for running websites on it. We can optimize and tweak the configurations of the application servers in order to get best performance according to the needs and requirements. So, if you have any questions, suggestions, feedback please write them in the comment box below. Thank you ! Enjoy :-)


Tagged With : , , ,

Free Linux Ebook to Download

Comments (2)

Trackback URL | Comments RSS Feed

  1. Rolando says:

    Amazing, thanks.

  2. sunoom says:

    when open returned a 404 error,any advice? thanks
    vbox,feebsd11 nginx php56 mysql56

Leave a Reply

All comments are subject to moderation.