LEMP is a popular web hosting stack used by developers and web hosting companies to test and host web applications. It comprises 3 components: the Nginx ( pronounced as Engine-X) webserver, MariaDB, and PHP which is a server-side scripting language.
In this walkthrough, you will learn how to install the LEMP stack on AlmaLinux and Rocky Linux 8.4.
Before we get underway, confirm that you have an instance of AlmaLinux or Rocky Linux 8 with SSH access. Additionally, be sure that you have a sudo user set up for this guide.
Step 1: Install EPEL
EPEL, an acronym for Extra Packages for Enterprise Linux is a fre repository from the Fedora project which provides RHEL-based distros with a set of high-quality packages.
To install EPEL, first, update the system packages.
$ sudo dnf update
Then install it as shown
$ sudo dnf install epel-release
With EPEL installed, now we can focus on installing the core packages of the LEMP stack, beginning with Nginx.
Step 2: Install the Nginx webserver
Nginx is an open-source and high-performance web server that can also serve as a reverse proxy depending on how it is configured. It’s stable and is mostly used for hosting high-traffic sites such as e-commerce platforms, and news sites to name a few.
To install Nginx, simply run the command:
$ sudo dnf install nginx
Once the installation is complete, start the Nginx webserver
$ sudo systemctl start nginx
Once the Nginx service is started, you might want to confirm its running status as provided.
$ sudo systemctl status nginx
Just to confirm the version of Nginx installed, you can do this using the command:
$ nginx -v
You can also confirm that the Nginx web server is running by browsing the URL shown
You will get the Nginx welcome page displayed.
Step 2: Install MariaDB database server
MariaDB is an open-source relational database management system (RDMS) which is a fork of MySQL which was acquired by Oracle. It’s a fast and stable database server with a myriad of features that enhance its performance including storage engines such as InnoDB and clustering technology such as Galera Cluster.
To install the MariaDB database server, execute:
$ sudo dnf install mariadb-server mariadb
Next, start and enable the database server to start automatically once AlamLinux is booted.
$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb
Thereafter, confirm that the relational database system is running:
$ sudo systemctl status mariadb
The default settings for MariaDB are weak and pose security challenges. In light of this, we need to go an extra step and secure or harden the database server. To achieve this, run the script shown.
$ sudo mysql_secure_installation
This takes you through a series of prompts to secure it. Start by setting a strong root password.
For the remaining prompts, simply answer ‘Y’ to configure the database server to the recommended settings.
Once the hardening procedure is complete, you can log in as shown.
$ sudo mysql -u root -p
From this point, you can view the existing databases and tables and run SQL queries.
Step 3: Install PHP and additional PHP modules
PHP is the last component that we are going to install. As mentioned previously, PHP is a popular server-side scripting language used to develop dynamic web applications. By default, the AppStream repository provides PHP 7.2. You can view the versions using the command:
$ sudo dnf module list php
We are going to install the latest PHP version, and to achieve this, we need to add the Remi repository which provides the latest PHP versions.
$ sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Next, give your AlmaLinux a system update to sync with the newly added repository.
$ sudo dnf update
This time around, when you view the available repositories, you will see the PHP versions provided by the Remi repository. At the very bottom, we can see that the latest version of PHP is PHP 8.0
So, reset the default PHP modules
$ sudo dnf module reset php
And enable the latest PHP 8.0 module provided by Remi.
$ sudo dnf module enable php:remi-8.0
Then install PHP and PHP-FPM alongside other extensions as provided. PHP-FPM(FastCGI Process Manager) is a web tool for enhancing the webserver by handling multiple loads exceptionally well compared to traditional CGI methods.
$ sudo dnf install php php-fpm php-curl php-cli php-gd
When the installation is complete, be sure to start and enable the PHP-FPM service using the following commands in succession.
$ sudo systemctl start php-fpm
$ sudo systemctl enable php-fpm
Usually, PHP-FPM runs as the apache user. Since we are running Nginx, we need to make a few changes
$ sudo vim /etc/php-fpm.d/www.conf
Locate the lines shown and set the values to nginx as provided.
user = nginx
group = nginx
To apply the changes, restart Nginx
$ sudo systemctl restart nginx
As well as the PHP-FPM daemon.
$ sudo systemctl restart php-fpm
Perfect! Now let’s proceed and Test if the PHP installation on our system was successful.
Step 4: Test PHP installation
To test the PHP installation, create a PHP file in the /usr/share/nginx/html/ path.
$ sudo vim /usr/share/nginx/html/info.php
Insert the following PHP lines and save the file.
<?php phpinfo(); ?>
Finally, head over to your web browser and visit the URL:
This should direct you to this webpage.
Now that we have verified PHP was successfully installed, we can safely remove the test PHP file.
$ sudo rm -f /usr/share/nginx/html/info.php
Wonderful! We have managed to successfully install the LEMP stack on AlmaLinux 8.4. All the best in developing and testing applications.