LEMP is the brief of Linux, Nginx (Engine X), MariaDB/MySQL, and PHP. In this article, we will check how to install LEMP on CentOS 8. Lemp includes a set of free and open-source tools for using high-traffic dynamic websites. Nginx is a free, powerful, high-performance web server that can also be used as a reverse-proxy.
MariaDB is a database used to store user information, and PHP is a server-side programming language used to develop and support dynamic web pages.
There are 5 steps to install LEMP:
- Update software packages
- Installation of Nginx webserver
- Install MariaDB
- Install PHP 7
- PHP data testing
Step 1: Update software packages
To get started, update your repository and software packages on CentOS 8 using the dnf command.
$ sudo dnf update
Step 2: Install Nginx web server on
After completing the software package update, install the Nginx using the following command.
$ sudo dnf install nginx
The photo below shows that the Nginx installation was done without any problems.
Once the installation is complete, run the following commands to automatically boot the X engine during system boot, as well as confirm its functionality.
$ sudo systemctl enable nginx $ sudo systemctl start nginx $ sudo systemctl status nginx
To check the Nginx version installed, run the following command.
$ nginx -v
If you are curious to learn more about Nginx, run the rpm command as below.
$ rpm -qi nginx
Now add the Apache service to your operating system firewall for access. To do this, use the following command, then reload the firewalld service.
# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --reload
To make sure the Nginx server is up and running, enter your server’s IP address in a browser and press ENTER.
If you see the Welcome to Nginx page, your server is up and you can make the rest of the settings.
Step 3: Installation of MariaDB on CentOS 8
MariaDB is free and open-source, a branch of MySQL, and has the latest features that make it a better alternative to MySQL. To install MariaDB, run the following command.
$ sudo dnf install mariadb-server mariadb
To activate the MariaDB service in the Boot system, run the following command.
$ sudo systemctl enable mariadb
Run the following command to start MariaDB server.
$ sudo systemctl start mariadb
Then use the following command to check the status of MariaDB service, which should be in the active (running) mode.
$ sudo systemctl status mariadb
The MariaDB database engine is not secure and anyone can login to it without authentication. To secure MariaDB to reduce the chances of unauthorized access, run the following command.
$ sudo mysql_secure_installation
After executing the command, a series of questions are asked. The first question asks you to define a root password. Press Enter and select Y to select yes to specify the root password. After defining the root password, answer the remaining questions in order to delete the anonymous user, delete the test database, and disable root remote access.
As soon as you have completed all the above steps, you can log in to MariaDB server and check the MariaDB server version information. (To login to MariaDB, enter the password you created in the database security step)
$ mysql -u root -p
Step4: Installation of PHP 7
Finally, we install the last component of the LEMP server, which is PHP, which is actually a web programming language that is very commonly used for programming dynamic web pages.
Currently, the latest version is PHP 7.4, which we plan to install using Remi repository. Remi is a free repository that contains the latest versions of advanced software that are not installed by default on CentOS.
First, run the following command to install the EPEL monitor.
$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Then continue and install yum-utils and activate the remi repository using the following command.
$ sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Then, search for available PHP modules available for installation.
$ sudo dnf module list php
As you can see below, it shows the output of the command, modules, stream and PHP installation profiles. From the output below we can detect the existing PHP version written with a letter d inside the bracket close to the PHP version (PHP 7.2).
From the output, we can also see that the last PHP module is actually PHP 7.4, which we are going to install, but first we need to reset the PHP modules. As a result, we execute the following command.
$ sudo dnf module reset php
Then, by running the following command, we activate the PHP 7.4 module.
$ sudo dnf module enable php:remi-7.4
Finally, by activating the PHP 7.4 module, we install the PHP and PHP-FPM packages (FastCGI Process Manager) and the corresponding PHP modules using the following command.
$ sudo dnf install php php-opcache php-gd php-curl php-mysqlnd
Now, check the installed version.
$ php -v
Then launch php-fpm and enable it to run during system boot
$ sudo systemctl enable php-fpm $ sudo systemctl start php-fpm
Run the following command to check its status.
$ sudo systemctl status php-fpm
Another case in point is that PHP-FPM is configured by default to run under the Apache user. But since we run it on the Nginx web server, we need to change it to the Nginx user.
As a result, open the /etc/php-fpm.d/www.conf file.
$ vi /etc/php-fpm.d/www.conf
Then find the following two lines in it.
user = apache group = apache
Now change their value to Nginx.
user = nginx group = nginx
Save and close the configuration file.
Then restart Nginx and PHP-FPM services to apply the changes.
$ sudo systemctl restart nginx $ sudo systemctl restart php-fpm
Step 5: PHP data testing
By default, the Nginx engine root folder is in / usr / share / nginx / html /. To test PHP-FPM, we can create a PHP file called info.php and paste the following lines into it.
<?php phpinfo(); ?>
Save the file and exit.
Open your browser and enter your web server in the ip address bar as shown below.
If you did everything right, you should see the running PHP version information and other parameters.
We have now successfully installed LEMP Server on CentOS 8 Linux VPS. It is recommended that you remove the info.php file for security reasons and to prevent hackers from accessing your Nginx server information.