How to Install LAMP Stack Setup On RHEL Rocky Ubuntu Alma Centos Linux: Step-by-Step Guide

Linux Administration / RHEL 9 / Apache / MariaDB / PHP

In this practical Linux administration tutorial, we will install and configure a complete LAMP Stack on Red Hat Enterprise Linux (RHEL) Rocky Ubuntu Alma Centos . You will learn how to configure Apache Web Server, MariaDB, PHP, PHP-FPM, firewall rules, and database connectivity.

This guide is designed for Linux System Administrators, RHCSA learners, and anyone who wants hands-on experience deploying a Linux-based web server.

LAMP Stack Architecture

Operating System: Red Hat Enterprise Linux 9

Web Server: Apache HTTP Server (httpd)

Database: MariaDB Server

Application Language: PHP

PHP Process Manager: PHP-FPM

HTTP Port: 80

HTTPS Port: 443, after TLS is configured

Web Root: /var/www/html


Section Number Section Topic
1 Prerequisites
2 Update RHEL 9
3 Install Apache
4 Configure the firewall
5 Test Apache
6 Install MariaDB
7 Secure MariaDB
8 Create a database and user
9 Install PHP and PHP-FPM
10 Test PHP
11 Create a PHP website
12 Connect PHP to MariaDB
13 Verify the installation
14 Troubleshooting
15 Security best practices
16 Conclusion

1. Prerequisites

Before starting, make sure you have:

  • A working RHEL 9 server or virtual machine.
  • A user account with sudo privileges.
  • Internet connectivity and access to the required software repositories.
  • A reachable server IP address.
  • At least 2 GB RAM recommended for a small practice lab.

2. Update RHEL 9

First, verify your operating system version and network configuration.

bash — system information
cat /etc/redhat-release
ip addr show
hostname -I
sudo dnf repolist

Update installed packages:

bash — system update
sudo dnf update -y

If DNF cannot find the required packages, check RHEL registration, repository availability, DNS resolution, and internet access.

3. Install Apache Web Server

Apache receives HTTP requests from browsers and serves website content. On RHEL, its service and package name is httpd.

bash — install Apache
sudo dnf install httpd -y
sudo systemctl enable --now httpd
sudo systemctl status httpd
sudo apachectl configtest
Expected result: The service should report active (running), and the configuration test should return Syntax OK.

Check whether Apache is listening on port 80:

bash — verify HTTP listener
sudo ss -lntp | grep ':80'

4. Configure the Firewall

RHEL uses firewalld to manage host firewall rules. Check its status first:

bash — firewall status
sudo systemctl status firewalld

If firewalld is running, allow HTTP traffic:

bash — allow HTTP and HTTPS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services
Important: Adding the HTTPS firewall service does not configure HTTPS itself. You must also configure Apache TLS and a valid certificate. If your server is hosted in a cloud environment, check its security group or external firewall too.

5. Test Apache Web Server

Create a simple HTML page in the default web root:

bash — create test page
echo '<h1>Welcome to My RHEL LAMP Server</h1>' | sudo tee /var/www/html/index.html

Test the page locally:

bash — local HTTP test
curl http://localhost

Expected output:

output — HTML response
<h1>Welcome to My RHEL LAMP Server</h1>

Open http://YOUR_SERVER_IP in a browser and replace YOUR_SERVER_IP with your actual server IP address.

6. Install MariaDB Database Server

MariaDB is a relational database server used to store application data such as user accounts, website posts, and application records.

bash — install MariaDB
sudo dnf install mariadb-server -y
sudo systemctl enable --now mariadb
sudo systemctl status mariadb
mariadb --version

Check that the MariaDB service reports active (running).

7. Secure MariaDB

Run the security setup utility provided by your installed MariaDB package:

bash — database security setup
sudo mariadb-secure-installation

Follow the prompts appropriate to your installed version. Where applicable, remove anonymous accounts, restrict remote root login, and remove the test database.

Security note: MariaDB should not be exposed to the public internet for this lab. Keep database access local unless remote access is specifically required and secured.

8. Create a Database and Application User

Open the MariaDB console:

bash — database console
sudo mariadb

Create a database named lampdb:

sql — create database
CREATE DATABASE lampdb;

Create a dedicated application user. Replace the example password with a strong, unique password.

sql — create application user
CREATE USER 'lampuser'@'localhost'
IDENTIFIED BY 'ReplaceWithAStrongUniquePassword!';

Grant access only to the application database:

sql — grant database permissions
GRANT ALL PRIVILEGES ON lampdb.*
TO 'lampuser'@'localhost';

SHOW DATABASES;
EXIT;

The application account now has permissions on lampdb, rather than administrative access to every database.

9. Install PHP and PHP-FPM

PHP executes server-side application code. PHP-FPM manages PHP worker processes and communicates with Apache through FastCGI.

bash — install PHP packages
sudo dnf install php php-fpm php-mysqlnd -y
php -v
php -m

Enable and start PHP-FPM, then restart Apache:

bash — start PHP-FPM
sudo systemctl enable --now php-fpm
sudo systemctl restart httpd

sudo systemctl status php-fpm
sudo systemctl status httpd

RHEL package versions and enabled repositories can affect which PHP version is installed. Use the packages available from your configured repositories.

10. Test PHP Execution

Create a temporary PHP information page:

bash — create PHP test file
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php

Visit http://YOUR_SERVER_IP/info.php. If the PHP information page appears, Apache is executing PHP successfully.

Critical: The PHP information page exposes environment details. Remove it immediately after testing.
bash — remove PHP test page
sudo rm -f /var/www/html/info.php

11. Create a PHP Website

Create the homepage:

bash — create homepage
sudo vi /var/www/html/index.php

Add the following code:

php — homepage
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RHEL LAMP Stack</title>
</head>
<body>
    <h1>Welcome to My LAMP Server</h1>
    <p>Apache and PHP are working on RHEL 9.</p>

    <?php
    echo "<p>Server time: " . date("Y-m-d H:i:s") . "</p>";
    ?>
</body>
</html>

Save the file and visit http://YOUR_SERVER_IP/index.php. You should see the welcome message and current server time.

12. Connect PHP to MariaDB

We will now test whether PHP can connect to the MariaDB database using PDO and the MySQL driver.

Open the homepage file again:

bash — edit PHP application
sudo vi /var/www/html/index.php

Replace its contents with this lab example. Enter the same database password you configured in Step 8.

php — database connectivity test
<?php
$host = "localhost";
$dbname = "lampdb";
$username = "lampuser";
$password = "ReplaceWithAStrongUniquePassword!";

try {
    $pdo = new PDO(
        "mysql:host=$host;dbname=$dbname;charset=utf8mb4",
        $username,
        $password,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ]
    );

    echo "<h1>LAMP Stack Installed Successfully!</h1>";
    echo "<p>Apache: Working</p>";
    echo "<p>PHP: Working</p>";
    echo "<p>MariaDB: Connected successfully</p>";

} catch (PDOException $e) {
    http_response_code(500);
    error_log($e->getMessage());
    echo "Database connection failed. Check the server logs.";
}
?>

Open http://YOUR_SERVER_IP/index.php in your browser.

Expected result
  • Apache: Working
  • PHP: Working
  • MariaDB: Connected successfully

If the connection fails, verify the database username, password, database name, and installed PHP MySQL driver.

Production security: This code is intended for a lab. Do not keep database credentials directly in a web-accessible script. Store secrets in a protected configuration file or an appropriate secrets-management system, and restrict access to that configuration.

13. Verify the Complete LAMP Stack

Check the status of all three primary services:

bash — service verification
systemctl is-active httpd
systemctl is-active php-fpm
systemctl is-active mariadb

Expected output:

output — service status
active
active
active

Check the installed packages:

bash — installed packages
rpm -q httpd mariadb-server php php-fpm php-mysqlnd

Check listening ports:

bash — network listeners
sudo ss -lntp

14. Common LAMP Stack Troubleshooting

ProblemCommand or action
Apache fails to startsudo journalctl -u httpd -n 50 --no-pager
Website is unreachablesudo firewall-cmd --list-services
Port 80 is not listeningsudo ss -lntp | grep ':80'
PHP is not executingCheck Apache PHP integration and PHP-FPM status.
MariaDB fails to startsudo journalctl -u mariadb -n 50 --no-pager
Database connection failsphp -m | grep -Ei 'PDO|mysql'
PHP-FPM errorssudo journalctl -u php-fpm -n 50 --no-pager

Check Apache's error log:

bash — Apache error log
sudo tail -n 50 /var/log/httpd/error_log

Check PHP-FPM logs:

bash — PHP-FPM logs
sudo journalctl -u php-fpm -n 50 --no-pager

Check MariaDB logs:

bash — MariaDB logs
sudo journalctl -u mariadb -n 50 --no-pager

If the issue persists, investigate SELinux audit messages, file contexts, DNS, routing, and any external firewall rules. Keep SELinux enabled rather than disabling it to bypass errors.

15. Security Best Practices

  • Keep RHEL and application packages updated.
  • Use HTTPS with a valid TLS certificate.
  • Keep MariaDB inaccessible from public networks unless remote access is required and properly secured.
  • Use dedicated database accounts with minimum necessary privileges.
  • Remove PHP test pages such as info.php.
  • Never publish database credentials in source code or Git repositories.
  • Keep SELinux enabled and investigate access denials correctly.
  • Back up website files and databases regularly.
  • Test restoration procedures, not just backup creation.
  • Review service logs and firewall rules periodically.

16. Conclusion

In this tutorial, we installed and configured a LAMP Stack on RHEL 9 using Apache, MariaDB, PHP, and PHP-FPM. We configured firewall rules, created a database and dedicated application user, tested PHP execution, and verified database connectivity.

This practical project helps Linux System Administrators develop real-world skills in web server administration, database management, service troubleshooting, and basic security hardening.

Practice these steps in a test environment before deploying a production website.