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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Marin Todorov
I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.