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

Install lamp stack on Rocky Linux 9 with Apache, PHP 8.3, MariaDB, firewalld, SELinux-safe defaults, and a working PHP test on a fresh VPS today.
A polished 3D illustration of a Rocky Linux server, web gateway, PHP processor, and database cylinder connected by blue and orange light trails.

Install lamp stack on Rocky Linux 9 and you will have Apache serving web pages, PHP 8.3 handling dynamic requests, and MariaDB ready for an application database. On a fresh VPS, the setup takes about ten minutes and includes firewalld rules plus a disposable PHP health check.

Rocky Linux 9 uses DNF modules for alternate application versions. This guide selects the PHP 8.3 AppStream stream, keeps PHP behind PHP-FPM, and leaves SELinux enabled rather than weakening the server to make a test site work.

Prerequisites

  • A 64-bit Rocky Linux 9 VPS with at least 2 GB of RAM and 10 GB of free disk space.
  • SSH access as root, or a regular account that can run sudo.
  • A public IPv4 address and outbound access to Rocky Linux BaseOS and AppStream repositories.
  • A domain is optional while installing the stack. You need YOUR_DOMAIN later for a named Apache virtual host and a trusted TLS certificate.
  • A VPSLake Linux VPS if you need a fresh Rocky Linux server with root access.

The commands assume a non-root SSH session. If you are already logged in as root, remove sudo from each command.

Step 1: Confirm Rocky Linux 9 and apply updates

Checking the release first prevents an EL8 or EL10 package instruction from being applied to this server, while the update installs current security fixes.

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

The release should identify Rocky Linux 9 and the architecture should normally be x86_64. The exact minor release and package revisions change as Rocky publishes updates, so do not copy a version number from an old install log.

PRETTY_NAME="Rocky Linux 9.x (Blue Onyx)"
VERSION_ID="9.x"
ID_LIKE="rhel fedora"
x86_64

If the upgrade installs a new kernel, keep the current SSH connection open until you have confirmed that the VPS can reboot and accept a new connection.

Step 2: Install Apache and permit web traffic

Apache handles HTTP requests, while firewalld limits public access to SSH and the two web ports used by an HTTP-to-HTTPS deployment.

sudo dnf install -y httpd firewalld curl
sudo systemctl enable --now firewalld httpd
sudo firewall-cmd --permanent --zone=public --add-service=ssh
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 firewalld
sudo firewall-cmd --zone=public --list-services

Add the SSH rule before reloading firewalld so a normal SSH connection remains allowed. If SSH listens on a custom port, add that port with --add-port=YOUR_SSH_PORT/tcp and replace the placeholder with the numeric port.

Put a small index file in the document root before testing. A local response confirms that Apache is listening even when a provider firewall, DNS record, or browser is the real source of a later connectivity problem.

printf '%s\n' 'Rocky Linux 9 Apache is running.' | sudo tee /var/www/html/index.html > /dev/null
sudo apachectl configtest
curl -fsSI http://127.0.0.1/
Syntax OK
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8

Step 3: Install PHP 8.3 with PHP-FPM

Rocky Linux 9.6 and later 9.x updates provide PHP 8.3 as an AppStream module. PHP-FPM runs the interpreter separately from Apache, which gives PHP requests a clear service boundary and avoids older mod_php instructions.

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 php-zip
sudo systemctl enable --now php-fpm httpd
php -v
sudo systemctl is-active php-fpm httpd

The version line should begin with PHP 8.3, and both units should report active. The PHP-FPM documentation describes the process manager used for these requests. Rocky’s PHP and PHP-FPM guide covers the distribution package layout.

Create a short-lived endpoint that returns a fixed value. This proves Apache can pass a .php request to FPM without publishing the detailed configuration that phpinfo() would reveal.

printf '%s\n' '<?php echo "PHP OK"; ?>' | 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. Remove the endpoint immediately after the test; diagnostic files do not belong in a public document root.

PHP OK

Step 4: Install MariaDB and create an application database

MariaDB provides the SQL layer, and a user restricted to localhost lets a website on this VPS connect without exposing port 3306 to the Internet.

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

The security utility is interactive. Remove anonymous accounts, reject remote root login, remove the test database, and reload the privilege tables when prompted. Keeping local administrative access through the Unix socket is a sensible default; an application should have its own database account.

Create a database and a separate login for the application. Replace YOUR_DB_PASSWORD with a long, unique secret before running the block; the placeholder is not a password to keep.

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

The final query should return one row named lampdb. Keep the real password in your application’s secret configuration, not in /var/www/html or a shell history file.

Verify the LAMP stack

Run these checks together to confirm that Apache, PHP-FPM, firewalld, and MariaDB are active and enabled at boot, PHP has the MariaDB connector, and the web server answers locally.

sudo systemctl is-active httpd php-fpm firewalld mariadb
sudo systemctl is-enabled httpd php-fpm firewalld 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/
sudo firewall-cmd --zone=public --list-services

You should see five active results, five enabled results, Syntax OK, PHP module names, a MariaDB version, an HTTP success status, and ssh http https in the firewalld service list.

active
active
active
active
active
enabled
enabled
enabled
enabled
enabled
Syntax OK
HTTP/1.1 200 OK
ssh http https

Troubleshooting

No matching module streams for php:8.3

The server may be on an older Rocky 9 minor release, or DNF’s AppStream metadata may be stale. Bring the system current and refresh the repository metadata before considering any third-party source.

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

The list should include the 8.3 stream on a current Rocky Linux 9 system. Do not enable a PHP stream from a different Enterprise Linux major version.

503 Service Unavailable when opening a PHP page

PHP-FPM may be stopped, or Apache may still have a stale configuration after the package installation. Check the service log and the FPM socket, then restart both services.

sudo systemctl status php-fpm --no-pager
sudo journalctl -u php-fpm -n 40 --no-pager
sudo test -S /run/php-fpm/www.sock && echo "FPM socket present"
sudo apachectl configtest
sudo systemctl restart php-fpm httpd

If the socket is absent, resolve the PHP-FPM error shown by journalctl before restarting again. A successful restart should make the temporary health.php test return PHP OK.

403 Forbidden after changing files in /var/www/html

Apache can be denied by an incorrect SELinux label or by unreadable file permissions. Restore the standard web-content context and use conservative modes instead of disabling SELinux.

sudo restorecon -Rv /var/www/html
sudo find /var/www/html -type d -exec chmod 0755 {} +
sudo find /var/www/html -type f -exec chmod 0644 {} +
sudo systemctl reload httpd

For uploads, label only the directory that truly needs Apache write access using the appropriate SELinux policy. Do not make the whole document root world-writable.

Access denied for user 'lampuser'@'localhost'

The application password may not match the value used when the account was created, or the application may be connecting with a different host name. Test the account directly over the local database socket.

mariadb -u lampuser -p lampdb -e "SELECT 1;"

Enter the real password. If it is wrong, reset it from the administrative socket and update the application’s secret at the same time.

sudo mariadb -e "ALTER USER 'lampuser'@'localhost' IDENTIFIED BY 'NEW_DB_PASSWORD';"

Hardening

  • Keep port 3306 closed. A local PHP application can use localhost or MariaDB’s Unix socket without public database access.
  • Point YOUR_DOMAIN to the VPS, configure an Apache virtual host, and issue a trusted TLS certificate before accepting logins or personal data.
  • Keep SSH key authentication enabled, remove unused firewalld services, and review the final rules before opening any additional port.
  • Remove test files, back up /var/www/html and the MariaDB databases, and apply Rocky Linux security updates regularly.

FAQ

Which PHP version does Rocky Linux 9 use here?

This guide selects the PHP 8.3 AppStream module available in current Rocky Linux 9 updates. Patch releases change through normal DNF updates, so the command checks the installed major and minor version rather than pinning one RPM build.

Why use PHP-FPM instead of loading PHP directly into Apache?

PHP-FPM keeps the interpreter in its own managed service and is the current Enterprise Linux approach for Apache PHP requests. It also makes service status and troubleshooting clearer than mixing an older mod_php configuration with FPM.

Should I open MariaDB port 3306 for a website?

No. A website running on this VPS can connect through localhost, so public database access is unnecessary. If a separate server must connect, restrict the source address and configure encrypted database traffic before adding a firewall rule.

Can I finish the installation without a domain name?

Yes. Apache and the PHP health check work with 127.0.0.1 or the VPS IP. Add the domain later, create the virtual host, and obtain TLS after DNS points to the server.

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

MT4 vs MT5 for Expert Advisors in 2026

Next Article

How to Install LAMP Stack on Ubuntu 22.04

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 ✨