This guide demonstrates how to install LEMP Stack on CentOS 8, we have explained how Install MySQL as well as MariaDB, an alternative for MySQL.

1. Installing NGNIX Web Server

The Nginx web server is the most popular web servers in the world. It’s active community and detailed documentation eventually makes it a great choice for hosting a website.

Install Nginx using ( Yellowdog Updater, Modified ) YUM :

The sudo (Super User DO) command allows you to run programs with the security privileges.

sudo yum update
sudo yum install nginx -y

Once the installation is completed, enable and start the Apache service.

sudo systemctl enable nginx
sudo systemctl start nginx

Configure Firewall

Enable Firewalld

systemctl enable firewalld

Start Firewalld

systemctl start firewalld

Check the Status of Firewalld

systemctl status firewalld

If your server is protected by the firewall and you haven’t opened the HTTP and HTTPS ports. Enable them with the following command.

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

To verify that the change was applied, you can run:

sudo firewall-cmd --permanent --list-all

You’ll see output like this:

Output
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: cockpit dhcpv6-client http https ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

You’ll need to reload the firewall configuration so the changes take effect:

sudo firewall-cmd --reload

Now you can verify Apache installation. Every process in Apache is managed with the systemctl command. Verify the installation of Apache with the following command.

sudo systemctl status nginx
 nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor prese>
   Active: active (running) since Tue 2020-09-08 16:16:00 CST; 8min ago
 Main PID: 50194 (nginx)
    Tasks: 2 (limit: 5045)
   Memory: 9.4M
   CGroup: /system.slice/nginx.service
           ├─50194 nginx: master process /usr/sbin/nginx
           └─50195 nginx: worker process

Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: Starting The nginx HTTP and>
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ nginx[50190]: nginx: the configuration >
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ nginx[50190]: nginx: configuration file>
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: nginx.service: Failed to pa>
Sep 08 16:16:00 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: Started The nginx HTTP and >
lines 1-15/15 (END)

You can now check which IP address is accessible, by running the following command:

curl -4 icanhazip.com

Type the IP address that you receive in your web browser and it will take you to Apache’s default landing page:

2. Install MySQL/MariaDB Database

The third layer of the LAMP stack is MySQL or MariaDB. Both are open-source database management systems used for storing and managing data on your website.

To Install MySQL:

sudo yum install mysql-server -y

To Install MariaDB:

sudo yum install mariadb-server -y
If command sudo systemctl enable mysqld.service leads to a prompt ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' then restart DB as follow's:

For MySQL:

service mysqld start

For MariaDB:

sudo systemctl start mariadb

After completing the installation, enable the MySQL service to auto-start on the system start. Also start service manually for the first time.

sudo systemctl enable mysqld.service
sudo systemctl start mysqld.service

Then check the service current status using the following command:

sudo systemctl status mysqld.service
  mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor pres>
   Active: active (running) since Tue 2020-09-08 16:38:04 CST; 1min 4s ago
  Process: 51591 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, s>
  Process: 51461 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service >
  Process: 51437 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, sta>
 Main PID: 51547 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 5045)
   Memory: 433.8M
   CGroup: /system.slice/mysqld.service
           └─51547 /usr/libexec/mysqld --basedir=/usr

Sep 08 16:37:53 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: Starting MySQL 8.0 database>
Sep 08 16:37:53 iZa2dc2w6sz7gbkh2kc8exZ mysql-prepare-db-dir[51461]: Initializi>
Sep 08 16:38:04 iZa2dc2w6sz7gbkh2kc8exZ systemd[1]: Started MySQL 8.0 database >
lines 1-16/16 (END)

The MySQL installation has been completed. Now you can connect the MySQL server without any password but we recommend securing the MySQL installation. The MySQL packages provide mysql_secure_installation command to apply the security. Just run the below command on terminal:

sudo mysql_secure_installation

and follow the on-screen instructions. Below are the details which require user input.

  • Press y|Y for Yes, any other key for No: y
  • Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
  • New password: [ENTER STRONG PASSWORD HERE]
  • Re-enter new password: RE ENTER PASSWORD HERE
  • Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
  • Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
  • Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
  • Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
  • Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

5: Install PHP

Finally you can install PHP, by default CentOS ships with PHP 7.2. So you can install PHP using the following command.

sudo yum install php-fpm php-mysqlnd -y

Now open the /etc/php-fpm.d/www.conf configuration file using nano or your editor of choice:

sudo nano /etc/php-fpm.d/www.conf

Look for the user and group directives. If you are using nano, you can hit CTRL+W to search for these terms inside the open file. Make sure to change both values from apache to nginx:

/etc/php-fpm.d/www.conf

…
; RPM: apache user chosen to provide access to the same directories as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache
…

/etc/php-fpm.d/www.conf

…
; 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
…

Hit CTRL + X followed by Y and Enter to save and exit the file.

To enable and start the php-fpm service, run:

sudo systemctl start php-fpm

After the installation is finished, you’ll need to restart the Apache web server in order to enable the PHP module:

sudo systemctl restart nginx

6: Verify the LEMP setup

Create a new file with the name info.php

sudo nano /var/www/html/info.php

Paste the following code in the editor and save the file.

<?php
phpinfo();

Hit CTRL + X followed by Y and Enter to save and exit the file.

Now open your browser and point it to your external IP address of your server followed by info.php in the URL.

http://IP_Address/info.php

You will see the PHP information which indicates you have installed and configured NGNIX, MySQL/MariaDB and PHP on your CentOS 8 server.

Conclusion

Now that we have installed LEMP STACK, you can now host your website in the server. One of the popular thing you can do is install WordPress and design your own website or blog for your business.

To manage MySQL with GUI, we need to install phpMyAdmin. Operations such as the management of databases, tables, indexes, permissions, and so on are executed with the graphical user interface of phpMyAdmin.

Hope this tutorial is helpful and comment down if you have any query or issue.