How To Install LEMP Stack on Rocky Linux

LEMP (Linux operating system, Nginx pronounced as Enginx a web server, MariaDB or MySQL a popular database and PHP programming language) is one of the most popular stacks that make a perfect environment to deploy web applications, whether it for production or development.

NOTE: Before installing, make sure to run the update command and install the packages.

$ sudo dnf update

1. Install Nginx On Rocky Linux

After installation of updates, run this command to install Nginx and all its dependencies.

$ sudo dnf install nginx

Now to run the following command to start the nginx on boot and to check the status of the nginx.

$ sudo systemctl enable nginx
$ sudo systemctl start nginx
$ sudo systemctl status nginx

Now you can check the nginx version as well by running this command.

$ nginx -v
output: nginx version: nginx/1.14.1

Now you should be able to see the following page after browsing IP adddress or domain in the url bar.

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

$ sudo dnf install firewalld
$ sudo systemctl unmask firewalld
$ sudo systemctl enable firewald
$ sudo systemctl start firewalld

Allow port by running the following command for 80 or http, 443 or https.

$ sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
$ sudo firewall-cmd --permanent --zone=public --add-port=http/tcp

$ sudo firewall-cmd --permanent --zone=public --add-port=443/tcp
$ sudo firewall-cmd --permanent --zone=public --add-port=https/tcp

2. Install MariaDB on Rocky Linux

We will install MariaDB. It is much more improved than MySQL. Use this following command.

$ sudo dnf install mariadb-server mariadb

Once installed, enable and start the MariaDB database and you can also check the status using status command.

$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb
$ sudo systemctl status mariadb

Check MariaDB running status.

The default settings for MariaDB are not secure enough and your database can easily be breached. run the below command to secure your MariaDB and follow the instructions.

$ sudo mysql_secure_installation

Now run this command to login into the MariaDB server.

sudo mysql -u root -p

3. Install PHP on Rocky Linux

We will install PHP via PHP-FPM (FastCGI Process Manager). PHP-FPM loads some extra features such as advanced process management, accelerated upload support, optimized security features, and many more.

First, we need to install the Remi repository which is a third-party free repository that provides the latest PHP versions.

$ sudo dnf install dnf-utils

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

$ sudo dnf module list php

PHP 7.2 is the default version and we can identify by the tag [d]. But we’re going to install PHP 8.1 version. 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

Now we need to 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

Once the installation is complete, enable, start PHP-FPM and you can also check the status as well.

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

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

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

Set “user = apache” to “user = nginx” and “group = apache” to “group = nginx” and finally, reload the PHP-FPM

user = nginx
group = nginx
$ sudo systemctl reload php-fpm

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

$ php -v

4. Configure Nginx Server Block in Rocky Linux

Server block allows you to host multiple websites on a single server. You need to define different site document root directories.

Every directory contains website files. We will create an Nginx server block to host a sample website.

Step 1: Create a Website Directory

First, we will create a website directory under a qualified domain name (let’s say here) where all the website files will be stored and change with your valid domain name.

$ sudo mkdir -p /var/www/
Step 2: Set Ownership & Permissions on Website

We have created a public_html folder under where website files can be stored and the current owner is root. Set ownership to the regular user as.

$ sudo chown -R $USER:$USER /var/www/

The $USER variable takes the value of the currently logged-in user and grants the user ownership to the html files and subdirectories. Additionally, grant read permission to the general web root directory so that site visitors can access the site pages.

$ sudo chmod -R 755 /var/www
Step 3: Create a Demo Website

Create a simple HTML page index.html under the public_html folder by using the following command.

$ vi /var/www/

And paste the below HTML content. After that save and exit.

    <title>This is a Demo HTML page!</title>
    <h1>Hurray! Your Nginx Server Block is Ready!</h1>
Step 4: Create a Nginx Server Block

A server block is a configuration file of your website. You can configure more than one website setting in a single server block or can create a server block for each website. First, create these two files.

  • /etc/nginx/sites-available – This directory contains the server block file.
  • /etc/nginx/sites-enabled – The directory notifies Nginx that the server block file is ready to serve requests.
$ sudo mkdir /etc/nginx/sites-available
$ sudo mkdir /etc/nginx/sites-enabled

Now, edit Nginx main configuration file.

$ vi /etc/nginx/nginx.conf

Paste the following line in nginx.conf file, save and exit.

include /etc/nginx/sites-enabled/*.conf;

Thereafter, create a server block file.

$ sudo vi /etc/nginx/sites-available/

Now paste the below content, and do not forget to change “” with your valid domain name.

server {
    listen  80;


    location / {
        root  /var/www/;
        index  index.html index.htm;
        try_files $uri $uri/ =404;

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;

Save and exit.

Step 5: Enable Nginx Server Block

Finally, enable server block. we’ll create a symbolic link for the server block file to the sites-enabled directory.

$ sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Now, restart the Nginx server to take effect of all the changes.

$ sudo systemctl restart nginx
Step 6: Test Your Website

Simply, visit your website in a browser.

After visiting the website, you should be able to see the following below message that is being configured in step 3 if you remembered.

I hope the given information is useful and helped you guys and if did, please let us know your thoughts or any suggestions in the comment section below.

Thank you!

You Might Also Like

More Similar Posts


Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x