How to Set Up a LAMP Stack on AlmaLinux/Rocky Linux

LAMP (Linux, Apache, MySQL/MariaDB, PHP) remains a common, well-understood stack for traditional web applications — this guide covers the complete setup specifically on AlmaLinux/Rocky Linux.

Step 1 — Install Apache (httpd)

sudo dnf install httpd -y
sudo systemctl enable --now httpd

See How to Install Apache (httpd) on AlmaLinux/Rocky Linux for more detail on this specific step.

Step 2 — Install MariaDB

sudo dnf install mariadb-server -y
sudo systemctl enable --now mariadb
sudo mysql_secure_installation

See How to Install MySQL/MariaDB on AlmaLinux/Rocky Linux for the complete database setup process.

Step 3 — Install PHP

sudo dnf install php php-mysqlnd php-fpm -y

See How to Install PHP on AlmaLinux/Rocky Linux for version-specific installation options via Remi's repository.

Step 4 — Configure Apache to Use PHP-FPM

sudo nano /etc/httpd/conf.d/php.conf
<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
</FilesMatch>

Step 5 — Start PHP-FPM

sudo systemctl enable --now php-fpm

Step 6 — Configure firewalld

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

Step 7 — Test PHP Processing

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
http://YOUR_SERVER_IP/info.php

Remove this test file after confirming it works — leaving phpinfo() accessible publicly exposes server configuration details unnecessarily.

Step 8 — Create a Database and User for Your Application

sudo mysql -u root -p
CREATE DATABASE myapp;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'CHANGE_ME';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;

SELinux Considerations for LAMP

See Understanding SELinux Basics on AlmaLinux/Rocky Linux — SELinux by default may restrict Apache/PHP from writing to certain directories or making outbound network connections (needed for some applications to reach external APIs); use setsebool for common web-server-related booleans rather than disabling SELinux outright.

Common SELinux Boolean for Web Applications

sudo setsebool -P httpd_can_network_connect on

Needed if your PHP application makes outbound HTTP requests (calling external APIs, for example) — without this, SELinux blocks such connections by default.

Adding HTTPS with Let's Encrypt

See How to Set Up Let's Encrypt SSL on AlmaLinux/Rocky Linux for completing the setup with proper TLS.

Common Errors

"403 Forbidden" despite correct file permissions — check SELinux context on your web files (ls -Z /var/www/html); files need the correct httpd_sys_content_t context, distinct from standard Unix file permissions alone.

Continue Reading

Browse more articles in AlmaLinux & Rocky Linux.

  • lamp stack almalinux, lamp stack rocky linux, apache mariadb php rhel, selinux httpd configuration
  • 0 Els usuaris han Trobat Això Útil
Ha estat útil la resposta?

Articles Relacionats

AlmaLinux vs Rocky Linux vs CentOS: What Happened to CentOS?

If you're choosing a RHEL-compatible Linux distribution for your VPS, understanding the CentOS...

How to Get Started with AlmaLinux/Rocky Linux on a VPS

This guide covers the essential first steps after deploying a new AlmaLinux or Rocky Linux VPS...

How to Use dnf: The Package Manager for AlmaLinux & Rocky Linux

dnf (Dandified YUM) is the package manager for AlmaLinux, Rocky Linux, and other RHEL-family...

How to Configure firewalld on AlmaLinux/Rocky Linux

firewalld is the default firewall management tool on AlmaLinux and Rocky Linux, using a...

How to Install Nginx on AlmaLinux/Rocky Linux

This guide covers installing and configuring Nginx on AlmaLinux or Rocky Linux, including the...