LEMP, an acronym for Linux Nginx ( Pronounced and Engine-X) MySQL and PHP is a popular web development stack used by developers to test and launch websites. LEMP is a close cousin to the LAMP server which ships with Apache webserver instead of Nginx. In this tutorial, we demonstrate how to install LEMP server on CentOS 8.
LEMP combines a hybrid system of tools that include :
- Nginx(pronounced Engine X) for the webserver
- MariaDB/MySQL to act as the database management server.
- PHP to serve as the server-side script language.
Let’s now get down to installing the LEMP server.
Before getting started out, ensure that you have an instance of CentOS 8 at your avail with an SSH connection. If you don’t have one ready, you can readily deploy a fully managed cloud VPS starting at $3.71 per month
Step 1: Update the system
To get started, begin by updating the system using the command:
$ sudo dnf update
Step 2: Install Nginx
Next, we will install the second component of the LEMP stack which is the Nginx web server.
$ sudo dnf install nginx –y
Upon successful installation, start the webserver using the command:
$ sudo systemctl start nginx
Additionally, you can configure the webserver to start on boot using the command.
$ sudo systemctl enable nginx
To confirm the Nginx version by running this command
$ nginx –v
You can verify the running status of the server by invoking:
$ sudo systemctl status nginx
The image above confirms that the Nginx webserver is up and running. You can also verify that Nginx is installed by browsing your Server’s IP address as shown. This displays the default Nginx welcome page.
Step 3: Install the MariaDB database server
MariaDB is a free and open-source database server that is a fork of MySQL. It became quite popular, especially after MySQL was acquired by Oracle. It ships with numerous features and optimizations such as better security and stability, additional storage engines, and overall better performance.
To install MariaDB on CentOS 8, run the command.
$ sudo dnf install mariadb-server mariadb
Once the installation is complete, start the database server as shown.
$ systemctl start mariadb
For best use, set MariaDB to start on boot using the command:
$ systemctl enable mariadb
Just to be sure that the server is running, execute:
$ systemctl status mariadb
The output indicates the MariaDB server is up and running. Now, the default settings in MariaDB are quite weak and extra steps are required to harden the server. Therefore, run the script below to make additional tweaks to further secure your server.
Be sure to set the root password.
For the remaining prompts, simply type in ‘Y’ for Yes and hit ENTER.
To log in, to MariaDB, simply run:
$ sudo mysql -u root -p
Step 4: Install PHP-FPM
To install the t PHP-FPM plus its related modules on CentOS 8, run the following command:
$ sudo dnf install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y
To start it after the installation, execute:
$ sudo systemctl start php-fpm
Next, configure the PHP-FPM to start on boot using the command:
$ sudo systemctl enable php-fpm
Next, proceed to edit the config file using this command
Usually, PHP-FPM is set to run as an Apache user. However, we have installed Nginx and we need to configure the user to Nginx. Therefore, proceed and open the /etc/php-fpm.d/www.conf. file:
$ sudo vim /etc/php-fpm.d/www.conf
Scroll and locate the following lines:
user = apache group = apache
Now change both values to Nginx.
user = nginx group = nginx
Save and exit the configuration file. Then restart Nginx and php-fpm.
$ sudo systemctl restart nginx $ sudo systemctl restart php-fpm
Step 4: Test PHP
Here, you will test the PHP-FPM module configurations with the Nginx web server by editing the root directory. Create a sample PHP file as shown:
$ sudo vim /usr/share/nginx/html/info.php
Paste the following PHP code inside
<?php phpinfo(); ?>
Save and close
To see if it works, fire up your browser and browse the server’s address as shown
You should get the webpage below showing the PHP version alongside the server status and PHP modules.
If you don’t get to see the webpage, be sure to restart both PHP-FPM and Nginx and repeat the process.
$ sudo systemctl restart nginx php-fpm
If you have come this far, its our hope that you are now in a position to install LEMP server on CentOS 8.