How to Install Nginx, MySQL/MariaDB and PHP on RHEL 8

Many of TecMint readers know about LAMP, but less people are aware of LEMP stack, which replaces Apache web server with the light weight Nginx. Each web server has their pros and cons and it depends on your specific situation which one you would choose to use.

In this tutorial, we are going to show you how to install LEMP stack – Linux, Nginx, MySQL/MariaDB, PHP on RHEL 8 system.

Note: This tutorial presumes that you have an active RHEL 8 subscription and that you have root access to your RHEL system.

Step 1: Install Nginx Web Server

1. First, we will start by installing the Nginx web server using the following command, which will install nginx with all the required dependencies.

# yum install nginx
Install Nginx on RHEL 8
Install Nginx on RHEL 8

2. Once the installation is complete, enable Nginx (to start automatically upon system boot), start the web server and verify the status using the commands below.

# systemctl enable nginx
# systemctl start nginx
# systemctl status nginx
Enable and Start Nginx on RHEL 8
Enable and Start Nginx on RHEL 8

3. To make our pages available to public, we will have to edit our firewall rules to allow HTTP requests on our web server using the following commands.

# firewall-cmd --permanent --zone=public --add-service=http 
# firewall-cmd --permanent --zone=public --add-service=https
# firewall-cmd --reload
Open Nginx Access on Firewalld
Open Nginx Access on Firewalld

4. Verify that the web server is running and accessible by accessing either http://localhost or your server’s IP address. You should see a page similar to the one below.

Verify Nginx on RHEL 8
Verify Nginx on RHEL 8

The directory root for nginx is /usr/share/nginx/html, so we will be placing our web accessible files in there.

Step 2: Install PHP Programing Language

5. Next we will install PHP – widely used language for web development. It is used on platforms such as WordPress, Joomla, Magento with which you can build all kind of websites.

To install PHP, use the following command.

# yum install php php-mysqlnd php-pdo php-gd php-mbstring
Install PHP on RHEL 8
Install PHP on RHEL 8

6. Now restart your web server so that Nginx knows that it will be serving PHP requests as well.

# systemctl restart nginx

7. Now let’s test a PHP by creating a simple info.php file with a phinfo() in it to review our PHP configuration.

# echo "<?php phpinfo() ?>" > /usr/share/nginx/html/info.php

8. Now access http://localhost/info.php or http://server-ip-address/info.php to verify that PHP is working. You should see page like this:

Check PHP Info on RHEL 8
Check PHP Info on RHEL 8

Step 3: Install MariaDB Server

9. If you want to use databases for your projects, you can use MariaDB which is one of the most popular database servers in the world. It’s installation is fairly easy and can be completed with the following command:

# yum install mariadb-server mariadb
Install MariaDB on RHEL 8
Install MariaDB on RHEL 8

10. Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the web server and verify the status using the commands below.

# systemctl enable mariadb
# systemctl start mariadb
# systemctl status mariadb
Enable and Start MariaDB on RHEL 8
Enable and Start MariaDB on RHEL 8

11. Finally, you can secure your MariaDB installation by issuing the following command.

# mysql_secure_installation

You will be asked few different questions such as to change the default password for root user, remove anonymous user, disallow remote root user login and remove the test database. Finally reload the privileges tables.

Here is a sample of this process:

Secure MariaDB Installation on RHEL 8
Secure MariaDB Installation on RHEL 8

12. To test your MySQL connection, you can see the available databases with the following command.

# mysql -e "SHOW DATABASES;" -p
Check MySQL Databases in RHEL 8
Check MySQL Databases in RHEL 8
Conclusion

The installation of the LEMP stack is an easy process completed in few steps. You can add extra configuration to your Nginx, PHP and MariaDB to improve functionality and performance, however these are tasks beyond the scope of this article. Hope the process was easy for you.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.