Saturday, November 16, 2024
Google search engine
HomeGuest BlogsInstall LEMP Stack on Rocky Linux 8|AlmaLinux 8

Install LEMP Stack on Rocky Linux 8|AlmaLinux 8

LEMP is an acronym for Linux Nginx, MariaDB/MySQL, and PHP. Nginx is a web server, MariaDB/MySQL is a database management application, PHP(Hypertext Pre-processor) is a programming language that is used on the backend to build web applications. All these open-source software make up the LEMP stack which collectively is used to host and serve web pages/applications in a production cycle or any other development stage.

Popularly, the LEMP stack is used to host highly scalable websites with high traffic. In this guide, we will systematically walk through the installation and configuration of LEMP Stack on Rocky Linux 8 / AlmaLinux 8.

Getting Started.

For this guide, you will require the following:

  • A Rocky Linux 8|AlmaLinux 8 system.
  • A user with sudo access

Begin by updating all the available packages on your system as below.

sudo dnf update -y

Step 1 – Install Nginx on Rocky Linux 8|AlmaLinux 8.

This powerful web server exists in the default Rocky Linux 8|AlmaLinux 8 repositories and can be installed using the command below.

sudo dnf install nginx 

Dependency tree:

Dependencies resolved.
================================================================================
 Package          Arch   Version                                Repo       Size
================================================================================
Installing:
 nginx            x86_64 1:1.14.1-9.module+el8.4.0+542+81547229 appstream 566 k
Installing dependencies:
 nginx-all-modules
                  noarch 1:1.14.1-9.module+el8.4.0+542+81547229 appstream  22 k
 nginx-filesystem noarch 1:1.14.1-9.module+el8.4.0+542+81547229 appstream  23 k
 nginx-mod-http-image-filter
                  x86_64 1:1.14.1-9.module+el8.4.0+542+81547229 appstream  34 k
 nginx-mod-http-perl
                  x86_64 1:1.14.1-9.module+el8.4.0+542+81547229 appstream  45 k
 nginx-mod-http-xslt-filter
                  x86_64 1:1.14.1-9.module+el8.4.0+542+81547229 appstream  32 k
 nginx-mod-mail   x86_64 1:1.14.1-9.module+el8.4.0+542+81547229 appstream  63 k
 nginx-mod-stream x86_64 1:1.14.1-9.module+el8.4.0+542+81547229 appstream  84 k
Enabling module streams:
 nginx                   1.14                                                  

Transaction Summary
================================================================================
Install  8 Packages

Total download size: 870 k
Installed size: 2.0 M
Is this ok [y/N]: y

Once installed, start and enable the Nginx webserver to run automatically on boot.

sudo systemctl start nginx 
sudo systemctl enable nginx 

Check the status of the service.

$ systemctl status nginx
 nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-12-29 03:00:08 EST; 7s ago
  Process: 128762 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 128752 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 128750 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 128763 (nginx)
    Tasks: 3 (limit: 36449)
   Memory: 7.6M
   CGroup: /system.slice/nginx.service
           ├─128763 nginx: master process /usr/sbin/nginx
           ├─128764 nginx: worker process
           └─128765 nginx: worker process

Dec 29 03:00:08 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 29 03:00:08 localhost.localdomain nginx[128752]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 29 03:00:08 localhost.localdomain nginx[128752]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 29 03:00:08 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.

The installed version of Nginx can be checked using the command:

$ nginx -v
nginx version: nginx/1.14.1

Test if Nginx is able to server web pages using the URL http://IP_Address or http://domain_name

Allow HTTP and HTTPS through the firewall.

sudo firewall-cmd --permanent --add-service={http,https}
sudo firewall-cmd --reload

You should be able to see the below page:

How To Install LEMP Stack on Rocky Linux 8AlmaLinux 8

Step 2 – Install MariaDB Server on Rocky Linux 8|AlmaLinux 8.

MariaDB can be installed on Rocky Linux 8|AlmaLinux 8 as below.

sudo dnf -y install @mariadb

Start and enable MariaDB on Rocky Linux 8|AlmaLinux 8.

sudo systemctl enable --now mariadb

Harden the MariaDB instance as below.

$ sudo mysql_secure_installation
Enter current password for root (enter for none): Press Enter
....
Set root password? [Y/n] y
New password: Enter Password
Re-enter new password: Re-Enter Password
......
Remove anonymous users? [Y/n] y
...
Disallow root login remotely? [Y/n] y
...
Remove test database and access to it? [Y/n] y
....
Reload privilege tables now? [Y/n] y
.....
Thanks for using MariaDB!

Login into the MariaDB shell.

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
MariaDB [(none)]> exit
Bye

Step 3 – Install PHP on Rocky Linux 8|AlmaLinux 8.

The AppStream repository has multiple versions of PHP that can be listed using the command:

$ sudo dnf module list php
Last metadata expiration check: 0:50:10 ago on Wed 29 Dec 2021 02:14:25 AM EST.
Rocky Linux 8 - AppStream
Name      Stream       Profiles                       Summary                   
php       7.2 [d]      common [d], devel, minimal     PHP scripting language    
php       7.3          common [d], devel, minimal     PHP scripting language    
php       7.4          common [d], devel, minimal     PHP scripting language    

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

As seen, the default version is PHP 7.2 and can be reset as below.

sudo dnf module reset php

Enable the desired version.

# For PHP 7.4
sudo dnf -y module install php:7.4

# For PHP 7.3
sudo dnf -y module install php:7.3

# For PHP 7.2
sudo dnf -y module install php:7.2

Proceed as below.

Dependencies resolved.
================================================================================
 Package          Arch   Version                                Repo       Size
================================================================================
Installing group/module packages:
 php-cli          x86_64 7.4.19-1.module+el8.5.0+696+61e7c9ba   appstream 3.1 M
 php-common       x86_64 7.4.19-1.module+el8.5.0+696+61e7c9ba   appstream 701 k
 php-fpm          x86_64 7.4.19-1.module+el8.5.0+696+61e7c9ba   appstream 1.6 M
 php-json         x86_64 7.4.19-1.module+el8.5.0+696+61e7c9ba   appstream  73 k
 php-mbstring     x86_64 7.4.19-1.module+el8.5.0+696+61e7c9ba   appstream 483 k
 php-xml          x86_64 7.4.19-1.module+el8.5.0+696+61e7c9ba   appstream 173 k
Installing dependencies:
 httpd-filesystem noarch 2.4.37-43.module+el8.5.0+714+5ec56ee8  appstream  38 k
Installing module profiles:
 php/common                                                                    
Enabling module streams:
 httpd                   2.4                                                   
 php                     7.4                                                   

Also, install PHP extensions.

sudo dnf -y install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring php-zip

Verify the installed version of PHP.

$ php --version
PHP 7.4.19 (cli) (built: May  4 2021 11:06:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.19, Copyright (c), by Zend Technologies

Start and enable PHP using php-fpm.

sudo systemctl enable --now php-fpm

Check the status of the service.

$ systemctl status php-fpm
 php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-12-29 03:06:55 EST; 6s ago
 Main PID: 131753 (php-fpm)
   Status: "Ready to handle connections"
    Tasks: 6 (limit: 36449)
   Memory: 13.7M
   CGroup: /system.slice/php-fpm.service
           ├─131753 php-fpm: master process (/etc/php-fpm.conf)
           ├─131754 php-fpm: pool www
           ├─131755 php-fpm: pool www
           ├─131756 php-fpm: pool www
           ├─131757 php-fpm: pool www
           └─131758 php-fpm: pool www

Dec 29 03:06:55 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Manager...
Dec 29 03:06:55 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manager.

Normally PHP-FPM runs as using the APache user by default. Since we are using Nginx, we need to edit the configuration file.

$ sudo vim /etc/php-fpm.d/www.conf
user = nginx
Group = nginx

Apply the made changes.

sudo systemctl reload php-fpm

Now create a simple PHP file in the /usr/share/nginx/html directory.

sudo vim /usr/share/nginx/html/info.php

In the file, add the below content.

<?php

phpinfo();

?>

Restart Nginx for the changes to apply.

sudo systemctl restart nginx

Access the webpage using the URL http://IP_address/info.php or http://domain_name/info.php. You will see a detailed list of PHP and its extensions.

How To Install LEMP Stack on Rocky Linux 8AlmaLinux 8 1

Step 4 – Configure Nginx Server Block on Rocky Linux 8|AlmaLinux 8.

The Nginx server block allows one to run multiple websites by defining the site document root directory. These root document directories contain the website files.

For this guide, we will create a single Nginx server block file using the steps below:

1. Create a Website Directory

This is the first step, ensure that you replace example.geeksforgeeks.org with your domain name.

sudo mkdir /var/www/example.geeksforgeeks.org
sudo vim /var/www/example.geeksforgeeks.org/index.html

In the file, add the content:

<html>
  <head>
    <title>Welcome to geeksforgeeks.org!</title>
  </head>
  <body>
    <h1>Voila! The LEMP server is working perfectly!</h1>
  </body>
</html>

2. Create a Nginx Server Block for the Website

This is a configuration file for the site. It spells out how Nginx will serve the requests.

sudo vim /etc/nginx/conf.d/example.neveropen.tech.conf

Add the below lines to the file.

server {
    listen  80;

    server_name example.geeksforgeeks.org www.example.geeksforgeeks.org;

    location / {
        root  /var/www/example.geeksforgeeks.org;
        index  index.html index.htm;
        try_files $uri $uri/ =404;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

Restart Nginx.

sudo systemctl restart nginx

Add your domain name to the list of known hosts.

$ sudo vim /etc/hosts
192.168.205.2 example.geeksforgeeks.org

Step 5 – Test the Smaple Website

Now the website can be accessed using the URL http://domain_name.

You should be able to see the below page.

How To Install LEMP Stack on Rocky Linux 8AlmaLinux 8 2

Now that confirms that our LEMP stack is working perfectly and that marks the end of this amazing guide.

Interested in more?

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments