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
Related Posts:
- How to Rapidly Install Java, OpenJDK & Oracle JDK on your VPS - December 14, 2015
- It’s been a great ride - December 14, 2015
- Cheap Windows VPS – $21/quarter 1GB KVM-based Windows VPS in 11 worldwide locations - November 30, 2015
Nice introduction :) – haven’t used MariaDB personally, but may give it a go soon!
Debian! But nice introduction.
Whoops, copy-paste much :P
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 :)
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
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.
So Joseph, tell us why this is insecure… unless, as I suspect you don’t know what you’re talking about.
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.
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
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/
Yes, it can. MariaDB is a drop-in replacement for MySQL
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.
CentminMod looks cool. Only for CentOS?
or you can use:
http://centminmod.com/getstarted.html
This makes it pretty easy to install that same stack :).
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.
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 !
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
Hi, If Im going to host only html and wordpress websites, Lighttpd and TokuDB recommended? ( and community or enterprise edition of tokudb? )
Why am I getting 502 Bad Gateway?
erorr my vps..
my vps error to