Install LAMP stack on Rocky Linux 8 and you will have Apache serving web pages, PHP 8.2 handling dynamic requests, and MariaDB 10.11 ready for an application database. Set aside about ten minutes for a fresh VPS; this guide also opens only the required web ports and checks the stack without leaving a diagnostic page online.
Rocky Linux 8 uses DNF modules for versioned application streams. The commands below select the current PHP and MariaDB streams available in Rocky 8.10 instead of accepting the old PHP 7.2 default.
Prerequisites
- A 64-bit Rocky Linux 8 VPS, preferably updated to Rocky Linux 8.10, 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 commands withsudo. - Outbound Internet access to the Rocky Linux BaseOS and AppStream repositories, plus a public IPv4 address for browser testing.
- A domain is optional while installing the stack. You need one before requesting a trusted TLS certificate for a production site.
- A VPSLake Linux VPS if you need a fresh Rocky Linux server with root access.
The examples assume a non-root SSH session. If you are logged in as root, remove sudo from the commands.
Step 1: Confirm Rocky Linux 8 and apply updates
Checking the release first prevents an EL9 or EL10 package instruction from being applied to this server, while the upgrade installs current security fixes.
grep -E '^(PRETTY_NAME|VERSION_ID|ID_LIKE)=' /etc/os-release
uname -m
sudo dnf upgrade -y
The first lines should identify Rocky Linux 8 and the architecture should normally be x86_64. Minor release and package versions change as updates are published, so do not copy a version number from an old package listing.
PRETTY_NAME="Rocky Linux 8.10 (Green Obsidian)"
VERSION_ID="8.10"
ID_LIKE="rhel fedora"
x86_64
If DNF installs a new kernel, keep this SSH connection open until you have confirmed that the VPS can reboot and accept a new connection.
Step 2: Install Apache and protect the web ports
Apache serves the document root, while firewalld limits incoming traffic to SSH, HTTP, and HTTPS instead of exposing every listening service.
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 SSH before reloading the permanent rules so a remote session remains reachable. If SSH uses a custom port, add that port with --add-port=YOUR_SSH_PORT/tcp and keep the current session open while testing a second connection.
active
active
ssh http https
Test Apache locally before blaming DNS or a provider-level firewall. A successful loopback response proves that the daemon is listening on the VPS.
sudo apachectl configtest
curl -fsSI http://127.0.0.1/
Syntax OK
HTTP/1.1 200 OK
Step 3: Enable PHP 8.2 and connect it to Apache
Rocky Linux 8.10 publishes PHP 8.2 in AppStream, but the default module stream is older. Selecting the stream explicitly gives Apache a current PHP branch without adding an untrusted repository; Rocky’s PHP documentation covers the available streams and Apache integration.
sudo dnf module reset php -y
sudo dnf module enable php:8.2 -y
sudo dnf install -y php php-cli php-mysqlnd php-gd php-curl php-zip php-mbstring php-xml php-opcache
php -v
sudo systemctl restart httpd
The first version line should begin with PHP 8.2. The php-mysqlnd package lets PHP applications use MariaDB’s MySQL-compatible protocol, while the other extensions cover common image, HTTP, archive, multibyte, XML, and opcode-cache needs.
Create a temporary endpoint that returns a fixed string. This tests PHP execution through Apache without publishing the full configuration details exposed by phpinfo().
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
PHP OK
The final command removes the test file immediately. Keep real application code and secrets outside any public test endpoint.
Step 4: Install MariaDB 10.11 and create an application database
MariaDB stores persistent application data, and a database account limited to localhost avoids exposing port 3306 to the Internet.
Rocky Linux 8.10 provides MariaDB 10.11 as an AppStream module. Reset the old selection before enabling the newer stream so DNF does not combine incompatible module metadata.
sudo dnf module reset mariadb -y
sudo dnf module enable mariadb:10.11 -y
sudo dnf install -y mariadb-server mariadb
sudo systemctl enable --now mariadb
sudo systemctl is-active mariadb
Secure the initial database installation with MariaDB’s supported hardening utility. The MariaDB secure-installation documentation explains each prompt.
sudo mariadb-secure-installation
When prompted, keep local administrative access through the Unix socket when offered, remove anonymous accounts, disallow remote root login, remove the test database, and reload the privilege tables. A local socket keeps administrative credentials out of a network listener.
Create a database and a separate login for the website. Replace YOUR_DB_PASSWORD with a long, unique password before running the block; the placeholder must not be used on a public server.
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 last command should return one row named lampdb. Give the application only this account’s database privileges; do not put the administrative MariaDB account in application configuration.
Verify the LAMP stack
Run the checks below after the temporary PHP file has been removed. Together they confirm that the services start at boot, Apache’s configuration parses, PHP has its database connector, MariaDB answers locally, and the firewall exposes the intended services.
sudo systemctl is-active firewalld httpd mariadb
sudo systemctl is-enabled firewalld httpd mariadb
sudo apachectl configtest
sudo httpd -M | grep php_module
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
Expect active and enabled for all three services, Syntax OK, a shared PHP module, PHP connector names, a MariaDB version, an HTTP success response, and ssh http https. From another computer, open http://SERVER_IP/ after replacing SERVER_IP with the VPS public address.
active
active
active
enabled
enabled
enabled
Syntax OK
php_module (shared)
HTTP/1.1 200 OK
ssh http https
Troubleshooting
No match for argument: php or PHP 7.2 installs
The AppStream metadata may be stale, or the PHP module stream was not selected. Confirm the OS and refresh the repositories before attempting another install.
grep -E '^(PRETTY_NAME|VERSION_ID)=' /etc/os-release
sudo dnf clean all
sudo dnf makecache
sudo dnf module reset php -y
sudo dnf module enable php:8.2 -y
sudo dnf install -y php php-cli php-mysqlnd
Do not enable an EL9 or EL10 module on Rocky Linux 8. Mixed major-release packages can leave Apache and PHP unable to load together.
PHP code appears as text in the browser
Apache is not loading the PHP module, or it was not restarted after PHP was installed. Check the module list and Apache syntax, then restart the service.
sudo httpd -M | grep php_module
sudo dnf reinstall -y php
sudo apachectl configtest
sudo systemctl restart httpd
If the module check remains empty, inspect sudo journalctl -u httpd -n 40 --no-pager for a package or configuration error before recreating the temporary health endpoint.
403 Forbidden after uploading site files
SELinux may have an incorrect context, or the files may not be readable by Apache. Restore the default labels and use ordinary web-readable modes; disabling SELinux is not a fix.
sudo restorecon -RFv /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
If the application needs uploads, label only its upload directory with the SELinux type it requires instead of making the entire document root writable.
Access denied for user 'lampuser'@'localhost'
The password in the application does not match the database account, or the application is connecting with a different host name. Test the credentials directly over the local connection.
mariadb -u lampuser -p lampdb -e "SELECT 1;"
Enter the real password, not YOUR_DB_PASSWORD. If it is wrong, reset it from the administrative socket and update the application secret at the same time.
sudo mariadb -e "ALTER USER 'lampuser'@'localhost' IDENTIFIED BY 'NEW_DB_PASSWORD';"
Hardening
- Keep port 3306 closed in firewalld. A local PHP application can use
localhostor MariaDB’s Unix socket without public database access. - Point
YOUR_DOMAINto the VPS, issue a trusted TLS certificate, and redirect HTTP to HTTPS before accepting logins or personal data. - Keep SELinux enforcing, remove temporary health files, and never store database passwords under
/var/www/html. - Back up
/var/www/htmland the MariaDB databases, then apply Rocky Linux updates regularly.
FAQ
Why does Rocky Linux 8 need a PHP module command?
Rocky Linux 8 uses AppStream module streams for software versions that can differ from the base operating system. The default PHP stream is older, so this guide selects PHP 8.2 before installing the packages. Rocky Linux 9 and 10 use different packaging behavior, so do not reuse their commands here.
Should I open port 3306 for a PHP website?
No. A website running on the same VPS connects to MariaDB through localhost or a Unix socket. Only consider remote database access when a separate host genuinely needs it, and then restrict the source address and require encrypted connections.
Can I use a domain name later?
Yes. Apache works with the public IP while you build the server. After DNS points YOUR_DOMAIN to the VPS, create a name-based virtual host and request a TLS certificate before exposing a login or other sensitive form.
Is PHP-FPM required for this Rocky Linux 8 setup?
No. Rocky’s PHP package can serve requests through Apache’s PHP module, which is sufficient for a basic single-site LAMP installation. Choose PHP-FPM later when you need separate pools, per-site process limits, or a larger multi-site configuration.