Search Our Database

How to Install and Configure a LEMP Stack on RHEL-Based Linux Servers

Last updated on |
by

Introduction

A LEMP stack (Linux, Nginx, MariaDB/MySQL, PHP) is an open-source software combination used to host dynamic websites and web applications. This guide explains how to install and configure Nginx, MariaDB, and PHP-FPM on RHEL-based Linux servers, such as AlmaLinux and Rocky Linux. These steps are useful when preparing a new server for web hosting.

 

Prerequisite

  • Access to the server via SSH with root or sudo privileges
  • Basic understanding of navigating the command-line interface
  • A supported RHEL-based distribution with access to DNF repositories and a server IP address

 

Step-by-step Guide

Step 1: Enter Administrator Privileges

  • Switch to the root account to run the installation commands without entering sudo each time. Only use administrator privileges when required.
sudo su

Step 2: Update Your System

  • Update installed packages to receive the latest available bug fixes and security updates. Restart the server if the update installs a new kernel or other components that require a reboot.
dnf update -y

Step 3: Check and Disable SELinux

  • First, check the current active state of SELinux on your server. If the status shows it is enforcing strict security policies, temporarily disable it by setting it to permissive mode to prevent permission conflicts during the installation and configuration of the LEMP stack
  • Check the current SELinux status:
sestatus
  • The output may show Enforcing, Permissive, or Disabled.
  • Temporarily change SELinux to permissive mode, if it is currently enforcing:
setenforce 0

Step 4: Install and Start Nginx Web Server

  • Nginx is a lightweight web server that delivers website content and forwards PHP requests to PHP-FPM.
  • Install Nginx:
dnf install nginx -y
  • Start Nginx and enable it to run automatically on system boot:
systemctl start nginx
systemctl enable nginx
  • Install and configure the firewall to allow HTTP and HTTPS traffic:
dnf install firewalld -y
systemctl start firewalld
systemctl enable firewalld
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
  • Verify that Nginx is running by checking its service status:
systemctl status nginx
  • Expected Output:

Step 5: Install and Secure MariaDB

  • MariaDB stores and manages application data, such as user accounts and website content.
  • Install the MariaDB server:
dnf install mariadb-server mariadb -y
  • Start and enable MariaDB:
systemctl start mariadb
systemctl enable mariadb
  • Run the database security wizard to remove anonymous users, restrict remote root access, and delete the test database. Follow the prompts carefully; password and authentication questions vary by MariaDB version:
mysql_secure_installation
    • Press Enter if asked for the current root password for none or type your desired password to set a new password.
    • Type ‘n’ for “Change the root password? [Y/n]” prompt.
    • Accept the defaults for the remaining prompts by pressing Enter or typing Y.

Step 6: Install and Configure PHP-FPM

  • PHP processes dynamic website content, while PHP-FPM allows Nginx to execute PHP scripts. Available PHP versions and module streams depend on your operating system release and enabled repositories.
  • Choose ONE of the following PHP installation methods. Confirm that the selected PHP version is available in your server repositories before enabling it.
Method A: Distribution AppStream (Recommended When Available)

1. Reset the default PHP module to clear any existing version configurations:

dnf module reset php -y

2. Enable the required PHP stream, if your operating system provides it (PHP 8.3 is shown as an example):

dnf module enable php:8.3 -y
  • Use this method when your required PHP version is not available from the distribution repositories.
Method B: Remi Repository (Advanced)

1. Enable the EPEL and Remi repositories:

dnf install epel-release -y
dnf install https://rpms.remirepo.net/enterprise/remi-release-$(rpm -E %rhel).rpm -y

2. Reset PHP modules and enable your desired version from Remi (e.g., PHP 8.3):

dnf module reset php -y
dnf module enable php:remi-8.3 -y
  • Install PHP along with the essential extensions commonly required by modern web applications:
dnf install php php-cli php-common php-mysqlnd php-fpm php-gd php-opcache php-mbstring php-xml -y
  • Start and enable PHP-FPM, then check that it is running:
systemctl enable --now php-fpm
systemctl status php-fpm

Expected Output:


Confirm the PHP-FPM socket and allow its worker processes to run as the Nginx user. Open the pool configuration:

dnf install nano -y
nano /etc/php-fpm.d/www.conf
  • Check or update these settings (uncomment them if necessary):
user = nginx
group = nginx
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
  • Save your changes and restart PHP-FPM:
systemctl restart php-fpm
  • Enable PHP processing in the existing Nginx default server block:
nano /etc/nginx/nginx.conf
  • Inside the existing server { … } block that listens on port 80, ensure the index directive supports PHP and add the following location block. Do not create a second default server block:
index index.php index.html;

location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php-fpm/www.sock;
}
    • For example:

    • Save and close the file (Ctrl + O > Enter > Ctrl + X).
    • Check the configuration and reload Nginx:
nginx -t
systemctl reload nginx

Step 7: Test Your LEMP Environment

  • Check whether Nginx can successfully pass PHP requests to PHP-FPM.
    Create a test PHP file in the default web root directory:
nano /usr/share/nginx/html/info.php
  • Paste the following line inside the file:
<?php phpinfo(); ?>
  • Save and close the file (Ctrl + O > Enter > Ctrl + X).
  • Open your web browser and go to http://YOUR_DOMAIN_NAME/info.php. If a PHP information page appears, PHP-FPM is working correctly with Nginx. If the browser downloads the file instead, review the Nginx PHP location configuration before continuing.
  • Expected output:

Important Note ⚠️: Remove the test file when finished:

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

Step 8: Create Your Website Directory & Nginx Server Block

  • For a custom domain, create a separate website directory and Nginx server block.
    Create a directory for your website (replace your_domain_name.com with your actual domain):
mkdir -p /var/www/your_domain_name.com
  • Assign proper ownership and permissions:
chown -R nginx:nginx /var/www/your_domain_name.com
chmod -R 755 /var/www/your_domain_name.com
  • Create a sample index page to verify your site directory:
nano /var/www/your_domain_name.com/index.html
  • Add some simple HTML code (Ctrl + O > Enter > Ctrl + X to save and exit):
  • For example:
<html>
 <head>
   <title>Welcome to My Website</title>
 </head>
 <body>
   <h1>This is my LEMP website</h1>
 </body>
</html>
  • Create a new Nginx server block for your domain:
nano /etc/nginx/conf.d/your_domain_name.com.conf
  • Paste the following configuration. Make sure server_name and root match your domain and website directory:
server {
listen 80;
server_name your_domain_name.com www.your_domain_name.com;
root /var/www/your_domain_name.com;
index index.php index.html;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php-fpm/www.sock;
}

access_log /var/log/nginx/your_domain_name.com_access.log;
error_log /var/log/nginx/your_domain_name.com_error.log;
}
  • Test your Nginx configuration and reload the service:
nginx -t
systemctl reload nginx

Step 9: Verify That Your Website Is Running

  • Open your web browser and type your domain name (e.g. your_domain_name.com) in search bar. Then, press Enter.
  • Expected Output: your sample index.html page displays the message This is my LEMP website.
Tips 🖊️:
If DNS is not ready, you can test directly from the server using:

curl -H 'Host: your_domain_name.com' http://127.0.0.1/

Expected output:

Note: This example uses HTTP for initial testing. Before hosting a live website, configure a valid SSL/TLS certificate to enable HTTPS.

 

Conclusion

You have now installed and configured a LEMP stack on your RHEL-based Linux server. Nginx serves your website, MariaDB manages its data, and PHP-FPM processes PHP scripts. For production use, enable HTTPS and keep system updates and security controls in place.

For additional assistance or if you encounter any issues, please contact our support team at support@ipserverone.com.