Home Database Install Nginx, MariaDB, and PHP (FEMP) Stack in FreeBSD 10.2

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

By sk
438 views

In our previous tutorial, we have described how to setup FAMP stack in FreeBSD.

Now, We will show you how to install Nginx, MariaDB, and PHP (FEMP) stack in FreeBSD 10.2 server.

As you probably know, FEMP is the acronym of FreeBSD, Nginx (engine x), MariaDB/MySQL, and PHP.

For the purpose of this tutorial, I will be using the following test machine.

  • Operating system: FreeBSD 10.2 64 bit system
  • Hostname: freebsd.ostechnix.local
  • IP Address: 192.168.1.103/24

Well, now let us start to deploy FAMP stack on FreeBSD 10.2.

1. Update your FreeBSD

Like any other operating system, we must update FreeBSD before installing any software. To do so, switch to root user:

$ su

And run the following commands one by one to update your FreeBSD server:

# freebsd-update fetch install

2. Install Nginx

Install Nginx web server using command:

# pkg install nginx

sk@sk: ~_001

Configure Nginx

Nginx default configuration file is /usr/local/etc/nginx/nginx.conf.

Let us backup the configuration file first, just in case if anything goes wrong, we can easily restore from the backup.

To do so, run:

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

Next, create a new Nginx configuration file:

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

Add the following lines:

user www;
worker_processes  1;
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;
        server_name  ostechnix.local www.ostechnix.local;
        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;
        }
    }
}

sk@sk: ~_004

Replace the server_name directive (ostechnix.local) with your own. Save and close the file.

Next, create necessary log files. These log files will be useful for any troubleshooting and error analysis in future.

# mkdir -p /var/log/nginx
# touch /var/log/nginx/access.log
# touch /var/log/nginx/error.log

Next, we need to configure the correct web root directory. By default, the web root directory is /usr/local/www/nginx, however it is just a symlink to the “nginx-dist” directory.

We should remove that link and point it to the correct location.

So, let us remove the web root directory with command:

# rm -rf /usr/local/www/nginx

Then recreate the web root directory with command:

# mkdir /usr/local/www/nginx

Next, we should copy index.html file to the web root directory, because it was deleted while we removing the web root directory in the earlier step.

# cp /usr/local/www/nginx-dist/index.html /usr/local/www/nginx

Test the nginx configuration file is correct with command:

# nginx -t

Sample output:

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

Finally, we need to enable and start Nginx service. To do so, run:

# sysrc nginx_enable=yes
# service nginx start

sk@sk: ~_002

Test Nginx

Open up your web browser and navigate to: http://IP-address/ or http://localhost. You'll see the following Nginx test page.

Selection_005

Congratulations! Nginx web server installation is successful and it is working well.

Next, we need to install MariaDB.

3. Install MariaDB

MariaDB is drop-in replacement of MySQL. To install MariaDB, run:

# pkg install mariadb100-server

Next, copy MariaDB configuration file from directory ‘/usr/local/share/mysql/’ to ‘/usr/local/etc/’ as shown below.

# cp /usr/local/share/mysql/my-medium.cnf /usr/local/etc/my.cnf

Then, enable and start MariaDB service using commands:

# sysrc mysql_enable=yes
# service mysql-server start
Setup MariaDB root user password

As you probably know, MariaDB root user has empty, which is not recommended, at the time of installation. So to secure MariaDB root user, it is mandatory to setup a strong password for the root user. To do so, run:

# mysql_secure_installation

When prompt “Enter current password for root”, just press ENTER key and set the password twice. Then simply press Y to accept the default values.

Sample output:

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): ## Press Enter
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] ## Press Enter

New password: ## Enter password
Re-enter new password: ## Re-enter 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] ## Press Enter
 ... 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] ## Press Enter
 ... 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] ## Press Enter
 - 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] ## Press Enter
 ... 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!

That’s it. MariaDB is installed and secured now. Let us go ahead and install PHP.

4. Install PHP

To install PHP, run:

# pkg install php56-mysql php56-mysqli

After installing PHP, copy the sample PHP configuration file  /usr/local/etc/php.ini-production  to /usr/local/etc/ directory as shown below.

# cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

If you do any changes, you must update the changes, using command:

# rehash

Now, we need to configure PHP with Nginx web server.

Configure PHP with Nginx web server

Edit /usr/local/etc/php-fpm.conf file:

# nano /usr/local/etc/php-fpm.conf

Find the following line:

listen = 127.0.0.1:9000

And replace it with:

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

Then, find the following lines and uncomment them.

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

Save and close the file.

Test PHP

Create a sample PHP file in the web root directory:

# nano /usr/local/www/nginx/test.php

Add the following line:

<?php phpinfo(); ?>

Save and close the file.

Then, enable and start PHP-FPM service with the following commands:

# sysrc php_fpm_enable=yes
# service php-fpm start

Finally, restart nginx service using command:

# service nginx restart

Now, open up your web browser and navigate to: http://IP-Address/info.php

You should see the following PHP test page.

phpinfo() - Google Chrome_006

Install PHP Modules

We need to install PHP modules (extensions) to enhance the functionality of PHP. This is optional, you can skip it if you don’t need to install any extensions.

To view the list of available modules, just run:

# pkg search php56

sk@sk: ~_010

You can verify what each module does from the comment section in the above output, or just run the following command

# pkg search -f php56-curl

Sample output:

php56-curl-5.6.18
Name : php56-curl
Version : 5.6.18
Origin : ftp/php56-curl
Architecture : freebsd:10:x86:64
Prefix : /usr/local
Repository : FreeBSD [pkg+http://pkg.FreeBSD.org/FreeBSD:10:amd64/quarterly]
Categories : ftp
Licenses : PHP301
Maintainer : ale@FreeBSD.org
WWW : http://www.php.net/
Comment : The curl shared extension for php
[...]

To install a PHP extension, for example php56-curl-5.6.18, run:

# pkg install php56-curl

Congratulations! At this stage, FEMP stack is ready to host your websites or any web based applications.

That’s all for now. Thank you for reading this tutorial. If you have any questions, feel free to ask in the comment section below. We will sort it out as soon as possible.

If you find this tutorial useful, please share it on your social networks and support OSTechNix.

Cheers!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More