We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Install Apache, MariaDB, PHP (LAMP) on Rocky Linux 9

Learn how to – Install Apache, MariaDB, PHP (LAMP) on Rocky Linux 9.

For general web development and hosting, you need a Programming language, a database, and a web server. The LAMP stack is a collection of open-source Linux tools that include Apache, MySQL/MariaDB, and PHP. These tools work closely together to enable a server to host and run modern and dynamic web applications.

The components of the LAMP stack are well elaborated below:

  • Apache: This is an open-source cross-platform web server that runs 67% of all websites in the world. This software is developed and maintained by the Apache Software Foundation.
  • MariaDB: This is a relational database management system developed by the original developers of MySQL and guaranteed to stay open source.
  • PHP: This is an abbreviation of Hypertext Pre-processor. This is an open-source general scripting language that serves as a backend programming language for Web applications. Moreso, this language can also be used with HTML when creating web applications. The main difference between PHP and other languages such as JavaScript is that the codes are executed on the server, and HTML scripts are generated and sent to the client.

How does LAMP Stack work?

The Apache web server receives requests for web pages for the browser. If a PHP file is requested, then it passes the request to PHP which then loads and executes the code contained in the file. PHP also links with the database(MySQL/MariaDB) to obtain any referenced data in the code.

The PHP code and data fetched from the database are then used to generate HTML scripts that are used to display the web pages. The LAMP stack is effective when handling both static and dynamic web pages where data changes from time to time when loaded.

PHP then passes these results back to the Apache Web server which then sends them back to the browser.

In this guide, we will install and use Apache, MariaDB, and PHP (LAMP) on Rocky Linux 9.

#1. Install Apache on Rocky Linux 9

The first component to be installed here is the Apache Web server. This software is provided as httpd in the Rocky Linux repositories. To install it, execute the command:

sudo dnf install httpd

Dependency Tree:

Transaction Summary
Install  10 Packages

Total download size: 1.9 M
Installed size: 5.9 M
Is this ok [y/N]: y

Once installed, start and enable the service:

sudo systemctl enable --now httpd

Check the status of the service:

$ 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 Tue 2022-07-19 11:29:32 CEST; 10s ago
       Docs: man:httpd.service(8)
   Main PID: 2287 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
      Tasks: 213 (limit: 23441)
     Memory: 35.0M
        CPU: 64ms
     CGroup: /system.slice/httpd.service
             ├─2287 /usr/sbin/httpd -DFOREGROUND
             ├─2288 /usr/sbin/httpd -DFOREGROUND
             ├─2289 /usr/sbin/httpd -DFOREGROUND
             ├─2290 /usr/sbin/httpd -DFOREGROUND
             └─2291 /usr/sbin/httpd -DFOREGROUND

You can also verify if Apache is running by accessing the default page on a browser. First, allow the HTTP traffic through the firewall:

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

Access the web page using the URL http://IP_Address or http://domain_name

#2. Install MariaDB on Rocky Linux 9

We need a database server for web hosting. There are two options here, MySQL or the MariaDB database. In this guide, we will opt for the MariaDB database due to its high-performance storage engines, faster and safer replication, and compatibility with MySQL.

Rocky Linux 9 provides MariaDB 10 in the default repository. This can be installed using the command:

sudo dnf install mariadb-server mariadb

Once installed, start and enable the MariaDB service:

sudo systemctl enable --now mariadb

Verify if the service is running:

$ systemctl status mariadb
● mariadb.service - MariaDB 10.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
     Active: active (running) since Tue 2022-07-19 11:31:24 CEST; 1s ago
       Docs: man:mariadbd(8)
    Process: 3609 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)
    Process: 3631 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)
    Process: 3724 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)
   Main PID: 3711 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 12 (limit: 23441)
     Memory: 70.8M
        CPU: 254ms
     CGroup: /system.slice/mariadb.service
             └─3711 /usr/libexec/mariadbd --basedir=/usr

By default, the MariaDB is not secure and this might cause vulnerabilities. To secure it, proceed as shown:

$ sudo mysql_secure_installation
Enter current password for root (enter for none): Just press Enter
Switch to unix_socket authentication [Y/n] Y
Change the root password? [Y/n] Y
New password:  New-root-password
Re-enter new password: Re-enter New-root-password
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
Thanks for using MariaDB!

Verify if you can log in using the created password:

$ 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.13-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.5.13-MariaDB |
1 row in set (0.000 sec) 

MariaDB [(none)]> QUIT

#3. Install PHP on Rocky Linux 9

The default PHP version available in the Rocky Linux 9 repositories is PHP 8.0. This version comes with bug fixes and enhancements, including the use of structured metadata syntax, newly named arguments that are order-independent, and improved performance for Just-In-Time compilation.

Install PHP 8.0 and its dependencies using the command:

sudo dnf install php-{common,gmp,fpm,curl,intl,pdo,mbstring,gd,xml,cli,zip,mysqli}

Check the PVH version:

$ php --version
PHP 8.0.13 (cli) (built: Nov 16 2021 18:07:21) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.13, Copyright (c) Zend Technologies

It is also possible to install other PHP versions. This can be done by adding the Remi and EPEL repositories:

sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm 

Once added, list the PHP versions available:

$ sudo dnf module list php
Last metadata expiration check: 0:43:34 ago on Tue 19 Jul 2022 01:42:06 PM CEST.
Remi's Modular repository for Enterprise Linux 9 - x86_64
Name            Stream                  Profiles                             Summary                         
php             remi-7.4[e]          common [d], devel, minimal           PHP scripting language          
php             remi-8.0               common [d], devel, minimal           PHP scripting language          
php             remi-8.1               common [d], devel, minimal           PHP scripting language          

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

Enable and install the preferred version, say PHP 8.1 as shown

sudo dnf module enable php:remi-8.1 -y

Now install the PHP and its dependencies:

sudo dnf install php-{common,gmp,fpm,curl,intl,pdo,mbstring,gd,xml,cli,zip,mysqli}

Verify the installation.

$ php --version
PHP 8.1.8 (cli) (built: Jul  5 2022 21:55:55) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.8, Copyright (c), by Zend Technologies

#4. Configure PHP for Apache on Rocky Linux 9

To configure PHP, open the file below:

sudo vi /etc/php.ini

In the file, make the desired settings. For example:

max_execution_time = 300
upload_max_filesize = 100M
post_max_size = 128M
date.timezone = Africa/Nairobi

After making the desired changes, allow Apache to load PHP files:

$ sudo vi /etc/httpd/conf/httpd.conf
# LoadModule foo_module modules/mod_foo.so
AddHandler php-script .php

You can also check the PHP version by creating a test PHP file using the command:

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

Add the below lines to the file:


Save the file and restart Apache and PHP-FPM:

sudo systemctl restart php-fpm
sudo systemctl restart httpd

Access the page that displays the PHP parameters using the URL http://IP_Address/info.php

Delete test script:

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

#5. Creating Apache Virtual Host file for your site

For you to be able to access a site via the browser, you need to create a virtual host file for the site. In this guide, we will configure Apache to serve a sample page as shown.

Create the virtual host file:

sudo vi /etc/httpd/conf.d/yourdomain.conf

In the file, add the lines below:

<VirtualHost *:80>
    ServerName test.example.com
    DocumentRoot /var/www/html/
    ErrorLog /var/log/httpd/error.log
    CustomLog /var/log/httpd/requests.log combined

Create the file in the document root specified:

sudo vi /var/www/html/index.html

Add the lines:

    <title>This is a test page</title>
    <h1>LAMP Stack is working perfectly on Rocky Linux 9!</h1>

Restart Apache:

sudo systemctl restart httpd

Now access the web page using the URL http://IP_address or http://domain_name

Remove test page:

sudo rm /var/www/html/index.html

#6. Host PHP Application with Database Connectivity

To test the better working of the LAMP stack, we will configure the PHP Websites to connect to the MySQL database. The below steps are used.

Begin by creating a database.

mysql -u root -p

Create a database with the below table

USE db_contact;
CREATE TABLE tbl_contact (Id INT,fldName VARCHAR(50), fldEmail VARCHAR(30), fldPhone VARCHAR(15),fldMessage VARCHAR(1000));

Create an HTML form for connecting to the database:

sudo mkdir /var/www/html/contacts
sudo vi /var/www/html/contacts/contact.html

Add the below lines to the file:

<!DOCTYPE html>
<html >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Form - PHP/MySQL Demo Code</title>

<legend>Contact Form</legend>
<form name="frmContact" method="post" action="contact.php">
<label for="Name">Name </label>
<input type="text" name="txtName" id="txtName">
<label for="email">Email</label>
<input type="text" name="txtEmail" id="txtEmail">
<label for="phone">Phone</label>
<input type="text" name="txtPhone" id="txtPhone">
<label for="message">Message</label>
<textarea name="txtMessage" id="txtMessage"></textarea>
<input type="submit" name="Submit" id="Submit" value="Submit">

Now create a PHP script to save data from HTML form to your database:

sudo vi /var/www/html/contacts/contact.php

Add the below lines to it:

// database connection code
// $con = mysqli_connect('localhost', 'database_user', 'database_password','database');

$con = mysqli_connect('localhost', 'root', 'root_password','db_contact');

// get the post records
$txtName = $_POST['txtName'];
$txtEmail = $_POST['txtEmail'];
$txtPhone = $_POST['txtPhone'];
$txtMessage = $_POST['txtMessage'];

// database insert SQL code
$sql = "INSERT INTO `tbl_contact` (`Id`, `fldName`, `fldEmail`, `fldPhone`, `fldMessage`) VALUES ('0', '$txtName', '$txtEmail', '$txtPhone', '$txtMessage')";

// insert in database 
$rs = mysqli_query($con, $sql);

	echo "Contact Records Inserted";


Remember to replace the root_password and db_contact with the database name. Once the files have been saved, load the page using the URL http://domain_name/contacts/contact.html

Fill in the form and submit the changes

Verify if data has been added to the database:

$ mysql -u root -p
Enter password: 
MariaDB [(none)]> SELECT * FROM  db_contact. tbl_contact;

Sample Output:

That is it!. We can now clean up the test:

sudo rm -rf /var/www/html/contacts

Recommended books to read:

Closing Thoughts

That marks the end of this guide on how to install Apache, MariaDB, and PHP (LAMP) on Rocky Linux 9. I hope this was significant to you.

See more:


Your Cart is Empty

Back To Shop