LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

An introduction to NGINX, PHP-FPM and MariaDB on CentOS

lowendtutorial

Last week, I wrote about setting up a NGINX, PHP-FPM and MariaDB stack for Ubuntu. Today we’re going to tackle the same stack for 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).

EPEL

NGINX and PHP-FPM

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 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    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 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini

# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# 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 = 127.0.0.1:9000

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:

<?php

phpinfo();

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.

MariaDB

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/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

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

mpkossen

21 Comments

  1. Nice introduction :) – haven’t used MariaDB personally, but may give it a go soon!

    December 28, 2013 @ 8:40 pm | Reply
  2. Debian! But nice introduction.

    December 28, 2013 @ 9:35 pm | Reply
    • Maarten Kossen:

      Whoops, copy-paste much :P

      December 30, 2013 @ 12:26 am | Reply
  3. Joseph:

    Great article.

    Just my $0.02, don’t use for production site if you only know copy paste method from article above :). This installation instruction is easy to follow, and yes it work. Unfortunately, your box need to be securing, and hardening and securing your server is the hard part. To make your server secure, is not easy like use YUM or APT GET :)

    December 28, 2013 @ 11:18 pm | Reply
    • Frank:

      So Joseph, would you please tell us what’s the -at least- basic method to secure a Nginx vps for production server. Any tutorial much appreciated.

      Thanks

      December 29, 2013 @ 8:04 am | Reply
      • Alexey Zilber:

        That really depends how paranoid you are. I use sshutout to secure the server, and use the Suhosin module to make sure the code you run, if not written by you, won’t open your server to injections and attacks.

        December 30, 2013 @ 1:30 am | Reply
    • hmmmm:

      So Joseph, tell us why this is insecure… unless, as I suspect you don’t know what you’re talking about.

      December 29, 2013 @ 12:28 pm | Reply
    • Maarten Kossen:

      There is a tutorial available on how to get started with iptables, if that’s what you mean? See http://lowendbox.com/blog/introduction-to-iptables/

      Other than that, this should be pretty secure by default. You could set a root password for MariaDB, though that’s only applicable to local connections as root can only login from those. For me, that was all “next step” compared to the things in this tutorial.

      December 30, 2013 @ 12:30 am | Reply
  4. I love Mariadb Galera works awesome as a cluster i have mine on Ubuntu with Nginx load balancing between several vps for a API service. +1 for Mariadb

    December 29, 2013 @ 12:48 am | Reply
  5. Great guides. Can WordPress be installed on MariaDB?

    Have always been using the LEB’s Bootstraping WordPress/Nginx/PHP/MySQL on a Cheap VPS with lowendscript @ http://lowendbox.com/blog/wordpress-cheap-vps-lowendscript/

    December 29, 2013 @ 3:29 am | Reply
    • hmmmm:

      Yes, it can. MariaDB is a drop-in replacement for MySQL

      December 29, 2013 @ 12:30 pm | Reply
  6. Thanks for the tutorial. But, I use menu base Nginx installer CentminMod for easy installer. Just choose option from the menu, Nginx, PHP-FPM and Maria DB will automatically installed.

    December 29, 2013 @ 12:03 pm | Reply
  7. or you can use:

    http://centminmod.com/getstarted.html

    This makes it pretty easy to install that same stack :).

    December 29, 2013 @ 5:15 pm | Reply
    • Maarten Kossen:

      Yep, there’s a lot of installers available. What worries me, though, is that people use this installers without knowing what’s happening. This probably means they have a harder time fixing issues as well.

      Other than that, if you know how this stuff works, those installers are really great.

      December 30, 2013 @ 12:32 am | Reply
      • Took me six months toi figure out what I was doing,am not there yet but I now I know what the Yellow button does. Openvpn, nginx with page speed and mod security, ffmpeg/avconv fully loaded. Video torrent files downloading , downgrading via handbrake on server side and streaming with SMS alerts, Bastille , csf and a little bash too.

        Moved my site to a brand new vps. Thanks to LEB for the deals and & ec2 for the free Lego !

        December 31, 2013 @ 1:04 pm | Reply
  8. Alexey Zilber:

    A few suggestions:

    1. Swap out MariaDB for TokuDB. Even though the latest MariaDB supports the tokudb engine, it’s not 100% and isn’t available for Centos. Check out the tutorial here: http://www.devtome.com/doku.php?id=mariadb

    2. As much as I like Nginx. Lighttpd is sometimes an easier and better choice. It’s installed in a similar fashion and supports php-fpm as well, which is how I’m running it now.

    3. You MAY want to upgade your php. Latest PHP yum repos are here: http://www.webtatic.com/ Instructions for upgrades, etc are there. You can build the latest xcache for php using spec files here: https://github.com/deogracia/centos-spec-files

    December 30, 2013 @ 1:28 am | Reply
    • Saul:

      Hi, If Im going to host only html and wordpress websites, Lighttpd and TokuDB recommended? ( and community or enterprise edition of tokudb? )

      January 26, 2014 @ 10:29 pm | Reply
  9. Vizius:

    Why am I getting 502 Bad Gateway?

    June 2, 2014 @ 2:27 pm | Reply
  10. erorr my vps..

    October 18, 2014 @ 10:11 pm | Reply
  11. my vps error to

    February 20, 2016 @ 2:57 pm | Reply

Leave a Reply to James Monroe Cancel reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *