How To Install LEMP on CentOS Stream 9

Install LEMP on CentOS Stream 9

In this tutorial, we will show you how to install LEMP on CentOS Stream 9. For those of you who didn’t know, LEMP is a software stack that consists of Linux, Nginx, MariaDB, and PHP. These components make up a stack that is capable of handling high traffic web applications. It’s a popular choice for web developers who want to build fast and scalable 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 (Nginx, MariaDB, PHP) on CentOS Stream 9.

Prerequisites

  • A server running one of the following operating systems: CentOS Stream 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).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Nginx, MariaDB, and PHP.
  • A non-root sudo useror 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 on CentOS Stream 9

Step 1. First, let’s start by ensuring your system is up-to-date.

sudo dnf update
sudo dnf install epel-release

Step 2. Installing the Nginx.

By default, Nginx is available on the CentOS Stream base repository. Now we run the following command to install Nginx to your system:

sudo dnf install nginx

Once the installation is complete, now enable Nginx (to start automatically upon system boot), start the webserver, and verify the status using the commands below:

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

To verify the Nginx version, use the following command to confirm:

nginx -v

If your server is protected by a firewall you need to open both HTTP (80) and HTTPS (443) ports:

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

Once successfully installed, open http://your-IP-address in your browser of choice, and you will see the default Nginx welcome page as shown in the image below:

Install Nginx on CentOS 9 Stream

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

Step 2. Installing the MariaDB.

By default, MariaDB is available on the CentOS Stream base repository. Now we run the following command to install MariaDB to your system:

sudo dnf install mariadb-server

Check the version once the installation is complete:

mariadb --version

Once the installation is complete, now enable MariaDB (to start automatically upon system boot), start the MariaDB, and verify the status using the commands below:

sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo systemctl status 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:

mysql_secure_installation

Configure it like this:

- Set root password? [Y/n] y
- Remove anonymous users? [Y/n] y
- Disallow root login remotely? [Y/n] y
- Remove test database and access to it? [Y/n] y
- Reload privilege tables now? [Y/n] y

To log into MariaDB, use the following command (note that it’s the same command you would use to log into a MariaDB database):

mysql -u root -p

Now we create a database, create a MariaDB user and enable remote connections to the MariaDB database:

For example, uses the following parameters:

  • DB_NAME = idroot_db
  • USER_NAME = idroot_user
  • REMOTE_IP = 10.0.46.36
  • PASSWORD = your-strong-passwd
  • PERMISSIONS = ALL
## CREATE DATABASE ##
MariaDB [(none)]> CREATE DATABASE idroot_db;

## CREATE USER ##
MariaDB [(none)]> CREATE USER 'idroot_user'@'10.0.15.25' IDENTIFIED BY 'your-strong-passwd';

## GRANT PERMISSIONS ##
MariaDB [(none)]> GRANT ALL ON idroot_db.* TO 'idroot_user'@'10.0.46.36';

##  FLUSH PRIVILEGES, Tell the server to reload the grant tables  ##
MariaDB [(none)]> FLUSH PRIVILEGES;

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

Step 3. Installing the PHP.

By default, PHP is available on the CentOS 9 Stream base repository. To install PHP on CentOS Stream, you can use the following command below:

sudo dnf install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring

Once the installation is complete, you can start the PHP-FPM service by running the following command:

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

Since we are using the Nginx web server, we need to change the following line:

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

user = apache
group = apache

Change them to:

user = nginx
group = nginx

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

Step 4. Test the Installation.

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

nano /usr/share/nginx/html/test.php

Add the following file:

<?php
phpinfo ();
?>

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

sudo systemctl restart nginx
sudo systemctl restart php-fpm

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

Congratulations! You have successfully installed LEMP. Thanks for using this tutorial to install the LEMP stack on CentOS Stream 9. 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!