How to Install LAMP Stack Setup On RHEL Rocky Ubuntu Alma Centos Linux: Step-by-Step Guide
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.
cat /etc/redhat-release
ip addr show
hostname -I
sudo dnf repolist
Update installed packages:
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.
sudo dnf install httpd -y
sudo systemctl enable --now httpd
sudo systemctl status httpd
sudo apachectl configtest
active (running), and the configuration test should return Syntax OK.Check whether Apache is listening on port 80:
sudo ss -lntp | grep ':80'
4. Configure the Firewall
RHEL uses firewalld to manage host firewall rules. Check its status first:
sudo systemctl status firewalld
If firewalld is running, allow HTTP traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services
5. Test Apache Web Server
Create a simple HTML page in the default web root:
echo '<h1>Welcome to My RHEL LAMP Server</h1>' | sudo tee /var/www/html/index.html
Test the page locally:
curl http://localhost
Expected output:
<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.
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:
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.
8. Create a Database and Application User
Open the MariaDB console:
sudo mariadb
Create a database named lampdb:
CREATE DATABASE lampdb;
Create a dedicated application user. Replace the example password with a strong, unique password.
CREATE USER 'lampuser'@'localhost'
IDENTIFIED BY 'ReplaceWithAStrongUniquePassword!';
Grant access only to the application database:
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.
sudo dnf install php php-fpm php-mysqlnd -y
php -v
php -m
Enable and start PHP-FPM, then restart Apache:
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:
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.
sudo rm -f /var/www/html/info.php
11. Create a PHP Website
Create the homepage:
sudo vi /var/www/html/index.php
Add the following code:
<!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:
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
$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.
- Apache: Working
- PHP: Working
- MariaDB: Connected successfully
If the connection fails, verify the database username, password, database name, and installed PHP MySQL driver.
13. Verify the Complete LAMP Stack
Check the status of all three primary services:
systemctl is-active httpd
systemctl is-active php-fpm
systemctl is-active mariadb
Expected output:
active
active
active
Check the installed packages:
rpm -q httpd mariadb-server php php-fpm php-mysqlnd
Check listening ports:
sudo ss -lntp
14. Common LAMP Stack Troubleshooting
| Problem | Command or action |
|---|---|
| Apache fails to start | sudo journalctl -u httpd -n 50 --no-pager |
| Website is unreachable | sudo firewall-cmd --list-services |
| Port 80 is not listening | sudo ss -lntp | grep ':80' |
| PHP is not executing | Check Apache PHP integration and PHP-FPM status. |
| MariaDB fails to start | sudo journalctl -u mariadb -n 50 --no-pager |
| Database connection fails | php -m | grep -Ei 'PDO|mysql' |
| PHP-FPM errors | sudo journalctl -u php-fpm -n 50 --no-pager |
Check Apache's error log:
sudo tail -n 50 /var/log/httpd/error_log
Check PHP-FPM logs:
sudo journalctl -u php-fpm -n 50 --no-pager
Check 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.