View Thread
Web Master Trouble » Linux Apache MySQL PHP optimalisation tips tricks » Linux tutorials, Tips & Tricks
Linux tutorials, Tips & Tricks Getting started with SSH An introduction to NGINX, PHP-FPM and MariaDB on CentOS An introduction to NGINX, PHP-FPM and MariaDB on Ubuntu Getting started with OpenVZ! Handle DNS properly with cPanel Munin amass, all your server charts in one place Monitoring server performance with Munin! Getting started with OpenVPN (client) Getting started with OpenVPN (server) Your own mail server with Virtualmin IPtables: IPv6 and more rules
 Print Thread
An introduction to NGINX, PHP-FPM and MariaDB on CentOS

A couple of years back, getting a web server up and running involved setting up Apache, mod_php and MySQL. Everything was owned by 'a user' and if a script needed file access, you would just chmod 777 all the files that needed to be accessed. Over the past couple of years, that landscape has shifted. Nowadays, mod_php is the slowest option you could probably go with and MySQL isn't all hipster anymore since Oracle bought Sun. Apache is still at large, but requires more resources to accomplish what NGINX does with less.

So, let's go set up a web server that runs NGINX, PHP-FPM and MariaDB! MariaDB is a MySQL fork created by the original author of MySQL. It's slowly replacing MySQL as the database server on Linux distributions and I have to say, I'm a big fan as well. PHP-FPM is a process manager for PHP that makes PHP run independently from the web server, which has multiple advantages over including PHP in the web server process (which is wrong to being with).



First update CentOS:

sudo yum update

Then, let's clean out any packages that may make you go 'why doesn't it work?':

sudo yum erase httpd httpd-tools

That's Apache out the door, including any libraries. The above may or may not be necessary, depending on what kind of VPS you have and what kind of template. It's safe to run it anyway.

Now, let's install NGINX and PHP-FPM:

sudo yum install nginx php-fpm

When this is done and you visit your server's hostname or IP address, you should see the NGINX landing page. This means installation has succeeded.

On to the configuration. Open up /etc/nginx/conf.d/default.conf. Look for a code block that looks like this:

# pass the PHP scripts to FastCGI server listening on
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;

This snippet is from NGINX's default virtual host configuration file and enables us to run PHP-FPM with NGINX. We're going to change it to this:

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have 'cgi.fix_pathinfo = 0;' in php.ini

# # With php5-cgi alone:
# fastcgi_pass;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;

As you see, we've modified it a bit. I'm going to explain them line by line.

location ~ \.php$ {

This matches any location ending in *.php, which make sure that such requests get into the following block of code.

fastcgi_split_path_info ^(.+\.php)(/.+)$;

This matches the script's file name and any variables that may have been added after that. It splits it up into two variables for later use by NGINX.

fastcgi_pass unix:/var/run/php5-fpm.sock;

This line passes on the request to the PHP-FPM socket, which we'll configure in a bit. It basically sends the request there for it to be handled.

fastcgi_index index.php;

This is the name of the file NGINX should fall back to if no filename is given.

include fastcgi_params;

This includes any parameters made by the fastcgi module (like the ones we split before) and makes them available to NGINX.

That's it NGINX-wise, let's configure PHP-FPM. Open up /etc/php-fpm.d/www.conf. Look for the following line:

listen =

And change it to:

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

This tells PHP-FPM to open a UNIX socket rather than listening on a port. This should speed things up as well.

Now, restart both PHP-FPM and NGINX:

sudo /etc/init.d/php-fpm restart

sudo /etc/init.d/nginx restart

And you're good! NGINX now works with PHP files and passes those on to PHP-FPM. If you want to test this, create a file in /usr/share/nginx/html and put this in it:


If you surf to that file, it should display information about your PHP environment. Be sure to remove the file afterwards, as you probably don't want all those details lying around in the open.


The final step for this tutorial is installing MariaDB.

Let's add the repository. Create the file /etc/yum.repos.d/MariaDB.repo and put this in it:

# MariaDB 5.5 CentOS repository list - created 2013-12-27 07:29 UTC
# http://mariadb.org/mariadb/repositories/
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-x86

This repository should suit you fine. If you want something closer to home, go here: https://downloads.mariadb.org/mariadb/repositories/

And install MariaDB and php-mysql, so we can use MariaDB with PHP later:

sudo yum install MariaDB-server MariaDB-client php-mysql

Once that's done, there's a final step:

sudo /etc/init.d/php-fpm restart

Restarting PHP-FPM so the MySQL plugin gets loaded.

That's it, you're good to go!

What's Next

You're ready to install a script or an application now, but there's a couple of things you should know. First, we've only configured the default virtual host. This is not bad per se, but less suitable for a multi-user environment. Also, all files go into /usr/share/nginx/html and should be owned by www-data. This ensures everything keeps running smoothly. Finally, you need to set up a database by hand in case you need one. You could use a tool like PhpMyAdmin, but be sure to properly secure it.

As this guide was an introduction to NGINX, PHP-FPM and MariaDB, there's a lot more to write about it. For example, the multi-user or multi-site environment I mentioned before. But also running this efficiently on a LowEndBox. Possibly even making the installation easier and configuring SSL. That's for the future, though ;-)

Up next time: Getting started with SSH on Windows and Ubuntu

Warning about ParkingCrew.com! Case: ParkingCrew.com acquires NameDrive.com but earnings are not transferred despite assurances and promises. Inquiries about this are ignored! It's just a con compagny. Don't do business with them!
Jump to Forum
New Thread Post Reply
Use BBcode or HTML to refer to; 'An introduction to NGINX, PHP-FPM and MariaDB on CentOS'
Simular threads
Subject Discussion Forum         Last Post
NGINX on CentOS 7 Linux tutorials, Tips & Tricks : 1 21-09-2015
Setup for streaming live events to browser clients using Nginx, FMLE, and FlowPlayer Linux tutorials, Tips & Tricks : 1 16-09-2014
An introduction to NGINX, PHP-FPM and MariaDB on Ubuntu Linux tutorials, Tips & Tricks : 1 30-11-2013