How to install Apache, MariaDB, PHP (LAMP) on Rocky Linux 9

LAMP stands for Linux, Apache, MySQL/MariaDB and PHP that work together to serve web content also called LAMP stack. Rocky Linux 9 is released and available for public consumption. In this article you will learn how to install Apache, MariaDB and PHP (LAMP) on Rocky Linux 9.

Apache installation

Before installation, update the system.

dnf update

After update, install Apache (httpd).

dnf install httpd nano

Start and enable httpd.

systemctl enable httpd
systemctl start httpd

Check it’s status.

systemctl status httpd

To check if a module is loaded, rewrite for example.

httpd -M | grep rewrite

Or check all the loaded modules.

httpd -M

Modules are loaded from /etc/httpd/conf.modules.d directory.


By default firewall is enabled on Rocky Linux, we will add firewalld rules to allow port 80, 443 etc.

firewall-cmd --zone=public --permanent --add-port 80/tcp
firewall-cmd --zone=public --permanent --add-port 443/tcp

firewall-cmd --reload

Check more info about firewall in the troubleshoot section.

MariaDB installation

Rocky Linux 9 comes with MariaDB 10.5, we will install that for now.

dnf install mariadb-server

Enable/start MariaDB.

systemctl enable mariadb
systemctl start mariadb

Check the status with.

systemctl status mariadb

Secure MariaDB server

MariaDB has a script which runs some pre configured commands to secure the database server.


Check the database server details.

mysqladmin -u root -p version

Install Redis

This is optional at this stage, but if you are hosting a web app like Nextcloud etc it is needed.

dnf install redis

Enable at boot time and start.

systemctl enable redis
systemctl start redis

Check it’s status.

systemctl status redis

Redis will be listening on port 6379 on localhost.

Install PHP

Before we install PHP, there are some packages (if you need them) available in EPEL repository which we will enable first.

dnf install epel-release

dnf update

Install some base packages for PHP modules.

dnf install ImageMagick zip unzip

Once done, you can now install PHP.

dnf install php php-gmp php-bcmath php-gd php-json php-mysqlnd php-curl php-intl php-zip php-apcu php-pear php-devel php-sodium

To install imagick, redis, memcache PHP packages from epel repository.

dnf install php-pecl-redis php-pecl-imagick php-pecl-memcache php-pecl-memcached

To check what PHP modules are loaded.

php -m

Next, create a index.php file in /var/www/html.

nano /var/www/html/index.php

Add the following in it.


Change owner and group permissions of html directory.

sudo chown -R apache:apache /var/www/html

And finally test and restart Apache.

httpd -t

systemctl restart httpd

To restart php-fpm if you have made any changes to the PHP configurations.

systemctl restart php-fpm

Open a browser and enter the server IP, you will see the following page.



How to remove the default page?

Web root is /var/www/html, adding index.php or index.html will overwrite the default page content.
If you still want to remove the default page, just comment out all lines in /etc/httpd/conf.d/welcome.conf.

How to remove a firewall rule?

Let’s say you want to remove port 80 rule. Remember to reload the firewall.
sudo firewall-cmd --zone=public --permanent --remove-port 80/tcp
sudo firewall-cmd --reload

How to check the firewall rules?

You can do so with sudo firewall-cmd --list-all

How to check SELinux and modify it?

sestatus will tell you the status of SELinux.
To change it’s status, modify it in sudo nano /etc/sysconfig/selinux.

Why I am getting 500 internal server error?

This maybe caused by SELinux, allow it to handle.
chcon -t httpd_sys_rw_content_t -R /var/www/html/