How to Install LEMP Stack on Rocky Linux 8

LEMP is a popular stack that comprises open-source software that is collectively used to host and serve web applications, whether in production or any stage along the development cycle.

The terminology LEMP is an acronym for Linux, Nginx (pronounced as Engine X, hence the E) which is a web browser, MariaDB or MySQL – database, and PHP for processing dynamic content). LEMP stack is popularly used for hosting high-traffic and highly scalable web applications and websites.

In this guide, you will learn how to install the LEMP stack on Rocky Linux 8.4.

Prerequisites

Before setting out to install the LEMP stack, ensure that you have the following requirements in place.

Let’s get started…

Step 1: Install Nginx on Rocky Linux

The first step is to install the first component of the LEMP stack which is the Nginx web server. First, upgrade the packages.

$ sudo dnf update -y

After the update is complete, install Nginx by running the following command. This installs Nginx alongside other dependencies required by the webserver.

$ sudo dnf install nginx 
Install Nginx on Rocky Linux
Install Nginx on Rocky Linux

Once Nginx is in place, enable it to start on boot time and start the Nginx daemon.

$ sudo systemctl enable nginx $ sudo systemctl start nginx 

To confirm that the web server is running, execute the command:

$ sudo systemctl status nginx

From the output, we can conclude that the webserver is up and running.

Check Nginx Status
Check Nginx Status

If you are curious enough, you can check the version of Nginx as follows. The output indicates we are running Nginx 1.14.1.

$ nginx -v nginx version: nginx/1.14.1

Additionally, you can confirm that the browser is working by browsing the URL shown. This will display the default Nginx Welcome page indicating that all is well.

http://server-ip or domain name
Check Nginx Page on Rocky Linux
Check Nginx Page on Rocky Linux

If you are having issues viewing the page, consider opening port 80 or allowing the HTTP traffic on the firewall.

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

Then reload the firewall and reload the page.

$ sudo firewall-cmd --reload

Step 2: Install MariaDB on Rocky Linux

For this guide, we will install the MariaDB database. This is due to the optimized performance and wealth of storage engines it provides which makes it far more superior to MySQL.

To install the MariaDB database server, run the command:

$ sudo dnf install mariadb-server mariadb
Install MariaDB on Rocky Linux
Install MariaDB on Rocky Linux

Once done, enable and start MariaDB as shown.

$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb

Then verify its status.

$ sudo systemctl status mariadb
Check MariaDB Status
Check MariaDB Status

The default settings for MariaDB are not secure enough and your database can easily be breached. As a precaution to deter intruders at the most basic level, run the script below.

$ sudo mysql_secure_installation

Be sure to set up the Root password.

Set MariaDB Root Password
Set MariaDB Root Password

For the remaining prompts, simply type 'Y' to remove anonymous users, deny remote root login, remove the test database, and finally save the changes made.

Secure MariaDB Installation
Secure MariaDB Installation

To login to the database server, run the command:

$ sudo mysql -u root -p

Provide the password and hit ENTER.

Connect MariaDB Database
Connect MariaDB Database

Step 3: Install PHP on Rocky Linux

The last component to install is PHP via PHP-FPM, which stands for FastCGI Process Manager. This is an efficient and highly advanced processor for PHP that provides features that guarantee optimal performance, and security for high-traffic websites.

To start off, we will install the Remi repository which is a third-party free repository that provides the latest PHP versions.

To enable the Remi repository, run the command:

$ sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Enable Remi Repository on Rocky Linux
Enable Remi Repository on Rocky Linux

Once the Remi repository is enabled, check out the list of PHP modules that are hosted using the command shown.

$ sudo dnf module list php
List PHP Modules
List PHP Modules

From the output, we can see that the default version is 7.2 – with the tag [d]. However, we are going to install the latest module which is Remi 8.0.

Therefore, reset the default PHP modules and enable the latest Remi PHP module.

$ sudo dnf module list reset php
$ sudo dnf module enable php:remi-8.0
Reset PHP Modules
Reset PHP Modules

Next, update the system and install PHP and PHP-FPM alongside PHP extensions of your preference.

$ sudo dnf install php php-fpm php-gd php-mysqlnd php-cli php-opcache
Install PHP on Rocky Linux
Install PHP on Rocky Linux

Once the installation is complete, enable and start PHP-FPM as shown.

$ sudo systemctl enable php-fpm
$ sudo systemctl start php-fpm

Next, verify the running status of PHP-FPM.

$ sudo systemctl status php-fpm
Check PHP-FPM Status on Rocky Linux
Check PHP-FPM Status on Rocky Linux

Normally, PHP-FPM runs as the Apache user, but since we are using Nginx, we need to set it to Nginx. So, open the following configuration file.

$ sudo vim /etc/php-fpm.d/www.conf

Set the user and group to Nginx.

user = nginx
Group = nginx

Thereafter, reload the PHP-FPM daemon.

$ sudo systemctl reload php-fpm

To confirm that we have installed the latest version of PHP, run the command.

$ php -v
Check PHP Version
Check PHP Version

Another nifty way of Testin PHP is by creating a simple PHP file and placing it in the webroot directory which is located in /usr/share/nginx/html. So, create a simple info.php file in the /usr/share/nginx/html webroot directory.

$ sudo vim /usr/share/nginx/html/info.php

Add the following content and save the file.

<?php phpinfo(); ?>

To effect the changes, reload the Nginx web server.

$ sudo systemctl restart nginx

Finally, access the following URL.

http://server-ip/info.php

A webpage with detailed information about the installed PHP version alongside other PHP extensions will be displayed.

Check PHP Info
Check PHP Info

And this wraps it up. In this guide, we have walked you through the installation of the LEMP stack on Rocky Linux 8.

Posted by Contributor