How to install LEMP stack on AlmaLinux

A LEMP stack is an assortment of software that contains everything you need in order to serve a website, show dynamic content, and store or retrieve data from a database. The software is all within the LEMP acronym, namely the Linux operating system, NGINX web server, MySQL database (or MariaDB alternatively), and PHP programming language.

If you’ve installed AlmaLinux or migrated from CentOS to AlmaLinux, then you already have the first requirement done. Next, you just need to get your LEMP stack up and running. In this guide, we’ll show the step by step instructions to install a LEMP stack on AlmaLinux.

Note that it’s also possible to install a LAMP stack on AlmaLinux, which is very similar to LEMP except that it contains Apache as the web server instead of NGINX.

In this tutorial you will learn:

  • How to install all LEMP prerequisite packages on AlmaLinux
  • How to secure MariaDB database
  • How to start NGINX and MariaDB services
  • How to open HTTP and HTTPS firewall ports
LEMP stack running successfully on AlmaLinux

LEMP stack running successfully on AlmaLinux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System AlmaLinux
Software LEMP (NGINX, MySQL/MariaDB, PHP)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install and configure LEMP packages on AlmaLinux

Follow along with the steps below to get NGINX, MariaDB, and PHP setup and configured on AlmaLinux.

For this guide, we’ll be installing MariaDB instead of the official MySQL server package. MariaDB is an open source implementation of MySQL and works exactly the same, but it has a few more features. If you don’t want to install MariaDB, feel free to install MySQL instead.
  1. Execute the following command to install NGINX, MariaDB, PHP, and some additional PHP modules with the dnf package manager.
    # dnf install nginx mariadb-server php-mysqlnd php-fpm

  2. If you have the default firewalld enabled on AlmaLinux, you’ll have to open up ports 80 and 443 for incoming HTTP and HTTPS connections to your web server.
    # firewall-cmd --permanent --zone=public --add-service=http 
    # firewall-cmd --permanent --zone=public --add-service=https
    # firewall-cmd --reload
  3. Start both NGINX webserver and MariaDB services:
    # systemctl start mariadb
    # systemctl start nginx

    Enable MariaDB and NGINX to start after system reboot:

    # systemctl enable mariadb
    # systemctl enable nginx
  4. Execute the following command to secure your MariaDB installation and set the root password. There will be a few prompts to go through. It’s recommended that you disable remote root login when the question shows up, unless you have some circumstance that requires it.
    # mysql_secure_installation
  5. To confirm that our web server is accessible and that PHP is working as expected, we can create a file called info.php inside the /usr/share/nginx/html directory. The file should contain the following line, which verifies whether PHP is working or not.
    <?php phpinfo(); ?>
  6. Change permissions of the web server directory, and change the SELinux security context.
    # chown -R nginx:nginx //usr/share/nginx/html/*
    # chcon -t httpd_sys_rw_content_t /usr/share/nginx/html/ -R

  7. In your browser, navigate to the test page we’ve created by opening the URL at http://localhost/info.php. You should see a result like the one in the screenshot below.
  8. LEMP stack running successfully on AlmaLinux

    LEMP stack running successfully on AlmaLinux

  9. So far, we have just installed a bare bones LEMP stack. Depending on the application you are going to use, you might also need to install additional PHP modules. To see a list of modules that are available for installation from the system’s package manager, execute the following command in terminal.
    # dnf search php-

    Then, to install an additional package, execute:

    # dnf install PACKAGENAME

    Once the package is installed, reload the NGINX service for the changes to take effect:

    # systemctl reload nginx

Closing Thoughts

In this guide, we saw how to install and configure a LEMP stack on AlmaLinux. This involved installing the individual software packages, namely NGINX, MariaDB, and PHP. Your system is now ready to serve and store dynamic web content.