How to Install LAMP Stack with MariaDB on Ubuntu 26.04

Install LAMP stack with MariaDB on Ubuntu 26.04 using Apache, PHP 8.5-FPM, UFW, and a secure database setup for a working, secure VPS in minutes.
A polished 3D illustration of an Ubuntu server, web server node, firewall shield, and glowing database cylinder.

Install LAMP stack with MariaDB on Ubuntu 26.04 and you will have Apache serving web requests, PHP 8.5 processing dynamic pages, and MariaDB storing application data. This setup takes about ten minutes on a clean VPS and includes a firewall, a local database account, and a safe PHP check.

Prerequisites

  • A 64-bit Ubuntu 26.04 LTS (Resolute) VPS with at least 1 GB of RAM and 10 GB of free disk space.
  • SSH access as root or a normal user with working sudo privileges.
  • A public IP address and outbound access to the Ubuntu package repositories.
  • A domain such as YOUR_DOMAIN is optional for this initial install, but required for a trusted TLS certificate.
  • A VPSLake Linux VPS with root access if you need a server for this stack.

The commands below use sudo. If you are already logged in as root, remove sudo from each command. Keep your current SSH session open while enabling UFW.

Step 1: Confirm Ubuntu 26.04 and update packages

Checking the release first prevents you from installing packages or service names from a different Ubuntu version.

grep -E '^(PRETTY_NAME|VERSION_ID|VERSION_CODENAME)=' /etc/os-release
uname -m
sudo apt update
sudo apt full-upgrade -y

The release check should show VERSION_ID="26.04", the resolute codename, and normally x86_64 on an AMD64 VPS.

PRETTY_NAME="Ubuntu 26.04 LTS"
VERSION_ID="26.04"
VERSION_CODENAME=resolute
x86_64

If apt update reports a failing third-party repository, disable that repository before continuing. Do not mix packages from another Ubuntu codename.

Step 2: Install Apache and allow web traffic

Apache handles HTTP requests, while UFW permits only the SSH and web traffic needed to administer and reach this server.

sudo apt install -y apache2 apache2-utils curl ufw
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
sudo systemctl enable --now apache2
sudo apachectl configtest
sudo systemctl is-active apache2
sudo ufw status verbose

Create the SSH rule before enabling UFW. If SSH uses a custom port, replace OpenSSH with YOUR_SSH_PORT/tcp, using the numeric port configured for sshd. The checks should show Syntax OK, active, and an active firewall. Ubuntu’s Apache2 installation guide explains the standard configuration layout.

Test Apache locally before testing the public IP. This confirms that the service works independently of DNS or a provider-level firewall.

curl -fsSI http://127.0.0.1/
HTTP/1.1 200 OK
Content-Type: text/html

You can also visit http://SERVER_IP/ in a browser. Replace SERVER_IP with the VPS address; the default Apache page means port 80 is reachable.

Step 3: Install PHP 8.5-FPM for Apache

Ubuntu 26.04 provides PHP 8.5 in its native repositories, and PHP-FPM lets Apache pass dynamic requests to a separate, restartable FastCGI service.

Install the runtime, MariaDB connector, and common extensions used by PHP applications.

sudo apt install -y php8.5-cli php8.5-fpm php8.5-mysql php8.5-curl php8.5-gd php8.5-mbstring php8.5-xml php8.5-zip php8.5-opcache
php --version

The first version line should begin with PHP 8.5.

PHP 8.5.x (cli) (built: ...)

Enable Apache’s FastCGI modules and the configuration that points Apache to the PHP 8.5 socket.

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.5-fpm
sudo systemctl enable --now php8.5-fpm
sudo systemctl reload apache2
sudo systemctl is-active php8.5-fpm

A short health endpoint proves that Apache is executing PHP without exposing the detailed server information shown 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 file is removed immediately after the check. Do not leave diagnostic PHP pages in a public document root.

Step 4: Install and secure MariaDB

MariaDB stores the application data; keeping its listener private allows PHP on this VPS to connect without exposing port 3306 to the Internet.

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

The security utility asks interactive questions. On a new installation, press Enter when it asks for an empty current root password if that is the authentication state shown on your server. Remove anonymous accounts, reject remote root login, remove the test database, and reload the privilege tables. MariaDB documents the choices in its official mariadb-secure-installation reference; Unix-socket authentication for the administrative account is normal on current packages.

Create a database and a separate local account for the application. Replace YOUR_DB_PASSWORD with a long, unique password before running this block. It is a placeholder, not a password to copy literally.

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 lampdb. Applications should use lampuser, not the MariaDB administrative account. Do not add a UFW rule for port 3306 when the website and database share this VPS.

Verify the LAMP stack

Run these checks together to confirm that all three services start at boot, PHP is connected to Apache, and MariaDB is answering local queries.

sudo systemctl is-active apache2 php8.5-fpm mariadb
sudo systemctl is-enabled apache2 php8.5-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/
sudo ufw status

Expect three active results, three enabled results, Syntax OK, PHP module names, a MariaDB version, an HTTP success response, and Status: active from UFW.

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

Troubleshooting

E: Unable to locate package php8.5-fpm

The package index may be stale, or the VPS may not be using the Resolute repositories. Confirm the release and inspect the package candidate before retrying.

grep -E '^(VERSION_ID|VERSION_CODENAME)=' /etc/os-release
sudo apt update
apt-cache policy php8.5-fpm
sudo apt install -y php8.5-fpm

The policy output should show a candidate for Ubuntu 26.04. Do not add a PHP repository or install packages built for another Ubuntu release to work around a source configuration error.

Apache returns 503 Service Unavailable for PHP

PHP-FPM may be stopped, or Apache may not have loaded the matching FastCGI configuration. Check the unit, log, and socket before restarting services.

sudo systemctl status php8.5-fpm --no-pager
sudo journalctl -u php8.5-fpm -n 40 --no-pager
ls -l /run/php/php8.5-fpm.sock
sudo apachectl configtest
sudo systemctl restart php8.5-fpm apache2

If the socket is missing, fix the first PHP-FPM error in the journal. Never expose the FPM socket or a FPM TCP port to the public network.

PHP code appears as plain text in the browser

Apache is serving the file but is not passing .php requests to PHP-FPM. Re-enable the proxy modules and version-specific configuration, then reload Apache.

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.5-fpm
sudo apachectl configtest
sudo systemctl reload apache2

SSH stops working after UFW is enabled

The firewall rule did not match the port used by sshd. Open VPS control panel (Virtualizor) → VNC console from the VPSLake client area, allow the actual SSH port, and keep the current console open while testing another connection.

sudo ss -ltnp | grep ssh
sudo ufw allow YOUR_SSH_PORT/tcp
sudo ufw status numbered

Replace YOUR_SSH_PORT with the numeric listening port. Remove an obsolete rule only after a new SSH session succeeds.

Hardening

  • Point YOUR_DOMAIN at the VPS, create an Apache virtual host, and issue a trusted TLS certificate before accepting passwords or personal data.
  • Keep MariaDB restricted to local access and port 3306 closed. If remote access is truly required, restrict it to a known source IP and use encrypted connections.
  • Keep SSH key authentication enabled, remove unused firewall rules, and review the policy with the VPSLake UFW firewall generator.
  • Back up /var/www and MariaDB separately, and apply Ubuntu security updates regularly.

FAQ

Which PHP version does Ubuntu 26.04 use?

The native Ubuntu 26.04 repositories provide the PHP 8.5 package series. This guide names php8.5-fpm explicitly so Apache, the systemd unit, and the FPM socket all refer to the same version.

Do I need to open MariaDB port 3306?

No. A PHP application on the same VPS can connect locally, so the database port should remain closed to the Internet. Open it only for a deliberate remote connection, restricted by source address and protected with encryption.

Can Apache and PHP-FPM run on a server without a domain?

Yes. Apache can serve its default site through the VPS IP, and the local health check does not require DNS. Add a domain later, create a virtual host, and enable TLS after the DNS record resolves to the server.

Why is the PHP health file deleted immediately?

A dedicated health response confirms that PHP execution works without revealing configuration, paths, or extensions. Leaving even a harmless test endpoint online creates an unnecessary public URL that can be forgotten during later maintenance.

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

How to Install LAMP Stack with MariaDB on Ubuntu 25.10

Next Article

RDP Session Timeout: Fix Idle Disconnects and Logoffs

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 ✨