How To Install LEMP Stack on Rocky Linux 9 | Fast Hosting
This guide on the Orcacore website will teach you How To Install LEMP Stack on Rocky Linux 9. LEMP stands for Linux, Nginx, MariaDB or MySQL, and PHP. Understanding How To Install LEMP Stack on Rocky Linux 9 is crucial for modern web development.
The advantage of using a LEMP stack instead of just one technology like Apache or Nginx is that it gives you more control over each technology and how they interact with each other.
For example, if you want to change how PHP works with your database then there will be changes made to both PHP and MySQL as well as Nginx – this makes it easier to manage since you don’t have to tune each part separately while still being able to customize them in exactly what way you want them.
To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 9.
Now, follow the steps below to complete LEMP Stack Setup on Rocky Linux 9.
1. LEMP Stack Setup: Install Nginx
To install Nginx, you need to update and upgrade DNF packages with the following command:
sudo dnf update -y && sudo dnf upgrade -y
Then, install Nginx with the following command:
sudo dnf install nginx -y
After your installation is completed, run the command below to enable and start Nginx on Rocky Linux 9:
sudo systemctl start nginx
At this point, we assumed that you are done with the requirements for setting up a basic firewall.
Now you need to allow connections to Nginx on Rocky Linux 9. Enable HTTP connections, which run on port 80 with the following command:
sudo firewall-cmd --permanent --add-service=http
Here you need to reload the firewall configuration by using the following command:
sudo firewall-cmd --reload
Now you can test that your Nginx web server is up and running. To do this, you need your server’s public IP address.
You can get your public IP address with the following command:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'
Or, you can use the curl tool to get your IP:
curl -4 icanhazip.com
Then, type your IP address in your web browser to access Nginx’s default landing page.
http://your_server_IP_address

If you see this page, it means that your service is installed correctly. At this point, the installation of Nginx of the LEMP stack on Rocky Linux 9 is finished.
Let’s start to install MariaDB.
2. LEMP Stack Setup: Install MariaDB
Here you need to install a database system to be able to store and manage data for your site. Install MariaDB on Rocky Linux 9 with the following command:
sudo dnf install mariadb-server -y
When the installation of MariaDB is finished, enable and start the service with the following command:
# sudo systemctl start mariadb
# sudo systemctl enable mariadb
At this step, it’s recommended to run a security script that comes pre-installed with MariaDB to improve the security of your database server. Run the following command:
sudo mysql_secure_installation
You will be asked some questions. The first prompt will ask you to enter the current database root password. Because you just installed MariaDB and haven’t made any configuration changes yet, just press Enter to leave it blank.
The next question is to set up a new root password.
From here, you can press ‘Y’ and press Enter to accept the defaults for all the subsequent questions.
Connect to the database instance using the credentials for the root user set in the previous step.
sudo mysql -u root -p
3. LEMP Stack Setup: Install PHP
The default PHP version available in the Rocky Linux 9 repositories is PHP 8.0. At the current time, the latest version of PHP is PHP 8.1.
To install the latest PHP on Rocky Linux 9, run the command below to enable the Remi repository first:
Enable Remi repository on Rocky Linux 9
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
Then, get the available PHP modules on Rocky Linux 9 with the command below:
sudo dnf module list php
In your output, you will see:
**Output**
Rocky Linux 9 - AppStream
Name Stream Profiles Summary
php 7.2 common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 [d] common [d], devel, minimal PHP scripting language
Remi's Modular repository for Enterprise Linux 9 - x86_64
Name Stream Profiles Summary
php remi-7.2 common [d], devel, minimal PHP scripting language
php remi-7.3 common [d], devel, minimal PHP scripting language
php remi-7.4 common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
php remi-8.1 common [d], devel, minimal PHP scripting language
The default module is PHP 7.4. Reset the default module with the command below:
sudo dnf module reset php
Then, enable the latest PHP module by using the following command:
sudo dnf module enable php:remi-8.1
Now you can install the latest PHP and its dependencies on Rocky Linux 9 with the command below:
sudo dnf -y install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring php-zip
When your installation is completed, verify your PHP installation by checking its version:
php -v
In your output, you will see:
**Output**
PHP 8.1.8 (cli) (built: Dec 15 2021 02:00:45) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies
Configure PHP-FPM on Rocky Linux 9
Then, you need to configure PHP-FPM to change the user from Apache to Nginx. Open the /etc/php-fpm.d/www.conf configuration file with your favorite text editor. Here we use the Vi text editor:
sudo vi /etc/php-fpm.d/www.conf
Find the user and group lines and change their value to Nginx:
…
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
…
When you are finished, save and close the file with “:wq
”. Enable and start the php-fpm
service with the following command:
sudo systemctl start php-fpm
Now, set ownership of the HTML directory with the $USER
environmental variable by the following command:
sudo chown -R $USER:$USER /usr/share/nginx/html/
You can create a test PHP page to make sure that your web server is working correctly. To do this, you need to create a new PHP file named info.php
at /usr/share/nginx/html/
directory.
vi /usr/share/nginx/html/info.php
Add the following PHP code to the file:
<?php
phpinfo();
When you are done, save and close the file. You can type your server name or IP address in your web browser, followed by /info.php:
http://server_host_or_IP/info.php
You should see a page similar to this:
After you have checked your PHP test page, it’s better to remove it. You can use the following command:
rm /usr/share/nginx/html/info.php
Here, the final component of the LEMP stack on Rocky Linux 9 is finished. You now understand How To Install LEMP Stack on Rocky Linux 9.
Conclusion
At this point, you have learned to install LEMP Stack on Rocky Linux 9. LEMP stack on Rocky Linux 9 is used for hosting high-performance websites and web applications. It includes Linux, Nginx, MySQL/MariaDB, and PHP, offering speed, security, and scalability for modern web development.
Hope you enjoy it. Please subscribe to us on Facebook, Instagram, and YouTube.
You may also be interested in these articles:
Install LEMP Stack on AlmaLinux 9
How To Install LEMP Stack on Ubuntu 22.04
LEMP Stack Setup with Docker Compose on Debian / Ubuntu
Install LEMP Stack Nginx MariaDB PHP on Debian 12
Alternative Solutions for LEMP Stack Installation
While the above guide provides a straightforward method for installing a LEMP stack on Rocky Linux 9, alternative approaches can offer increased flexibility, automation, and ease of management. Here are two such alternatives:
1. Using Docker Compose
Docker Compose allows you to define and manage multi-container Docker applications. This approach encapsulates each component of the LEMP stack (Nginx, MariaDB, PHP-FPM) within its own container, facilitating portability, scalability, and isolation. This is an alternative way of thinking about How To Install LEMP Stack on Rocky Linux 9.
Explanation:
Docker Compose uses a YAML file to define the services, networks, and volumes that comprise your application. Each service represents a container, and you can specify the image, ports, environment variables, and dependencies for each container. This is particularly useful when needing different versions of PHP, MySQL or Nginx for different web applications running on the same server.
Example docker-compose.yml
file:
version: "3.8"
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./app:/var/www/html
depends_on:
- php
php:
image: php:8.1-fpm
volumes:
- ./app:/var/www/html
environment:
- MYSQL_HOST=mariadb
mariadb:
image: mariadb:latest
environment:
- MYSQL_ROOT_PASSWORD=your_root_password
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
Steps:
-
Install Docker and Docker Compose: Follow the official Docker documentation to install Docker Engine and Docker Compose on your Rocky Linux 9 system.
-
Create a
docker-compose.yml
file: Create a directory for your project and place the abovedocker-compose.yml
file within it. Adjust theMYSQL_ROOT_PASSWORD
and volume mappings to suit your needs. You’ll also need to create thenginx/conf.d
andapp
directories. -
Create an Nginx configuration file: Inside the
nginx/conf.d
directory, create a file (e.g.,default.conf
) to configure Nginx to serve your PHP application.server { listen 80; index index.php index.html; root /var/www/html; location ~ .php$ { try_files $uri =404; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } location ~ /.ht { deny all; } }
-
Create your PHP application: Place your PHP files in the
app
directory. -
Start the LEMP stack: Navigate to your project directory in the terminal and run
docker-compose up -d
. This will download the necessary images, create the containers, and start the services in detached mode.
2. Using a Configuration Management Tool (Ansible)
Ansible is an open-source automation tool that allows you to define infrastructure as code. You can use Ansible playbooks to automate the installation and configuration of the LEMP stack on Rocky Linux 9, ensuring consistency and repeatability across multiple servers. Many organizations rely on tools such as Ansible to How To Install LEMP Stack on Rocky Linux 9 for scalability and consistency purposes.
Explanation:
Ansible uses YAML files called playbooks to define tasks and configurations to be performed on remote servers. You can create a playbook to install Nginx, MariaDB, PHP, and configure them to work together. This is ideal for managing multiple servers or automating deployments.
Example Ansible Playbook (lemp.yml):
---
- hosts: all
become: true
tasks:
- name: Update package cache
apt:
update_cache: yes
when: ansible_distribution == "Debian" or ansible_distribution == "Ubuntu"
- name: Update package cache
dnf:
update_cache: yes
when: ansible_distribution == "Rocky"
- name: Install Nginx
dnf:
name: nginx
state: present
- name: Install MariaDB server
dnf:
name: mariadb-server
state: present
- name: Install PHP and required modules
dnf:
name:
- php
- php-fpm
- php-mysqlnd
- php-opcache
- php-gd
- php-xml
- php-mbstring
- php-zip
state: present
- name: Configure PHP-FPM user
replace:
path: /etc/php-fpm.d/www.conf
regexp: '^(user|group) = apache'
replace: '1 = nginx'
notify: restart php-fpm
- name: Start Nginx
systemd:
name: nginx
state: started
enabled: yes
- name: Start MariaDB
systemd:
name: mariadb
state: started
enabled: yes
- name: Start PHP-FPM
systemd:
name: php-fpm
state: started
enabled: yes
handlers:
- name: restart php-fpm
systemd:
name: php-fpm
state: restarted
Steps:
-
Install Ansible: Install Ansible on your control machine (where you’ll run the playbook). You can use
pip install ansible
. -
Configure Ansible Inventory: Create an inventory file (e.g.,
hosts
) that lists the target servers and their connection details (IP address, username, password or SSH key). -
Create the Ansible Playbook: Create the
lemp.yml
file as shown above. -
Run the Playbook: Execute the playbook using the
ansible-playbook
command:ansible-playbook -i hosts lemp.yml
These alternative methods offer advantages in terms of automation, scalability, and portability. Choosing the right approach depends on your specific needs and technical expertise. Understanding the various ways to How To Install LEMP Stack on Rocky Linux 9 can significantly improve your web development workflow.