Nginx + PHP-FPM + MariaDB on CentOS 7 / RHEL 7


Nginx is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows you how to install Nginx on an CentOS 7 / RHEL 7 with PHP support (through PHP-FPM) and MariaDB support. PHP-FPM is an alternative PHP FastCGI implementation, it has some additional features useful for sites of any size, especially busier sites).
The first thing you need to do is to login as root, switch to root user.

$ su

Installing MariaDB:

First we will install the MySQL by issuing the following command.

# yum -y install mariadb mariadb-server

Start MySQL server.

# systemctl start mariadb.service

Step 3: To make the MySQL to start during the every boot, Type the following on terminal and hit Enter.

# systemctl enable mariadb.service

Next is to make the MariaDB secure by using the mysql_secure_installation command.

Installing Nginx:

Install Nginx repository rpm to download the Nginx from the official site.

# rpm -Uvh

Install Nginx using the following command.

# yum -y install nginx

Start the Nginx after the installation.

# systemctl restart nginx.service

Iptables and SELinux:

Issue the following commands to allow http request through the firewall.

# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --reload

If you get any error of SELinux on the Nginx logs, here is the tutorial on setting SELinux policy for Nginx.

Open web browser and visit http://your-ip-address, you should see the following page; this will confirm you that the ngnix is successfully installed on the server.

CentOS 7 - Nginx Default Page
CentOS 7 – Nginx Default Page

The default nginx document root on CentOS 7 is /usr/share/nginx/html/. The configuration files are under /etc/nginx directory.

Auto start Nginx at system start up.

# systemctl enable nginx.service

Installing PHP5-FPM:

Next is to install PHP through PHP-FPM (PHP-FPM (FastCGI Process Manager), it is an alternative PHP FastCGI implementation. it has some additional features useful for sites of any size, especially busier sites). Install it by issuing the following command.

# yum install php-fpm php-mysql php-cli

Edit /etc/php.ini.

# vi /etc/php.ini

set cgi.fix_pathinfo=0


Edit /etc/php-fpm.d/www.conf file,

# vi /etc/php-fpm.d/www.conf

Make sure the following values are UN-commented.

pm.min_spare_servers = 5
pm.max_spare_servers = 35

Enabling PHP-FPM Support on Virtual Host:

Lets create name based virtual host on Nginx server for the following details.

Server Name :
Document Root : /usr/share/nginx/html/

Create the configuration file called virtual.conf and Edit /etc/nginx/conf.d/virtual.conf.

# vi /etc/nginx/conf.d/virtual.conf

Add the following content.

server {
root /usr/share/nginx/html/;
location / {
index index.html index.htm index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;

Create host entry for your domain ( in the /etc/hosts file.

# vi /etc/hosts

Add host entry , the line should look like below.               localhost.localdomain localhost

Testing PHP-FPM support on Virtual host:

Create the document root directory.

mkdir /usr/share/nginx/html/

For testing the PHP, Place one PHP file on to the document root of the created virtual host, In the terminal copy/paste the following line:

# vi /usr/share/nginx/html/

This will open up a file called index.php, Copy/Paste this line into the index.php file:

<?php phpinfo(); ?>

Save and close the file. use Esc + ;wq for saving the file.

Restart the services and Enable autostart.

# systemctl restart nginx.service
# systemctl restart php-fpm.service
# systemctl enable php-fpm.service

Now open up your web browser and type the your domain in the web address:

The page look like below:

CentOS 7 - Nginx PHP FPM
CentOS 7 – Nginx PHP FPM

From the above screen shot , PHP is working, and it’s working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP.
Scroll down the browser and look for the MySQL support information.

CentOS 7 - Nginx PHP FPM With MariaDB Support
CentOS 7 – Nginx PHP FPM With MariaDB Support

That’s all!


Share This Post

  • Hi,

    I don’t see any SELinux or firewalld configurations in your tutorial, is it normal?


    • Raj

      Thanks for Notifying me, updated the post

  • Misty

    I having problems when i edit my docroot for /home/user/www 🙁

    I give forbidden access

  • Henry

    You forgot an important step in the setup of MariaDB!
    Security!!! this will clean and also allows to set a password for root access.

    Between step 2 and 3 do following:

    • Raj

      Dear Henry,

      Its already been there, i have mentioned the URL to the article.

  • Anson5

    Would I need to enable the php-fpm.service?
    i.e. systemctl enable php-fpm.service

  • cr4zyc0der

    where is the info for wordpress after this?