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
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
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
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.
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
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:
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
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
11. Finally, you can secure your MariaDB installation by issuing the following command.
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:
12. To test your MySQL connection, you can see the available databases with the following command.
# mysql -e "SHOW DATABASES;" -p
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.