How to Install LEMP(Linux, Nginx, MySQL and PHP) Stack on CentOS 8 Using 12 Best Steps

In this article, I will take you through the Steps to Install LEMP(Linux, Nginx, MySQL and PHP) Stack on CentOS 8 Using 12 Best Steps. LEMP stack is an open source framework consists of Linux, Nginx, MariaDB/MySQL and PHP. You might have heard about LAMP(Linux, Apache, MySQL and PHP) Architecture which is very much similar to LEMP with only difference that instead of using Apache, LEMP uses Nginx. Otherwise functionality wise both are more or less same.

Both are used for Dynamic websites and web applications deployment. There are other similar architecture like WAMP(Windows, Apache, MySQL and PHP) in Windows and MAMP(MAC, Apache, MySQL and PHP) in MAC. In this article, We will keep our focus on LEMP deployment on CentOS 8.

How to Install LEMP(Linux, Nginx, MySQL and PHP) Stack on CentOS 8 Using 12 Best Steps 1

Install LEMP(Linux, Nginx, MySQL and PHP) Stack on CentOS 8

Also Read: How to Install Ionic Framework in Linux (RHEL/CentOS 8) Using 10 Easy Steps

Step 1: Prerequisites

a)You need a Running RHEL/CentOS 8 System.

b)You should have dnf tool installed in your System. You can check 31 Useful DNF Command Examples for RPM Package Management in Fedora/RHEL/CentOS to know more about dnf Command.

c)You should have sudo access to run privileged commands in Your System. You can check How to Add Users to Sudoers to know more about providing sudo access to the User.

Step 2: Update Your System

Before going through the steps to Install LEMP Stack on CentOS 8 you need to first update your System using dnf update -y command. This command will install all the latest available releases from Enabled Repository.

[root@localhost ~]# dnf update -y
CentOS-8 - AppStream 4.8 kB/s | 4.3 kB 00:00
CentOS-8 - Base 4.9 kB/s | 3.9 kB 00:00
CentOS-8 - Extras 1.4 kB/s | 1.5 kB 00:01
Extra Packages for Enterprise Linux Modular 8 - x86_64 7.8 kB/s | 8.5 kB 00:01
Extra Packages for Enterprise Linux 8 - x86_64 10 kB/s | 9.1 kB 00:00
Extra Packages for Enterprise Linux 8 - x86_64 1.5 MB/s | 7.2 MB 00:04
Google Cloud SDK 262 B/s | 454 B 00:01
Dependencies resolved.
Package Architecture Version Repository Size
kernel-core x86_64 4.18.0-193.6.3.el8_2 BaseOS 28 M
alsa-lib x86_64 AppStream 441 k
container-selinux noarch 2:2.124.0-1.module_el8.2.0+305+5e198a41 AppStream 47 k
containers-common x86_64 1:0.1.40-11.module_el8.2.0+377+92552693 AppStream 50 k
fribidi x86_64 1.0.4-8.el8 AppStream 89 k
fuse-overlayfs x86_64 0.7.2-5.module_el8.2.0+305+5e198a41 AppStream 60 k
git x86_64 2.18.4-2.el8_2 AppStream 186 k
git-core x86_64 2.18.4-2.el8_2 AppStream 4.0 M
git-core-doc noarch 2.18.4-2.el8_2 AppStream 2.3 M

Step 3: Install Nginx on CentOS 8

Then you need to install Nginx on CentOS 8 using dnf install nginx -y command as shown below. Nginx is a web server which will be used for web applications deployment. It can also be used as a reverse proxy, load balancer, mail proxy etc. You might have seen similar functionality of Apache in LAMP Stack.

[root@localhost ~]# dnf install nginx -y
Last metadata expiration check: 0:09:06 ago on Sat 04 Jul 2020 04:52:09 PM EDT.
Dependencies resolved.
Package Architecture Version Repository Size
nginx x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 570 k
Installing dependencies:
gd x86_64 2.2.5-6.el8 AppStream 144 k
libXpm x86_64 3.5.12-8.el8 AppStream 58 k
libwebp x86_64 1.0.0-1.el8 AppStream 273 k
nginx-all-modules noarch 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 23 k
nginx-filesystem noarch 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 24 k
nginx-mod-http-image-filter x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 35 k
nginx-mod-http-perl x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 45 k
nginx-mod-http-xslt-filter x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 33 k
nginx-mod-mail x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 64 k
nginx-mod-stream x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream 85 k
Enabling module streams:
nginx 1.14

Transaction Summary
Install 11 Packages

Total download size: 1.3 M
Installed size: 3.4 M

Step 4: Enable Nginx Server on CentOS 8

After successful installation of Nginx Server on CentOS 8 you need to enable the service using systemctl enable --now nginx command so that the service will come up automatically even after a reboot.

[root@localhost ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/ → /usr/lib/systemd/system/nginx.service.

Step 5: Install MariaDB Server on CentOS 8

Next step is to install MariaDB Server using dnf install mariadb-server -y command as shown below. MariaDB is an open source MySQL DB Server used in the backend to store and handle data by PHP dynamic pages.

[root@localhost ~]# dnf install mariadb-server -y
Last metadata expiration check: 0:15:12 ago on Sat 04 Jul 2020 04:52:09 PM EDT.
Dependencies resolved.
Package Architecture Version Repository Size
mariadb-server x86_64 3:10.3.17-1.module_el8.1.0+257+48736ea6 AppStream 16 M
Installing dependencies:
mariadb-errmsg x86_64 3:10.3.17-1.module_el8.1.0+257+48736ea6 AppStream 232 k
psmisc x86_64 23.1-4.el8 BaseOS 150 k
Installing weak dependencies:
mariadb-backup x86_64 3:10.3.17-1.module_el8.1.0+257+48736ea6 AppStream 6.0 M
mariadb-gssapi-server x86_64 3:10.3.17-1.module_el8.1.0+257+48736ea6 AppStream 49 k
mariadb-server-utils x86_64 3:10.3.17-1.module_el8.1.0+257+48736ea6 AppStream 1.6 M

Transaction Summary
Install 6 Packages

Total download size: 24 M
Installed size: 130 M

Step 6: Enable MariaDB Service 

Once MariaDB Server is installed you need to enable the mariadb service using systemctl enable --now mariadb command as shown below.

[root@localhost ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/ → /usr/lib/systemd/system/mariadb.service.

Step 7: Secure MariaDB Server using mysql_secure_installation

Before going further you need to first set the mysql credentials using mysql_secure_installation command if this is a first time installation, remove anonymous user to secure the DB Server and also remove the root login access as shown below.

[root@localhost ~]# mysql_secure_installation


In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n
... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Step 8: Install PHP on CentOS 8

After securing MyDB Server now you need to move ahead and install PHP on CentOS 8 using below dnf install command. PHP is the scripting language in LEMP Stack which will be used for processing and generating dynamic web content from backend data.

[root@localhost ~]# dnf install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y
Last metadata expiration check: 0:27:13 ago on Sat 04 Jul 2020 04:52:09 PM EDT.
Dependencies resolved.
Package Architecture Version Repository Size
php x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 1.5 M
php-fpm x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 1.6 M
php-gd x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 84 k
php-mbstring x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 580 k
php-mysqlnd x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 190 k
php-opcache x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 231 k
php-xml x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 188 k
Installing dependencies:
apr x86_64 1.6.3-9.el8 AppStream 125 k
apr-util x86_64 1.6.1-6.el8 AppStream 105 k
httpd x86_64 2.4.37-21.module_el8.2.0+382+15b0afa8 AppStream 1.7 M
httpd-filesystem noarch 2.4.37-21.module_el8.2.0+382+15b0afa8 AppStream 36 k
httpd-tools x86_64 2.4.37-21.module_el8.2.0+382+15b0afa8 AppStream 103 k
mod_http2 x86_64 1.11.3-3.module_el8.2.0+307+4d18d695 AppStream 157 k
php-cli x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 3.1 M
php-common x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 661 k
php-pdo x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream 122 k
centos-logos-httpd noarch 80.5-2.el8 BaseOS 24 k
mailcap noarch 2.1.48-3.el8 BaseOS 39 k
Installing weak dependencies:
apr-util-bdb x86_64 1.6.1-6.el8 AppStream 25 k
apr-util-openssl x86_64 1.6.1-6.el8 AppStream 27 k
Enabling module streams:
httpd 2.4
php 7.2

Transaction Summary
Install 20 Packages

Total download size: 11 M
Installed size: 35 M


Please note that if you have firewall running in your System then you need to allow the relevant Port from firewall using firewall-cmd command else it will block the Port and hence will not allow the LEMP Stack to run.

Step 9: Enable PHP-FPM on CentOS 8

Now you need to enable PHP-FPM service on CentOS 8 using systemctl enable --now php-from command as shown below. PHP FPM is known as FastCGI Process Manager which will be used to take requests from Nginx Server for further processing.

[root@localhost ~]# systemctl enable --now php-fpm
Created symlink /etc/systemd/system/ → /usr/lib/systemd/system/php-fpm.service.

Step 10: Configure PHP FPM

By default you will use apache is configured as user and group in /etc/php-fpm.d/www.conf file. This needs to be changed to nginx user and group as shown below.

[root@localhost ~]# vi /etc/php-fpm.d/www.conf
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

After changing user and group to nginx you need to restart PHP FPM and Nginx service as shown below.

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# systemctl restart php-fpm

Step 11: Write A Test PHP Script

Since all the tools are installed so now we are ready to write our first Test PHP Script. In this script, we will use a simple phpinfo() function to display all the information about PHP Framework.

[root@localhost ~]# vi /usr/share/nginx/html/test.php

Step 12: Test Your PHP Script

Once the script is saved and close you need to access the Server and check its reachability using curl -I -XGET localhost/test.php command as shown below. As you can see the response from below output, it is 200 which means server is up and running. Alternatively, you can also put the URL localhost/test.php in your Server browser and check the Server response.

[root@localhost ~]# curl -I -XGET localhost/test.php
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sat, 04 Jul 2020 21:29:05 GMT
Content-Type: application/octet-stream
Content-Length: 22
Last-Modified: Sat, 04 Jul 2020 21:27:01 GMT
Connection: keep-alive
ETag: "5f00f425-16"
Accept-Ranges: bytes



Popular Recommendations:-

How to Set MariaDB root password in RHEL/CentOS 7/8 Using Simple mysql_secure_installation

How to Install AWS CLI in Linux (RedHat/CentOS 7/8) Using 6 Easy Steps

11 Useful LVM(lvcreate, pvcreate and vgcreate) command examples on Linux

How to Install and Configure FTP Server(vsftpd) in Linux (RedHat/CentOS 7/8) Using 8 Easy Steps

20 Useful Linux History Command Examples | Bash Command History

How to Install PHP on Ubuntu 18.04

How to Install Ruby on Ubuntu 18.04 with Easy Steps

How to Install Ruby on CentOS/RedHat 7 in 5 Easy Steps

33 Practical Examples of ulimit command in Linux/Unix for Professionals

Install Node.js in 6 Easy Steps on Ubuntu 18.04

How to Install NVM for Node.js on Ubuntu 18.04

How to Limit CPU Limit of a Process Using CPULimit in Linux (RHEL/CentOS 7/8)

How to Install Rust Programming Language in Linux Using 6 Best Steps

How to Install LEMP Stack on CentOS 8


Leave a Reply