How To Install LEMP Stack on Rocky Linux 9

In this guide, we want to teach you How To Install LEMP Stack on Rocky Linux 9.

LEMP stands for Linux, Nginx, MariaDB or MySQL, and PHP.

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 individual 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 individual part separately while still being able to customize them in exactly what way you want them

Steps To Install LEMP Stack on Rocky Linux 9

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 this guide.

Install Nginx on Rocky Linux 9

To install Nginx you need to update and upgrade DNF packages with the following command:

sudo dnf update && sudo dnf upgrade

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 the 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
Nginx default page

If you see this page, 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.

Install MariaDB on Rocky Linux 9

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

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

Set up PHP on Rocky Linux 9

The default PHP version available in the Rocky Linux 9 repositories is PHP 8.0.

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 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:

php info rocky linux 9

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.

Conclusion

At this point, you have learned to Install LEMP Stack on Rocky Linux 9.

Hope you enjoy it.

You may be interested in these articles:

Install LEMP Stack on AlmaLinux 9

How To Install LEMP Stack on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!