How to Install Nginx, MySQL, PHP (LEMP) Stack on CentOS 8/RHEL8

LEMP STACK

A software stack is a set of software tools bundled together. LEMP stands for Linux, Nginx (pronounced engine X), MariaDB/MySQL and PHP, all of which are open source. It is the most common software stack that powers dynamic websites and web applications. Linux is the operating system; Nginx is the web server; MariaDB/MySQL is the database server and PHP is the server-side scripting language responsible for generating dynamic web pages.

Note If logging in as a user other than admin or root use su command to gain root access.

1. Install Nginx

Step 1 : Install nginx using the command:

yum install nginx -y

Step 2: Start and enable nginx service

systemctl start nginx
systemctl enable nginx

Step 3: Check the status of nginx

systemctl status nginx

Check Nginx version

nginx -v

Step 4: Accessing Nginx

If you are installing LEMP on your local RHEL 8/CentOS 8 computer, then type 127.0.0.1 or localhost in the browser address bar.

If RHEL 8/CentOS 8 is running on a remote server, then enter the hostname or IP address

Note Before accesing Nginx in remote server need to add services http, https in the firewall.

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
systemctl reload firewalld

Finally, we need to make user nginx as the owner of web directory. By default it’s owned by the root use

chown nginx:nginx /usr/share/nginx/html -R

2.Installing MariaDB server

Step 1 : Install MariaDB using the command:

yum install mariadb-server mariadb -y

Step 2: Start and enable MariaDB service

systemctl start mariadb
systemctl enable mariadb

Step 3: Check the status of MariaDB

systemctl status mariadb

Step 4: Run the Security Script

mysql_secure_installation

Note Press Enter and create a new password for the mariadb. Press Enter for all other options

Step 5: Entering into MariaDB server

mysql -u root -p

To exit, run

exit;

3.Installing PHP-FPM

Step 1 : Install PHP and related modules using the command:

yum install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y

Step 2: Start and enable php-fpm service

systemctl start php-fpm
systemctl enable php-fpm

Step 3: Check the status of php-fpm

systemctl status php-fpm

Step4: Change the Configuration file

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

By default, PHP-FPM runs as the apache user. Since we are using Nginx web server, we need to change it

user = nginx
group = nginx

Reload PHP-FPM for the changes to take effect.

systemctl reload php-fpm

Step 5: Testing PHP

Create a file in document root :

/usr/share/nginx/html/info.php

And paste the following code

<?php phpinfo(); ?>

Save and close the file. If you installed LEMP stack on a local RHEL 8/CentOS 8 server, type in 127.0.0.1/info.php or localhost/info.php in the browser address bar.

If RHEL 8/CentOS 8 is running on a remote server, then enter server-ip-address/info.php in browser address bar. Replace sever-ip-address with your actual IP address.

Trouble Shooting Errors

1. If the browser fails to display the PHP info but prompt you to download the info.php file, simply restart Nginx and PHP-FPM.

sudo systemctl restart nginx php-fpm

2. If problem during yum installation Like Could not resolve host:mirrorlist.centos.org;
This indicates that you either (a) don’t have a properly configured DNS server or (b) your network configuration isn’t correct.

Replace content in the configuration file /etc/resolv.conf with

nameserver 9.9.9.9

You can also try other DNS hosts if you would rather, such as 8.8.8.8 or 8.8.4.4 or any of the OpenDNS hosts.

Related Articles