HomeHOWTOSHow to Install LEMP stack on Rocky Linux 9|AlmaLinux 9

How to Install LEMP stack on Rocky Linux 9|AlmaLinux 9

This guide will walk you through how to install LEMP stack on Rocky Linux 9|AlmaLinux 9. LEMP is a collection of open-source software that is free to use. The acronym LEMP refers to the first letters of Linux (operating system), Nginx Server, MySQL (database software), and PHP, PERL, or Python, which are the main components of a functional general-purpose web server. The LEMP stack is commonly used to host highly scalable, high-traffic websites.

How does LEMP Stack work?

The Webserver(Nginx) receives the web requests from the browser. If the file requested is a PHP file, it passes the request to PHP which loads and executes the code in the requested file as desired. PHP is also responsible for database linking to obtain any data referenced in the code.

The PHP executions and fetched data from the DB are then used to create HTML scripts appearing as web pages to the user. The LEMP stack components work together to handle both static and dynamic web pages.

The results are finally passed by PHP to Nginx which then sends them back to the web browser. That is a simple explanation of how the LEMP Stack works.

The following steps will guide you on how to install the Nginx on Rocky Linux 9| AlmaLinux 9 successfully.

Step 1 – Install Nginx on Rocky Linux 9|AlmaLinux 9

Begin by updating all of your system’s available packages, as shown below.

sudo dnf update

This powerful web server is available in the default Rocky Linux 9|AlmaLinux 9 repositories and can be installed with the command below.

sudo dnf install nginx

Sample results:

Last metadata expiration check: 2:21:18 ago on Sun 23 Oct 2022 03:42:37 PM EAT.
Dependencies resolved.
=====================================================================================
 Package                 Architecture Version                  Repository       Size
=====================================================================================
Installing:
 nginx                   x86_64       1:1.20.1-10.el9          appstream       594 k
Installing dependencies:
 nginx-filesystem        noarch       1:1.20.1-10.el9          appstream        11 k
 rocky-logos-httpd       noarch       90.11-1.el9              appstream        24 k

Transaction Summary
=====================================================================================
Install  3 Packages

Total download size: 629 k
Installed size: 1.8 M
Is this ok [y/N]: y

Start the Nginx webserver after installation and set it to launch automatically.

sudo systemctl start nginx 
sudo systemctl enable nginx 

Check the service’s status.

$ systemctl status nginx
nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset:>
     Active: active (running) since Sun 2022-10-23 18:16:03 EAT; 7s ago
    Process: 8352 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/>
    Process: 8353 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 8354 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 8355 (nginx)
      Tasks: 3 (limit: 23450)
     Memory: 2.8M
        CPU: 48ms
     CGroup: /system.slice/nginx.service
             ├─8355 "nginx: master process /usr/sbin/nginx"
             ├─8356 "nginx: worker process"
             └─8357 "nginx: worker process"

Oct 23 18:16:03 localhost.localdomain systemd[1]: Starting The nginx HTTP and revers>
Oct 23 18:16:03 localhost.localdomain nginx[8353]: nginx: the configuration file /et>
Oct 23 18:16:03 localhost.localdomain nginx[8353]: nginx: configuration file /etc/ng>
Oct 23 18:16:03 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse

The following command can be used to determine the version of Nginx that is currently installed:

$ nginx -v
nginx version: nginx/1.20.1

Allow HTTP and HTTPS through the firewall.

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

Test if Nginx is able to server web pages using the URL http://IP_Address on the web browser. If all is okay, you should be able to see the page below:

How to install LEMP stack on Rocky Linux 9|AlmaLinux 9

Step 2 – Install MariaDB Server on Rocky Linux 9|AlmaLinux 9

On Rocky Linux 9 |AlmaLinux 9, MariaDB can be installed as shown below.

sudo dnf install mariadb-server mariadb -y

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the MariaDB, and verify the status using the commands below.

sudo systemctl start mariadb
sudo systemctl enable mariadb

Check the status of the service:

$ systemctl status mariadb
 mariadb.service - MariaDB 10.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor p>
     Active: active (running) since Sun 2022-10-23 19:27:01 EAT; 1min 34s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 10645 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 8 (limit: 23450)
     Memory: 98.4M
        CPU: 871ms
     CGroup: /system.slice/mariadb.service
             └─10645 /usr/libexec/mariadbd --basedir=/usr

Finally, you should secure your MariaDB installation by running the command below.

 sudo mysql_secure_installation

Proceed as shown:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
.....
Enter current password for root (enter for none):PRESS ENTER 
OK, successfully used password, moving on...
....
Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
 ... Success!
....
Change the root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

Remove anonymous users? [Y/n] y
 ... Success!
.....
Disallow root login remotely? [Y/n] y
 ... Success!
.......
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
.....
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!

Using the following command, you can log in to MySQL after it has been secured.

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.5.16-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)]> 

Check out the current databases on your database server.

MariaDB [(none)]> SHOW DATABASES;


Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
MariaDB [(none)]> QUIT
Bye

Step 3 – Install PHP on Rocky Linux 9|AlmaLinux 9

The default PHP version available in Rocky Linux 9|AlmaLinux 9 default repositories is PHP 8.0. This version and its extensions can be installed using the command:

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

Once the installation is complete, use the following commands to start PHP-FPM, enable it (so that it starts automatically when the system boots), and check its status.

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Sample results:

$ systemctl status php-fpm
php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor p>
     Active: active (running) since Sun 2022-10-23 19:55:36 EAT; 46s ago
   Main PID: 12868 (php-fpm)
     Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 6 (limit: 23450)
     Memory: 14.1M
        CPU: 138ms
     CGroup: /system.slice/php-fpm.service
             ├─12868 "php-fpm: master process (/etc/php-fpm.conf)"
             ├─12876 "php-fpm: pool www"
             ├─12877 "php-fpm: pool www"
             ├─12878 "php-fpm: pool www"
             ├─12879 "php-fpm: pool www"
             └─12880 "php-fpm: pool www"

The default user for PHP-FPM is Apache. We must modify the following line because we are using the Nginx web server.

Edit the file /etc/php-fpm.d/www.conf with your preferred editor.

Look at the lines below:

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

Once modified, you must reload php-fpm.

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

Insert the following content into the file.

<?php
phpinfo();
?>

To make the changes take effect, restart Nginx.

sudo systemctl restart nginx

Use the URL http://IP address/info.php or http://domain name/info.php

How to Install LEMP stack on Rocky Linux 9|AlmaLinux 9
How to Install LEMP stack on Rocky Linux 9|AlmaLinux 9


You will find a comprehensive list of PHP and its extensions.

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

Nginx allows users to run several sites by configuring their virtual host file to point to their site.

In this guide, I will give a demonstration of how to create and configure a simple server block for Nginx.

Begin by creating the site directory:

sudo mkdir /var/www/example.tutornix.com

Now create a simple HTML script for your site:

$ sudo vim /var/www/example.tutornix.com/index.html
<html>
  <head>
    <title>Welcome to Tutornix.com!</title>
  </head>
  <body>
    <h1>The LEMP server is working perfectly!Voila! </h1>
  </body>
</html>

Then create a virtual host file to point to your site:

sudo vim /etc/nginx/conf.d/example.tutornix.com.conf

Add the below lines to the file:

server {
    listen  80;

    server_name example.tutornix.com www.example.tutornix.com;

    location / {
        root  /var/www/example.tutornix.com;
        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;
    }
}

Save the file and restart nginx:

sudo systemctl restart nginx

You may need to modify your /etc/hosts to accommodate your domain name:

$ sudo vim /etc/hosts
192.168.100.10 example.tutornix.com

Now try accessing your site with the URL http://domain_name.

How to Install LEMP stack on Rocky Linux 9|AlmaLinux 9
How to Install LEMP stack on Rocky Linux 9|AlmaLinux 9

The end!

Conclusion

You have successfully installed the LEMP stack on Rocky Linux 9 or AlmaLinux 9 after reading this guide. Cheers!

You might find these other guides interesting:

- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here