LAMP is an acronym for Linux, Apache, MySQL and PHP software stack. This combination of software pieces are open-source and they are used together to build powerful web applications and websites. Linux is the operating system where Apache, MySQL/MariaDB and PHP applications run. Apache is the web server and MySQL is a database management application. PHP (Hypertext Pre-processor) is the backend programming language used in building web applications.

In this article we will dive into the steps required to install and run LAMP stack on Rocky Linux 8 | AlmaLinux 8. If you done similar installations on CentOS 8 or Oracle Linux 8 the steps will be relatable. Both Rocky Linux / AlmaLinux 8 Linux distributions are meant to replace CentOS 8 demise whose direction was hijacked.

Below are the steps that can be followed to install and configure LAMP (Apache, MySQL/MariaDB, and PHP) on Rocky Linux 8 and AlmaLinux 8. This setup is done on the server edition but should also work on Desktop installations of Rocky and AlmaLinux.

Step 1: Install Apache httpd Web server

We start with the installation of httpd package which is available in the OS Appstream repository. Use the following command to install Apache httpd web server on Rocky Linux 8 / AlmaLinux 8 Linux distribution.

sudo dnf install -y @httpd

After package installation which pulls all required dependencies start and enable the service.

sudo systemctl enable --now httpd

Confirm if the service is started without any errors:

$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-05-08 21:00:25 EAT; 22s ago
     Docs: man:httpd.service(8)
 Main PID: 64115 (httpd)
   Status: "Running, listening on: port 443, port 80"
    Tasks: 213 (limit: 23412)
   Memory: 27.0M
   CGroup: /system.slice/httpd.service
           ├─64115 /usr/sbin/httpd -DFOREGROUND
           ├─64116 /usr/sbin/httpd -DFOREGROUND
           ├─64117 /usr/sbin/httpd -DFOREGROUND
           ├─64118 /usr/sbin/httpd -DFOREGROUND
           └─64119 /usr/sbin/httpd -DFOREGROUND

May 08 21:00:24 systemd[1]: Starting The Apache HTTP Server...
May 08 21:00:25 systemd[1]: Started The Apache HTTP Server.
May 08 21:00:25 httpd[64115]: Server configured, listening on: port 443, port 80

If firewalld is enabled consider allowing http and https services:

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

You can verify httpd web server is running on http://<your-server-ip>.

Step 2: Install PHP on Rocky Linux 8 | AlmaLinux 8

We now install PHP using commands similar to ones used for httpd. But AppStream repository has multiple versions of PHP as can be seen from below command output:

$ sudo dnf module list php
Last metadata expiration check: 0:31:48 ago on Sat 08 May 2021 08:31:42 PM EAT.
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

You can reset default module for PHP:

sudo dnf module reset php

Then enable the module for the version you intent to install:

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

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

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

Accept install prompts using y or yes:

Dependencies resolved.
 Package                              Architecture               Version                                                      Repository                     Size
Installing group/module packages:
 php-cli                              x86_64                     7.4.6-4.module+el8.3.0+202+dc8a8639                          appstream                     3.1 M
 php-common                           x86_64                     7.4.6-4.module+el8.3.0+202+dc8a8639                          appstream                     695 k
 php-fpm                              x86_64                     7.4.6-4.module+el8.3.0+202+dc8a8639                          appstream                     1.6 M
 php-json                             x86_64                     7.4.6-4.module+el8.3.0+202+dc8a8639                          appstream                      73 k
 php-mbstring                         x86_64                     7.4.6-4.module+el8.3.0+202+dc8a8639                          appstream                     483 k
 php-xml                              x86_64                     7.4.6-4.module+el8.3.0+202+dc8a8639                          appstream                     172 k
Installing dependencies:
 nginx-filesystem                     noarch                     1:1.14.1-9.module+el8.3.0+121+6327f1ce                       appstream                      23 k
Installing module profiles:
Enabling module streams:
 nginx                                                           1.14
 php                                                             7.4

Transaction Summary
Install  7 Packages

Total download size: 6.1 M
Installed size: 27 M
Is this ok [y/N]: y

Install basic PHP extensions as well:

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

Check PHP version:

$  php --version
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

RHEL 8 based systems use php-fpm instead of mod_php. Let’s start php-fpm service.

sudo systemctl enable --now php-fpm

Verify that php-fpm is running by checking on its status.

sudo systemctl status php-fpm

The output should be as shown:

Restart httpd service:

sudo systemctl restart httpd

Create a test PHP file called info.php under the directory /var/www/html

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

Paste below contents into the file:


Now on your browser, open http://<your-server-ip>/info.php and the output should be as below:

For PHP 8 installation use the guide:

Step 3: Install MariaDB server on Rocky Linux / AlmaLinux 8

The last software component we install is MariaDB/MySQL database. Execute below commands in your system terminal to install MariaDB.

sudo dnf -y install @mariadb

Once MariaDB is installed start the service and enable it to start on system boot.

sudo systemctl enable --now mariadb

Secure your database server.

$ sudo mysql_secure_installation


In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

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!

Use mysql command to connect to the database server:

$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
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)]> SELECT VERSION();
| VERSION()       |
| 10.3.28-MariaDB |
1 row in set (0.000 sec)

MariaDB [(none)]> QUIT

If you’ll prefer MySQL Database server refer to our guide in below link:

Install MySQL 8 on Rocky Linux 8 | AlmaLinux 8

Step 4: Create Apache virtual host for your site

Finally test your LAMP stack installation on Rocky Linux 8 | AlmaLinux 8 by creating sample apache virtual host.

sudo vim /etc/httpd/conf.d/

Add the following content:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ErrorLog /var/www/
    CustomLog /var/www/ combined

Now let’s create a demo web page

sudo mkdir /var/www/
sudo vim /var/www/

Add the following contents

    <title>This is a test page</title>
    <h1>It works!</h1>

Save the file and let Apache own the root directory then restart httpd.

sudo chown -R apache:apache /var/www/*
sudo chmod -R 755 /var/www
sudo systemctl restart httpd

Save the server IP and matching domain in your local system /etc/hosts file:

$ sudo vim /etc/hosts

Open your browser on configured domain in Virtual host file. If set correctly, you should see the content from your demo html file.

This is the end of our guide on the installation of LAMP Stack on Rocky Linux 8 | AlmaLinux 8. Below are the other guides available in our website:

Install PostgreSQL 13 on Rocky Linux 8 | AlmaLinux 8

Install MySQL 8 on Rocky Linux 8 | AlmaLinux 8