The LEMP stack server is a server running Linux, Nginx (pronounced Engine x), MySQL/MariaDB and PHP (or Perl / Python). It is similar to the LAMP server except that the web server platform is controlled by Nginx instead of Apache.

In this guide, we’ll get a LEMP stack installed on CentOS 8 and 9.

Prerequisites

  • Recommended minimum of 10GB of free disk space.
  • CentOS 8 ISO install file.

Step 1 – Linux Operating system – Centos8

CentOS 8 has been released on 24-09-2019. CentOS 8 could be run on various architectures including, x86_64 (64-bit Intel/AMD), aarch64 (64-bit ARM) and ppc64le (IBM POWER, little-endian). First step, you will need to download CentOS 8 from their official websites. You can download the CentOS 8 Linux using following URLs:

http://isoredirect.centos.org/centos/8/isos/x86_64/

You can also download the CentOS 8 ISO from the CentOS 8 Mirror.

After that, we need to mount this iso to CD/DVD drive if you use VMware ESXi or VirtualBox to ready bootable. If you install on local PC or server then you need to make a bootable USB stick or DVD.

Start the system up and you will get the following screen:

How to install Nginx, MariaDB and PHP (LEMP Stack) on Centos centos linux

Chose Install CentosOS Linux 8.0.1905:

How to install Nginx, MariaDB and PHP (LEMP Stack) on Centos centos linux

Select Localization, Software and System settings as below or you can customize for yourself.

How to install Nginx, MariaDB and PHP (LEMP Stack) on Centos centos linux

Wait till installation done and click Reboot button at bottom left.

And we need to create root password, also new user account for yourself at User Creation.

How to install Nginx, MariaDB and PHP (LEMP Stack) on Centos centos linux

After reboot process done you can check ssh to your server with your user account you create at last step.

Step 2 – Install Nginx

Nginx is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols. It also can run as a load balancer, HTTP cache, and web server (origin server). It has a strong focus on high concurrency, high performance and very low memory usage.

Now you have a clean install of Centos8. Enter the follow command to install Nginx web server. We using sudo command to run administration tasks. At the first time we need to input your account password:

[tuanhung@localhost ~]$ sudo yum install nginx -y
We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for tuanhung:
CentOS-8 - AppStream [=== ] --- B/s | 0 B --:-- ETA

After it’s installed, we can start Nginx and make it automatic start at boot time with this follow commands:

[tuanhung@localhost ~]$ sudo systemctl start nginx
[tuanhung@localhost ~]$ sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

Check status Nginx:

[tuanhung@localhost ~]$ sudo systemctl status nginx
? nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: dis>
Active: active (running) since Thu 2019-12-19 04:12:46 EST; 37s ago
Main PID: 30930 (nginx)
Tasks: 2 (limit: 5061)
Memory: 5.6M
CGroup: /system.slice/nginx.service
??30930 nginx: master process /usr/sbin/nginx
??30931 nginx: worker process

Dec 19 04:12:46 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse>


Dec 19 04:12:46 localhost.localdomain nginx[30926]: nginx: the configuration file /et>


Dec 19 04:12:46 localhost.localdomain nginx[30926]: nginx: configuration file /etc/ng>


Dec 19 04:12:46 localhost.localdomain systemd[1]: nginx.service: Failed to parse PID >


Dec 19 04:12:46 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse >


lines 1-15/15 (END)

Ok now Nginx is running. But till now we need do one more step so clients can reach our web server.

By default, Centos 8 using firewalld to control and protect you server from unwanted traffic. You can see the firewall rules or services status by this command:

[tuanhung@localhost ~]$ sudo firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[tuanhung@localhost ~]$

As you see now the firewall state only allow 3 services:cockpit,dhcpv6,ssh and block the HTTP connection so you need to open it to access the Web Server from outside. Do it by this command:

[tuanhung@localhost ~]$ sudo firewall-cmd --zone=public --add-service=http

We check again:

[tuanhung@localhost ~]$ sudo firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: cockpit dhcpv6-client http ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[tuanhung@localhost ~]$

Then head to your browser and you should see something like that.

How to install Nginx, MariaDB and PHP (LEMP Stack) on Centos centos linux

Note 1: In mycase i’m install on a VPS at same LAN. IP 10.10.8.3 is the IP of VPS Server. In your case maybe difference.

Note 2: in firewalld you can add command with option –permanent to make rule persistent across system reboots. Like this:

firewall-cmd --permanent --zone=public --add-service=http

Step 3 – Install MariaDB Server

MariaDB has some optimizations to improve the performance as compared to MySQL. It’s made by the original developers of MySQL and guaranteed to stay open source.

Install the MariaDB on Centos 8:

[tuanhung@localhost ~]$ sudo yum install mariadb-server mariadb -y

Also we start the service and enable it to able start automatic at boot time. After that we verified status to make sure the services is running.

[tuanhung@localhost ~]$ sudo systemctl start mariadb
[tuanhung@localhost ~]$ sudo systemctl enable 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/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[tuanhung@localhost ~]$ sudo systemctl status mariadb
? mariadb.service - MariaDB 10.3 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: d>
Active: active (running) since Thu 2019-12-19 05:16:37 EST; 1min 24s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 32550 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 30 (limit: 5061)
Memory: 87.0M
CGroup: /system.slice/mariadb.service
??32550 /usr/libexec/mysqld --basedir=/usr
Dec 19 05:16:36 localhost.localdomain mysql-prepare-db-dir[32448]: Please report any >
...

Ok now the MariaDB is running. We need to setup password for root user DB. Using follow script:

[tuanhung@localhost ~]$ sudo mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! 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]
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]
... 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]
... Success!

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]
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

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

Reload privilege tables now? [Y/n]
... 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!
[tuanhung@localhost ~]$

Step 4 – Install PHP module on Centos 8

We using the default repository for install PHP so we can easy upgradable later. By default Centos 8 repository already has PHP 7.2

Start the installation with the following command:

[tuanhung@localhost ~]$ sudo yum install -y php php-fpm phh-cli php-mysqlnd

As same as other component, we need to start the service after install and make it automatic to run at boot time.

[tuanhung@localhost ~]$ sudo systemctl start php-fpm
[tuanhung@localhost ~]$ sudo systemctl enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[tuanhung@localhost ~]$

Check status:

[tuanhung@localhost ~]$ sudo systemctl status php-fpm
? php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: d>
Active: active (running) since Thu 2019-12-19 08:18:37 EST; 56s ago
Main PID: 1236 (php-fpm)
Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 6 (limit: 5061)
Memory: 24.5M
CGroup: /system.slice/php-fpm.service
??1236 php-fpm: master process (/etc/php-fpm.conf)
??1243 php-fpm: pool www
??1244 php-fpm: pool www
??1245 php-fpm: pool www
??1246 php-fpm: pool www
??1247 php-fpm: pool www

Dec 19 08:18:37 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Ma>


….

Let check if the PHP is working or not by create file info.php with this info as same as follow (you can using vi, nano editor whatever you familiar with):

[tuanhung@localhost html]$ cat /usr/share/nginx/html/info.php

[tuanhung@localhost html]$

After that we restart Nginx to change take effect.

[tuanhung@localhost html]$ sudo systemctl restart nginx

Go to browser and and heading to http://10.10.8.3/info.php and you should see something like that:

How to install Nginx, MariaDB and PHP (LEMP Stack) on Centos centos linux

As you see in screenshot, PHP is working as expected.

After test we need to remove the info.php for security reason.

[tuanhung@localhost ~]$ sudo rm /usr/share/nginx/html/info.php
[tuanhung@localhost ~]$

Conclusion

Congratulation! You have done setup LEMP stack on your VPS/Server with Centos 8. Now you can host some blog platform as WordPress, Ghost or make some beautiful website in there. If you got any problem in installation follow my step please share comment below and share your feedback so i can make it better. Thank!