RHEL BasedRocky Linux

How To Install LEMP Stack on Rocky Linux 9

Install LEMP Stack on Rocky Linux 9

In this tutorial, we will show you how to install LEMP Stack on Rocky Linux 9. For those of you who didn’t know, LEMP is an open-source web application stack used to develop web applications. Nginx works as the web server application, MariaDB as the database server, and PHP as the dynamic scripting language that supports multiple frameworks for web applications.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the LEMP Stack on Rocky Linux. 9.


  • A server running one of the following operating systems: Rocky Linux 9.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install LEMP Stack on Rocky Linux 9

Step 1. The first step is to update your system to the latest version of the package list. To do so, run the following commands:

sudo dnf check-update
sudo dnf install dnf-utils

Step 2. Installing Nginx Web Server on Rocky Linux 9.

By default, Nginx is not available on Rocky Linux 9 base repository. Now run the following command below to add Nginx stable repository to your system:

sudo tee /etc/yum.repos.d/nginx-stable.repo<<EOF
name=nginx stable repo

Now, run the following command to install the latest stable version of Nginx to your server:

sudo dnf update
sudo dnf install nginx

Once the installation is done, start the Nginx service and enable it to automatically start on reboot all in one go with:

sudo systemctl enable --now nginx

To verify that the latest version of Nginx has been installed, run:

nginx -v

By default, Nginx listens on ports 80 and 443. If any firewall is installed and configured on your server, then you will need to allow both ports via firewalld. You can allow them with the following command:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

You can verify by listing the current firewall settings:

sudo firewall-cmd --permanent --list-all

Now, open your web browser and access the Nginx default page using the URL http://your-server-ip-address. You should see the Nginx default page on Rocky Linux. :

Install LEMP Stack on Rocky Linux 9

You can also test that your web page is working from the command line with:

curl -I http://your-ip-address

For additional resources on installing Nginx, read the post below:

Step 3. Installing MariaDB.

By default, MariaDB is available on the Rocky Linux 9 base repository. Simply install the MariaDB package by using the dnf command:

sudo dnf install mariadb-server mariadb

After the installation is completed, start the service of the Database server and then enable the same, so that it could start itself automatically with the system reboot:

sudo systemctl restart mariadb
sudo systemctl status mariadb
sudo systemctl enable mariadb

By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB:


First, log into the MariaDB shell with the following command:


For additional resources on installing MariaDB Database, read the post below:

Step 4. Installing PHP.

PHP is a popular scripting language that powers the dynamic content of millions of websites and apps. Now we run the commands below to install PHP:

sudo dnf epel-release
sudo dnf module enable php:remi-8.1

Once Remi PHP 8.1 module is enabled, you can now install PHP 8.1 and commonly used PHP extensions as follows:

sudo dnf install php php-cli php-fpm php-gd php-curl php-zip php-mbstring php-opcache php-intl php-mysqlnd

After the installation is complete, we need to make one more adjustment to the PHP-FPM service. Let’s now check how to configure Nginx to execute PHP using PHP-FPM:

sudo nano /etc/php-fpm.d/www.conf

Find the lines below for the user and group and change them to Nginx:

user = nginx
group = nginx

Save and close the file, then reload the PHP-FPM service:

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

Check and verify the installed version:

php -v


[root@idroot.us ~]# php -v
PHP 8.1.8 (cli) (built: Nov 10 2022 18:36:33) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.16, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.15 Copyright (c), by Zend Technologies

Then create a PHP test page to confirm it’s working with the Nginx server:

sudo nano /usr/share/nginx/html/info.php

Add the following file:

phpinfo ();

Save and close the file, then restart your web server so that Nginx knows that it will be serving PHP requests as well:

sudo systemctl restart nginx php-fpm

Once is done, now view the test page on your browser by entering your server IP or domain name followed by info.php e.g. localhost/info.php or your-IP-address/info.php

For additional resources on installing PHP, read the post below:

Congratulations! You have successfully installed LEMP. Thanks for using this tutorial for installing the LEMP Stack on your Rocky Linux 9 system. For additional help or useful information, we recommend you check the official LEMP website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!


r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button