Install Nginx + MariaDB + PHP on CentOS 7 / RHEL 7

8

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 CentOS 7 / RHEL 7 with PHP support (through PHP-FPM) and MariaDB support.

PHP-FPM is an alternative PHP FastCGI implementation, and it has some additional features useful for sites of any size, especially busier sites).

The first thing you need to do is to log in as the root user or switch to the root user.

$ su

Install MariaDB

First, we will install the MariaDB by issuing the following command.

yum -y install mariadb mariadb-server

Start MariaDB server using the following command.

systemctl start mariadb

Enable the MariaDB to start automatically during system boot.

systemctl enable mariadb

Once the installation is complete, you need to secure MariaDB using the mysql_secure_installation command.

Install Nginx

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

### CentOS 7 ###

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

### RHEL 7 ###

rpm -Uvh http://nginx.org/packages/rhel/7/noarch/RPMS/nginx-release-rhel-7-0.el7.ngx.noarch.rpm

Install Nginx using the following command.

yum -y install nginx

Start the Nginx service after the installation.

systemctl restart nginx

IPtables

Run the following commands in the terminal to allow HTTP requests through the firewall.

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

SELinux

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

Else, disable the SELinux using the following command temporarily for the current session.

setenforce 0

We recommend you to disable the SELinux permanently.

Open your web browser and visit the following URL.

http://your-ip-address

You should see the following page. This page confirms you that the Ngnix is successfully installed on the server.

Install Nginx + MariaDB + PHP on CentOS 7 - Nginx Default Page
Install Nginx + MariaDB + PHP on CentOS 7 – Nginx Default Page

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

Auto-start the Nginx service at system startup.

systemctl enable nginx

Install PHP

Next is to install PHP through PHP-FPM (PHP-FPM (FastCGI Process Manager). Install it using YUM command.

yum -y install php-fpm php-mysql php-cli

Edit /etc/php.ini.

# vi /etc/php.ini

set cgi.fix_pathinfo=0

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.

[.More.]
pm.min_spare_servers = 5
[.More.]
pm.max_spare_servers = 35
[.More.]

Enable PHP-FPM Support on Virtual Host

Let’s create a name-based virtual host on Nginx server for the following details.

Server Name : server.itzgeek.local
Document Root : /usr/share/nginx/html/itzgeek.local

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 {
        server_name server.itzgeek.local;
        root /usr/share/nginx/html/itzgeek.local;

location / {
        index index.html index.htm index.php;
}

location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/itzgeek.local$fastcgi_script_name;
}
}

Create host entry for your domain (server.itzgeek.local) in the /etc/hosts file.

vi /etc/hosts

Add a host entry like below.

127.0.0.1               localhost.localdomain localhost server.itzgeek.local

Test PHP-FPM support on Virtual host

Create the document root directory.

mkdir /usr/share/nginx/html/itzgeek.local

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/itzgeek.local/index.php

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
systemctl restart php-fpm
systemctl enable php-fpm

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

http://server.itzgeek.local

The page will look like below:

Install Nginx + MariaDB + PHP on CentOS 7 - PHP Information
Install Nginx + MariaDB + PHP on CentOS 7 – PHP Information

From the above screenshot, 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 the MariaDB support information.

Install Nginx + MariaDB + PHP on CentOS 7 - Nginx MySQL Support
Install Nginx + MariaDB + PHP on CentOS 7 – Nginx MySQL Support

That’s All.

You might also like

Install Nginx + MariaDB + PHP on CentOS 7 / RHEL 7

8