How To Install Linux, Nginx, MariaDB, PHP (LEMP Stack) in CentOS 7 / RHEL 7

8
THIS DOCUMENT IS ALSO AVAILABLE FOR

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).

Install Linux

Here is the article about Step by Step installation of CentOS 7 / RHEL 7.

Now you have Linux, and the next is to install Apache, MySQL, and PHP on it. Let’s install one by one.

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

$ su

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 start nginx

Firewall

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

firewall-cmd --permanent --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.

Test Nginx

Open your web browser and visit the following URL.

http://your-ip-address

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

Install Linux, Nginx, MariaDB, PHP (LEMP Stack) in CentOS 7 – Nginx Default Page
Install Linux, Nginx, MariaDB, PHP (LEMP Stack) in 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 MariaDB

CentOS 7 / RHEL 7 offers MariaDB v5.5 from base OS repository, which is a bit older and going to be EOL soon. You can consider installing MariaDB v10.x or MySQL 8.x for your LEMP stack.

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

yum -y install mariadb mariadb-server

Start the 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. You can install phpMyAdmin to manage databases over a web browser.

Install PHP

PHP v5.4 (End Of Support)

The PHP version (v5.4) available in the CentOS base repository is already the end of support.

PHP v7.3

Remi, a third party repository which offers up to date version of PHP (v7.3). To enable the Remi repository, install the Remi repository auto-configuration package.

yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm

Install PHP through PHP-FPM (PHP-FPM (FastCGI Process Manager) using YUM command.

yum install -y --enablerepo=remi-php73 php php-fpm php-mysqlnd php-cli

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

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

Verify PHP-FPM listens on port 9000.

listen = 127.0.0.1:9000

Make sure the following values are UN-commented.

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

Test LEMP Stack

Create 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

Create the document root directory.

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

Test Virtual host

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:

echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/itzgeek.local/index.php

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 Linux, Nginx, MariaDB, PHP (LEMP Stack) in CentOS 7 – PHP Information
Install Linux, Nginx, MariaDB, PHP (LEMP Stack) in 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 Linux, Nginx, MariaDB, PHP (LEMP Stack) in CentOS 7 – Nginx MariaDB Support
Install Linux, Nginx, MariaDB, PHP (LEMP Stack) in CentOS 7 – Nginx MariaDB Support

Conclusion

That’s All. You have successfully set up LEMP stack on CentOS 7 / RHEL 7. Additionally, you can install phpMyAdmin on CentOS 7 to manage MariaDB / MySQL databases via a web browser.

You might also like