Tutorials  /  Linux Basics

Install LEMP Stack on Rocky Linux 8: Nginx, MariaDB, PHP Guide

Ccentron Redaktion · May 2025 ·8 min read ·Linux Basics, Tutorial

The LEMP stack, which includes Linux, Nginx (commonly pronounced as Engine X), MySQL, and PHP, is a group of applications installed to power dynamic websites on a server. In this setup, Nginx acts as the web server, MySQL functions as the database server, and PHP is used as the dynamic scripting language, supporting a variety of frameworks for web development. This guide explains how to install LEMP on Rocky Linux 8.

Prerequisites

  • Deploy a fresh Rocky Linux 8 server
  • Log into the server as a non-root user with sudo permissions
  • Ensure the server system is updated
  • Configure a domain name and link it to your server
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Step 1: Install Nginx

Begin by installing the Nginx web server.

Console
$ sudo dnf install nginx

Enable Nginx to automatically launch at system startup.

Console
$ sudo systemctl enable nginx

Start the Nginx service.

Console
$ sudo systemctl start nginx

Open HTTP traffic through the firewall.

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

Apply the new firewall rules by reloading them.

Console
$ sudo firewall-cmd --reload

To verify that Nginx is functioning, open your browser and navigate to your server's IP address:

http://1.2.3.4

Step 2: Install MySQL/MariaDB

Next, install the MariaDB database server.

Console
$ sudo dnf install mariadb-server

Enable MariaDB to start automatically during system boot.

Console
$ sudo systemctl enable mariadb

Launch the MariaDB service.

Console
$ sudo systemctl start mariadb

Step 3: Install PHP

Proceed by installing PHP along with PHP-FPM (FastCGI Process Manager).

Console
$ sudo dnf install php php-fpm

Install the commonly used PHP extensions needed for a majority of web applications.

Console
$ sudo dnf install php-mysqlnd php-cgi php-bcmath php-json php-xml php-gd php-zip php-intl php-mbstring

Enable the PHP-FPM service to automatically launch at boot.

Console
$ sudo systemctl enable php-fpm

Finally, start the PHP-FPM service.

Console
$ sudo systemctl start php-fpm

Step 4: Configure MariaDB

Initialize the MariaDB setup and define a root password.

Console
$ sudo mysql_secure_installation

When prompted for the root password, press Enter. Confirm with y to set a new root password, remove anonymous accounts, disallow root login remotely, and delete the test database.

Access the MySQL shell as the root user.

Console
$ sudo mysql -u root -p

Create a test database within the console.

Code
CREATE DATABASE sampledb;

Generate a database user and assign a secure password.

Code
CREATE USER 'example-user'@'localhost' IDENTIFIED BY 'ultra-strong-password';

Grant complete access rights to the sample database for the new user.

Code
GRANT ALL PRIVILEGES ON sampledb.* TO 'example-user'@'localhost';

Reload the privileges to apply the changes.

Code
FLUSH PRIVILEGES;

Exit the MySQL console.

Code
EXIT

Now, test logging in as the newly created database user.

Code
mysql -u example-user -p

List all available databases.

Code
SHOW DATABASES;

Exit the console once more.

Code
EXIT

Step 5: Configure Nginx

Create a dedicated directory to store your web application files.

Console
$ sudo mkdir /usr/share/nginx/example.com

Use a text editor to create a basic HTML file inside this directory.

Console
$ sudo nano /usr/share/nginx/example.com/index.html

Insert the following content into the HTML file:

Hello World!! Your WebServer Works!

Code

Save your changes and close the editor.

Assign ownership of the new directory to the Nginx user and group.

Console
$ sudo chown -R nginx.nginx /usr/share/nginx/example.com

Create a new Nginx configuration file for your domain.

Console
$ sudo nano /etc/nginx/conf.d/example.com.conf

Paste the following configuration settings into the file:

Code
server {
    listen 80;
    listen [::]:80;
    server_name _;
    root /usr/share/nginx/example.com;
    index index.php index.html index.htm;
    error_log /var/log/nginx/example.com.error;
    access_log /var/log/nginx/example.com.access;
    
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    
    location ~ /\. {
        deny all;
        access_log off;
    }
}

Save and exit the configuration file.

Verify the Nginx configuration for syntax errors.

Console
$ sudo nginx -t

Finally, restart Nginx to apply the new settings.

Console
$ sudo systemctl restart nginx

Step 6: Enhance Server Security

Nginx uses HTTP and HTTPS ports to deliver web applications. Allow these ports through the firewall.

Permit HTTP traffic through the firewall.

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

Permit HTTPS traffic as well.

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

Reload the firewall to apply the modifications.

Console
$ sudo firewall-cmd --reload

Step 7: Configure SSL Certificates

Install the Extra Packages for Enterprise Linux (EPEL) repository to prepare for SSL setup.

Console
$ sudo dnf install epel-release

Activate the PowerTools repository.

Console
$ sudo dnf config-manager --set-enabled powertools

Install the Snap package manager.

Console
$ sudo dnf install snapd -y

Enable Snap's socket service and create a symbolic link to enable classic Snap support.

Console
$ sudo systemctl enable --now snapd.socket && sudo ln -s /var/lib/snapd/snap /snap

Log out of your SSH session and reconnect to complete Snap integration.

Next, install the Certbot utility to handle SSL certificates.

Console
$ sudo snap install --classic certbot

Request a free SSL certificate for your domain using Certbot.

Console
$ sudo certbot --nginx -d example.com

Make sure to substitute example.com with your actual domain name.

Restart the Nginx service to activate the SSL configuration.

Console
$ sudo systemctl restart nginx

Step 8: Testing the Setup

Use a web browser to visit your configured domain and verify that the website loads properly.

https://example.com

Your basic HTML application should display a "Hello World" message.

To verify database connectivity, create a new file named dbtest.php in your web server’s root directory.

Console
$ sudo nano /usr/share/nginx/example.com/dbtest.php

Add the following PHP code into the file, inserting the correct database details you set earlier:

PHP
<?php
$server = "localhost";
$user = "example-user";
$password = "ultra-strong-password";
$connect = new mysqli($server, $user, $password);

if ($connect->connect_error) {
    die("<h2>Connection failed: </h2>" . $connect->connect_error);
}

echo "<h2>Database Connected successfully</h2>";
echo "<h2><br>Below is your Server PHP Information</h2><br>";
phpinfo();
?>

Save your changes and close the editor.

Now, navigate to the following address in your browser to test:

https://example.com/dbtest.php

If everything is configured correctly, a success message will appear along with detailed PHP server information.

Conclusion

Congratulations! You have successfully deployed the LEMP stack on your Rocky Linux 8 server and confirmed communication between all major components. From here, you can proceed with deploying secure and functional web applications on your server.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren