How to Install LAMP Stack on AlmaLinux 9 (Step-by-Step)

Install LAMP stack on AlmaLinux 9 with Apache, PHP 8.3, PHP-FPM, MariaDB, and firewalld, then test the complete web stack safely on a new VPS in minutes.
A polished 3D scene with an illuminated web server, PHP processor, database cylinder, and blue light trails.

Install LAMP stack on AlmaLinux 9 and you will have Apache serving pages, PHP 8.3 running through PHP-FPM, and MariaDB ready for an application in about ten minutes. This setup uses AlmaLinux repositories, firewalld, and SELinux-friendly permissions so the first deployment has a sensible security baseline.

Prerequisites

  • A 64-bit AlmaLinux 9 VPS with at least 2 GB of RAM and 10 GB of available disk space.
  • SSH access as root, or a regular account that can run commands with sudo.
  • A public IPv4 address for testing the web server. A domain is optional for the installation, but required for a trusted production TLS certificate.
  • Internet access to the AlmaLinux BaseOS and AppStream repositories.
  • A VPSLake Linux VPS if you need a fresh AlmaLinux server with root access.

The commands use sudo for a non-root account. If your SSH session is already root, remove sudo from each command.

Step 1: Confirm AlmaLinux 9 and update the server

Checking the release before installing packages prevents an EL8 or EL10 command set from being applied to this server, while the update brings the base system current.

grep -E '^(PRETTY_NAME|VERSION_ID|ID_LIKE)=' /etc/os-release
uname -m
sudo dnf upgrade -y

The release should identify AlmaLinux 9 and the architecture should normally be x86_64. AlmaLinux publishes minor releases and package updates independently, so do not rely on a fixed patch number in later commands.

PRETTY_NAME="AlmaLinux 9.x (...)"
VERSION_ID="9.x"
ID_LIKE="rhel centos fedora"
x86_64

If DNF installs a new kernel, keep this SSH connection open until you have confirmed that the VPS can reboot and reconnect.

Step 2: Install Apache and allow web traffic

Apache handles HTTP requests, and firewalld exposes only the web services needed by visitors instead of opening arbitrary ports.

sudo dnf install -y httpd firewalld
sudo systemctl enable --now firewalld httpd
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
sudo systemctl is-active httpd
sudo firewall-cmd --zone=public --list-services

The service check should return active, and the firewalld list should contain http and https. HTTPS is opened in advance so adding a certificate later does not require another firewall edit.

curl -I http://127.0.0.1

Apache should return an HTTP response, usually 200 OK for the default page.

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8

To test from another computer, replace SERVER_IP with the VPS public address:

curl -I http://SERVER_IP

Step 3: Install PHP 8.3 with PHP-FPM

AlmaLinux 9 provides PHP 8.3 as an AppStream module; using its common profile installs the web-oriented packages and avoids the removed mod_php approach used by older Enterprise Linux guides.

sudo dnf module reset php -y
sudo dnf module enable php:8.3 -y
sudo dnf module install php:8.3/common -y
sudo dnf install -y php-mysqlnd php-gd php-mbstring php-opcache php-xml php-curl php-intl
sudo systemctl enable --now php-fpm httpd
php -v
sudo systemctl is-active php-fpm httpd

The version should begin with PHP 8.3, and both services should report active. PHP-FPM is the process manager Apache uses for PHP requests; its configuration and behavior are described in the official PHP-FPM documentation.

Make a temporary endpoint that returns a fixed string. This proves PHP executes without publishing the detailed server information that phpinfo() would expose.

printf '%s\n' '<?php echo "PHP OK\\n"; ?>' | sudo tee /var/www/html/health.php > /dev/null
curl -fsS http://127.0.0.1/health.php
sudo rm -f /var/www/html/health.php

The response should be PHP OK. The final command removes the test file immediately; never leave diagnostic scripts in a public document root.

PHP OK

Step 4: Install MariaDB and create an application database

MariaDB supplies the SQL layer, while a database account limited to localhost lets a local website connect without making port 3306 public.

sudo dnf install -y mariadb-server mariadb
sudo systemctl enable --now mariadb
sudo systemctl is-active mariadb
sudo mariadb-secure-installation

The security tool is interactive. On a new installation, follow its prompts to remove anonymous accounts, reject remote root login, remove the test database, and reload the privilege tables. Keeping local administrative socket authentication is fine; an application should use its own database account instead. MariaDB documents the choices in its official mariadb-secure-installation reference.

Create a separate database and user for your application. Replace YOUR_DB_PASSWORD with a long, unique secret before running this block.

sudo mariadb <<'SQL'
CREATE DATABASE appdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'YOUR_DB_PASSWORD';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
SQL
sudo mariadb -e "SHOW DATABASES LIKE 'appdb';"

The final query should return one row named appdb. Store the real password in your application’s secret configuration, not in /var/www/html.

Verify the LAMP stack

Run this final set of checks to confirm that all services are running at boot, Apache accepts its configuration, PHP has the MariaDB connector, and the web server responds locally.

sudo systemctl is-active httpd php-fpm mariadb
sudo systemctl is-enabled httpd php-fpm mariadb
sudo apachectl configtest
php -m | grep -E 'mysqli|mysqlnd|PDO|mbstring|xml'
sudo mariadb -e "SELECT VERSION();"
curl -fsSI http://127.0.0.1/

Expect three active results, three enabled results, Syntax OK, PHP module names, a MariaDB version, and a successful HTTP status.

active
active
active
enabled
enabled
enabled
Syntax OK
HTTP/1.1 200 OK

Troubleshooting

No matching module streams for php:8.3

The AppStream metadata may be stale, or the machine may be an older AlmaLinux 9 minor release that has not been updated. Confirm the release and refresh repository metadata before changing package sources.

grep -E '^(PRETTY_NAME|VERSION_ID)=' /etc/os-release
sudo dnf clean all
sudo dnf makecache
sudo dnf module list php

After the refresh, the list should show the 8.3 stream on a current AlmaLinux 9 system. Do not install an EL8 or EL10 repository on this server.

503 Service Unavailable when opening a PHP page

Apache cannot reach PHP-FPM, commonly because the service failed to start or a configuration file has an error. Inspect the unit log and Apache syntax before restarting both services.

sudo systemctl status php-fpm --no-pager
sudo journalctl -u php-fpm -n 40 --no-pager
sudo apachectl configtest
sudo systemctl restart php-fpm httpd

If the log reports a PHP configuration error, correct that line and rerun apachectl configtest. A successful test prints Syntax OK.

AH00558: Could not reliably determine the server's fully qualified domain name

This warning means Apache has no global ServerName; it does not usually stop the service, but it makes diagnostics noisy. Add a local configuration file using the server IP or fully qualified domain name, then validate and reload Apache.

printf '%s\n' 'ServerName SERVER_IP' | sudo tee /etc/httpd/conf.d/servername.conf > /dev/null
sudo apachectl configtest
sudo systemctl reload httpd

Replace SERVER_IP with the actual address or use YOUR_DOMAIN after DNS points to this VPS. ServerName is not a substitute for a TLS certificate.

Access denied for user 'appuser'@'localhost'

The application password does not match the value used when the account was created, or the account exists for another host name. Test the same local account directly before changing its privileges.

mariadb -u appuser -p appdb -e "SELECT 1;"

Enter the real password, not the literal placeholder. If the test fails, reset the password from the administrative socket with sudo mariadb, then update the application’s configuration as well.

Hardening

  • Keep port 3306 closed in firewalld. A site on this VPS can reach MariaDB through localhost or the Unix socket.
  • Point YOUR_DOMAIN to the VPS, install a trusted TLS certificate, and redirect HTTP to HTTPS before collecting credentials or personal data.
  • Restore SELinux labels after copying site files with sudo restorecon -Rv /var/www/html; do not disable SELinux to hide a labeling problem.
  • Remove health checks and unused sample files, update AlmaLinux regularly, and back up both the web root and MariaDB data.

FAQ

Does AlmaLinux 9 include PHP 8.3?

Yes. Current AlmaLinux 9 AppStream repositories provide PHP 8.3 as a module stream. Enabling the stream explicitly makes the intended PHP branch clear and avoids silently selecting the older base package.

Why does this guide use PHP-FPM instead of mod_php?

Enterprise Linux 9 removed the Apache mod_php module. PHP-FPM runs PHP as a FastCGI service, which Apache can pass PHP requests to without loading the interpreter directly into its worker process.

Should I expose MariaDB to the Internet?

Not for a website hosted on the same VPS. The local application account works without a public database port; only configure remote access when a separate host genuinely needs it, and then restrict the source address and require encrypted connections.

Can I use a domain later?

Yes. You can install and test the stack with the server IP first. When the domain’s DNS record points to the VPS, create a virtual host, issue a TLS certificate, and update the Apache ServerName to the domain.

Samrat Ghosh
Written by

Samrat Ghosh

Founder & Infrastructure Engineer

Samrat Ghosh is the founder of VPSLake, where he builds and runs the remote desktop and VPS hosting infrastructure the company is built on. He writes hands-on guides about RDP, Windows Server, VPS management and secure remote access - the practical documentation he wishes had…

Previous Article

SQL Server on VPS: Express Limits and Sizing

Next Article

Remote Desktop Can't Connect? A 10-Step Checklist

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨