How to Install LEMP Stack (Nginx, MariaDB, PHP7.2) on Rocky Linux 8

LEMP is a combination of free, open source software. The acronym LEMP refers to the first letters of Linux (Operating system), Nginx Server, MySQL (database software), and PHP, PERL or Python, principal components to build a viable general purpose web server.

Install Nginx Web Server

First, we will start by installing the Nginx web server. To complete the installation, use the following command:

yum install nginx -y

Output:

[root@server ~]# yum install nginx -y
Last metadata expiration check: 3:33:16 ago on Thu 06 May 2021 11:33:45 AM EDT.
Dependencies resolved.
================================================================================
 Package                     Arch   Version                     Repo       Size
================================================================================
Installing:
 nginx                       x86_64 1:1.14.1-9.module+el8.3.0+121+6327f1ce
                                                                appstream 566 k
Installing dependencies:
 dejavu-fonts-common         noarch 2.35-6.el8                  baseos     73 k
 dejavu-sans-fonts           noarch 2.35-6.el8                  baseos    1.5 M
 fontconfig                  x86_64 2.13.1-3.el8                baseos    273 k

Once the installation is complete, enable Nginx (to start automatically upon system boot), start the web server and verify the status using the commands below.

systemctl start nginx

systemctl enable nginx

systemctl status nginx

Output:

[root@server ~]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@server ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor prese>
   Active: active (running) since Thu 2021-05-06 15:08:01 EDT; 14s ago
 Main PID: 10636 (nginx)
    Tasks: 2 (limit: 4956)
   Memory: 3.9M
   CGroup: /system.slice/nginx.service
           ├─10636 nginx: master process /usr/sbin/nginx
           └─10637 nginx: worker process

Check Nginx version

nginx -v

Output:

[root@vps ~]# nginx -v
nginx version: nginx/1.14.1

To make your pages available to public, you will have to edit your firewall rules to allow HTTP requests on your web server by using the following commands.

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

firewall-cmd --permanent --zone=public --add-service=https

firewall-cmd --reload

Output:

[root@server ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@server ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@server ~]# firewall-cmd --reload
success

Verify that the web server is running and accessible by accessing your server’s IP address.

From your browser,

http://IP_address

image

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

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

Install MariaDB Server

MariaDB is a popular database server. The installation is simple and requires just a few steps as shown.

yum install mariadb-server mariadb -y

Output:

[root@server ~]# yum install mariadb-server mariadb -y
Last metadata expiration check: 3:36:48 ago on Thu 06 May 2021 11:33:45 AM EDT.
Dependencies resolved.
================================================================================
 Package             Arch   Version                             Repo       Size
================================================================================
Installing:
 mariadb             x86_64 3:10.3.28-1.module+el8.3.0+126+a7f7b5c5
                                                                appstream 6.0 M
 mariadb-server      x86_64 3:10.3.28-1.module+el8.3.0+126+a7f7b5c5
                                                                appstream  16 M
Installing dependencies:
 libaio              x86_64 0.3.112-1.el8                       baseos     31 k
 mariadb-common      x86_64 3:10.3.28-1.module+el8.3.0+126+a7f7b5c5

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the MariaDB and verify the status using the commands below.

systemctl start mariadb

systemctl enable mariadb

systemctl status mariadb

Output:

[root@server ~]# systemctl status mariadb
● mariadb.service - MariaDB 10.3 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor pre>
   Active: active (running) since Thu 2021-05-06 15:11:41 EDT; 16s ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 12068 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 30 (limit: 4956)
   Memory: 79.7M
   CGroup: /system.slice/mariadb.service
           └─12068 /usr/libexec/mysqld --basedir=/usr

Finally, you will want to secure your MariaDB installation by issuing the following command.

mysql_secure_installation

Output:

[root@server ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!   

Once secured, you can connect to MySQL and review the existing databases on your database server by using the following command.

mysql -e "SHOW DATABASES;" -p

Output:

[root@server ~]# mysql -e "SHOW DATABASES;" -p
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

Install PHP

To Install PHP-FPM by running the following command.

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

Output:

[root@server ~]# yum install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y
Last metadata expiration check: 3:40:09 ago on Thu 06 May 2021 11:33:45 AM EDT.
Dependencies resolved.
================================================================================
 Package           Arch   Version                               Repo       Size
================================================================================
Installing:
 php               x86_64 7.2.24-1.module+el8.3.0+200+280400f1  appstream 1.5 M
 php-fpm           x86_64 7.2.24-1.module+el8.3.0+200+280400f1  appstream 1.6 M
 php-gd            x86_64 7.2.24-1.module+el8.3.0+200+280400f1  appstream  83 k
 php-mbstring      x86_64 7.2.24-1.module+el8.3.0+200+280400f1  appstream 579 k
 php-mysqlnd       x86_64 7.2.24-1.module+el8.3.0+200+280400f1  appstream 189 k

Once the installation is complete, enable php-fpm (to start automatically upon system boot), start the php-fpm and verify the status using the commands below.

systemctl start php-fpm

systemctl enable php-fpm

systemctl status php-fpm

Output:

[root@server ~]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor pre>
   Active: active (running) since Thu 2021-05-06 15:14:35 EDT; 12s ago
 Main PID: 12664 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/s>
    Tasks: 6 (limit: 4956)
   Memory: 25.8M
   CGroup: /system.slice/php-fpm.service
           ├─12664 php-fpm: master process (/etc/php-fpm.conf)
           ├─12665 php-fpm: pool www
           ├─12666 php-fpm: pool www
           ├─12667 php-fpm: pool www

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

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

user = apache
group = apache

Change them to

user = nginx
group = nginx

Once changed, need to reload php-fpm

systemctl reload php-fpm

Test your PHP, by creating a simple info.php file with a phinfo() in it. The file should be placed in the directory root for your web server, which is /usr/share/nginx/html/info.php.

To create the file use:

echo "<?php phpinfo() ?>" > /usr/share/nginx/html/info.php

Restart the Nginx and PHP-FPM.

systemctl restart nginx php-fpm

Now again, access http://localhost/info.php or http://yourserver-ip-address/info.php. You should see a page similar to below one.

image

Done!