How To Install LEMP Stack on AlmaLinux 8

In this article, we will teach you how to install LEMP Stack on AlmaLinux 8. LEMP stack stands for Linux, Nginx, MySQL, and PHP. It is a group of software that can be used to serve dynamic web pages and web applications written in PHP.

In this article, we use MariaDB instead of MySQL. You can now proceed to the guide steps below on the Orcacore website to complete LEMP Stack setup on AlmaLinux 8.

How To Install LEMP Stack on AlmaLinux 8?

To install the LEMP stack on AlmaLinux 8, you need some requirements.

You need to log in to your server as a non-root user and set up a basic firewall. To do this, you can check our article about the Initial server setup with AlmaLinux 8.

1. Installing Nginx on AlmaLinux 8

To install Nginx, you need to update and upgrade DNF packages with the following command:

sudo dnf update -y && sudo dnf upgrade -y

Then, install Nginx with the following command:

sudo dnf install nginx -y

After your installation is completed, run the command below to enable and start Nginx on AlmaLinux 8:

sudo systemctl start nginx

At this point, we assumed that you are done with the requirements for setting up a basic firewall.

Now you need to allow connections to Nginx on AlmaLinux 8. Enable HTTP connections, which run on port 80, with the following command:

sudo firewall-cmd --permanent --add-service=http

Verify the changes with the following command:

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

In your output, you will see:

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

Here, you need to reload the firewall configuration by using the following command:

sudo firewall-cmd --reload

Now you can test that your Nginx web server is up and running. To do this, you need your server’s public IP address.

You can get your public IP address with the following command:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Or, you can use the curl tool to get your IP:

sudo curl -4 icanhazip.com

Then, type your IP address in your web browser to access Nginx’s default landing page.

http://your_server_IP_address
Nginx default land page on AlmaLinux 8-LEMP stack

If you see this page, it means that your service is installed correctly.

At this point, the installation of Nginx in the LEMP stack on AlmaLinux 8 is finished. Let’s start to install MariaDB.

2. Installing MariaDB on AlmaLinux 8

Here, you need to install a database system to be able to store and manage data for your site.

Install MariaDB on AlmaLinux 8 with the following command:

sudo dnf install mariadb-server -y

When the installation of MariaDB is finished, enable and start the service with the following command:

sudo systemctl start mariadb

At this step, it’s recommended to run a security script that comes pre-installed with MariaDB to improve the security of your database server.

Run the following command:

sudo mysql_secure_installation

You will be asked some questions. The first prompt will ask you to enter the current database root password. Because you just installed MariaDB and haven’t made any configuration changes yet, just press Enter to leave it blank.

The next question is to set up a new root password. You don’t need to set this now. Type “N” and press Enter to continue.

From here, you can press ‘Y’ and press enter to accept the defaults for all the subsequent questions.

How to use MariaDB? Create a Database

When you’re finished with mysql_secure_installation, log in to the MariaDB console on AlmaLinux 8 with the following command:

sudo mysql

In your output, you will see:

Output
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
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)]>

For increased security, it’s best to have dedicated user accounts with less extensive privileges set up for every database, especially if you plan on having multiple databases hosted on your server.

To create a new database, run the following command from your MariaDB console:

You can replace the name of the database with your own.

MariaDB [(none)]> CREATE DATABASE orca_database;

    You can create a new user and give them full privileges on the database that you have created with the following command:

    MariaDB [(none)]> GRANT ALL ON orca_database.* TO 'olivia'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

    Remember to replace the name of the database and user and choose a secure password.

    Now you need to flush the privileges to ensure that they are saved and available in the current session:

    MariaDB [(none)]> FLUSH PRIVILEGES;

    Exit the MariaDB shell with the following command:

    MariaDB [(none)]> EXIT;

    Now you can test that the new user has the right permissions. Log in to the MariaDB console again using the custom user with the following command:

    sudo mysql -u olivia -p

    The -p option is for the password you set before for the user.

    Confirm that you have access to the orca_database database:

    MariaDB [(none)]> SHOW DATABASES;

      You will see:

      Output
      +--------------------+
      | Database |
      +--------------------+
      | information_schema |
      | orca_database |
      +--------------------+
      2 rows in set (0.003 sec)

      Then, exit from the MariaDB console with:

      MariaDB [(none)]> EXIT;

      At this point, your database system is set up, and you can move on to installing PHP, the final component of the LEMP Stack setup on AlmaLinux 8.

      3. Install PHP on AlmaLinux 8

      At this point, you can install PHP to process code and generate dynamic content for the web server.

      You need to install the php-fpm and php-mysql packages with the following command:

      sudo dnf install php-fpm php-mysqlnd -y

      Now you need to open the /etc/php-fpm.d/www.conf configuration file with your favorite text editor. Here we use the vi text editor:

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

      Find the user and group lines and change their value to Nginx:

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

      When you are finished, save and close the file with “:wq”.

      Enable and start the php-fpm service with the following command:

      sudo systemctl start php-fpm

      Now, set ownership of the HTML directory with the $USER environmental variable by the following command:

      sudo chown -R $USER:$USER /usr/share/nginx/html/

      You can create a test PHP page to make sure that your web server is working correctly.

      To do this, you need to create a new PHP file named info.php in /usr/share/nginx/html/ directory.

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

      Add the following PHP code to the file:

      <?php
      phpinfo();

      You can type your server name or IP address in your web browser, followed by /info.php:

      http://server_host_or_IP/info.php

      You should see a page similar to this:

      PHP test page on AlmaLinux 8

      After you check your PHP test page, it’s better to remove it. You can use the following command:

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

      Here, the final component of the LEMP stack on AlmaLinux 8 is finished.

      Also, you can test database connections from PHP.

      How to test the database Connection from PHP?

      You can create a test table with dummy data and query for its contents from a PHP script.

      First, connect to the MariaDB console with your database user that you have created before.

      sudo mysql -u olivia -p

      Then create a table named todolist with the following command:

      MariaDB [(none)]> CREATE TABLE orca_database.todolist (
          item_id INT AUTO_INCREMENT,
          content VARCHAR(255),
          PRIMARY KEY(item_id)
      );

      Now, insert a few rows of content in the test table. You can repeat this command a few times.

      MariaDB [(none)]> INSERT INTO orca_database.todolist (content) VALUES ("My first important item");

      To see that your data is saved successfully in your table, run the command below:

      MariaDB [(none)]> SELECT * FROM orca_database.todolist;

      In your output, you will see:

      Output
      +---------+----------------------------+
      | item_id | content |
      +---------+----------------------------+
      | 1 | My first important item |
      | 2 | My second important item |
      | 3 | and this is one more thing |
      +---------+----------------------------+
      3 rows in set (0.002 sec)

      Exit from the MariaDB console:

      MariaDB [(none)]> EXIT;

      Now you can create the PHP script that will connect to MariaDB and query for your content. Run the command below:

      sudo vi /usr/share/nginx/html/todolist.php

      Add the following PHP script to the file:

      <?php
      $user = "olivia";
      $password = "password";
      $database = "orca_database";
      $table = "todolist";
      
      try {
      $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
      echo "<h2>TODO</h2><ol>";
      foreach($db->query("SELECT content FROM $table") as $row) {
      echo "<li>" . $row['content'] . "</li>";
      }
      echo "</ol>";
      } catch (PDOException $e) {
      print "Error!: " . $e->getMessage() . "<br/>";
      die();
      }

      Remember to change the values of the user, password, database, and table with your own.

      When you are done, save and close your file.

      You can now access this page in your web browser by visiting your server’s hostname or public IP address, followed by /todolist.php:

      http://server_host_or_IP/todolist.php

      You will see something similar to this:

      PHP todolist on AlmaLinux 8

      If you see this means that the PHP environment is ready to connect and interact with your MariaDB server. That’s it, you are done with the LEMP Stack setup on AlmaLinux 8.

      Conclusion 

      At this point, you have learned to install Nginx as your web server, MariaDB database to store your data, and PHP for dynamic processing on AlmaLinux 8.

      Hope you enjoy this article about How To Install LEMP Stack on AlmaLinux 8. Please subscribe to us on Facebook, X, and YouTube.

      You may also like to read the following articles:

      Install Nginx Web Server on AlmaLinux 10

      Install LEMP Stack with Docker Compose on Debian/Ubuntu

      Check Nginx Version Installed on Linux

      Run Nginx in a Docker Container on AlmaLinux

      Share your love

      Stay informed and not overwhelmed, subscribe now!