How to Install LAMP Stack with MariaDB on Ubuntu 24.04

Install LAMP stack on Ubuntu 24.04 with Apache, PHP 8.3-FPM, MariaDB, UFW, and a safe health check for a working web server in ten minutes.
A polished 3D illustration of an Ubuntu server connecting Apache, PHP, and a MariaDB database.

Install LAMP stack on Ubuntu 24.04 and you will have Apache serving web requests, PHP 8.3 running through PHP-FPM, and MariaDB ready for application data. On a fresh VPS, the base setup takes about ten minutes and includes UFW rules plus a disposable PHP health check.

This guide uses Ubuntu 24.04 LTS packages instead of a third-party PHP repository. The database stays local to the server, which is the safer default for a website running on the same machine.

Prerequisites

  • A 64-bit Ubuntu 24.04 LTS 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 IPv4 address for testing Apache and outbound access to Ubuntu package mirrors.
  • A domain such as YOUR_DOMAIN is optional for installation, but required for a production 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 connected as root, remove sudo from each command.

Step 1: Confirm Ubuntu 24.04 and update the VPS

Checking the release first prevents you from applying package instructions meant for another Ubuntu version, while the full upgrade installs current security updates.

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 identify Noble Numbat and a 64-bit architecture. A package version may differ as Ubuntu publishes updates.

PRETTY_NAME="Ubuntu 24.04 LTS"
VERSION_ID="24.04"
VERSION_CODENAME=noble
x86_64

If the upgrade installs a new kernel, keep this SSH connection open until you have confirmed that a reboot is safe.

Step 2: Install Apache and open web ports

Apache is the public-facing web server, and UFW limits inbound traffic to SSH and the HTTP(S) ports a website needs.

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

Add the SSH rule before enabling UFW. If SSH listens on a custom port, allow that port instead of the OpenSSH profile. The checks should show Syntax OK, an active Apache service, and an active firewall.

Test Apache from the server before testing its public address. A successful local response proves that the service is listening even if a provider firewall or DNS setting is still wrong.

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

Step 3: Install PHP 8.3-FPM and connect it to Apache

Ubuntu 24.04 provides PHP 8.3 in its native repositories; PHP-FPM keeps PHP workers separate from Apache and works with Apache’s event-based request handling. The PHP-FPM manual explains the process manager used here.

Install the PHP runtime, the Apache connector, and extensions commonly needed by database-backed applications.

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

The first line of the version check should start with PHP 8.3.

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

Enable Apache’s FastCGI modules, load Ubuntu’s PHP-FPM configuration, and start the matching service at boot.

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

Create a small endpoint that returns a fixed result rather than exposing server settings with phpinfo(). Remove it as soon as the check succeeds.

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

Step 4: Install and secure MariaDB

MariaDB stores application data, while a separate local account lets the website work without exposing the database service 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 is interactive. On a new install, press Enter when it asks for an empty current root password if that is what your server uses; remove anonymous users, disallow remote root login, remove the test database, and reload the privilege tables. Keep Unix-socket authentication enabled when the script offers it. MariaDB documents these choices in its official hardening reference.

Create one database and an application user restricted to that database. Replace YOUR_DB_PASSWORD with a long, unique password before running the block, and do not reuse your SSH password.

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 command should return a row named lampdb. Do not add a UFW rule for port 3306: a local PHP application can connect over the local socket or loopback interface.

Verify the LAMP stack

These checks confirm that Apache, PHP-FPM, and MariaDB are running, enabled for the next boot, and configured to handle PHP requests.

sudo systemctl is-active apache2 php8.3-fpm mariadb
sudo systemctl is-enabled apache2 php8.3-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 an active UFW policy.

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

Troubleshooting

E: Unable to locate package php8.3-fpm

The package index may be stale, or the machine may not be using Ubuntu 24.04 repositories. Confirm the release and refresh APT before trying the install again.

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

Do not mix Noble repositories with another Ubuntu release. That can produce incompatible PHP libraries and a broken Apache module.

Unit php8.3-fpm.service not found

The FPM package was not installed successfully, so systemd has no matching unit to start. Install the package, enable the Apache configuration, and reload Apache.

sudo apt install -y php8.3-fpm
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.3-fpm
sudo systemctl enable --now php8.3-fpm
sudo systemctl reload apache2

Apache returns 503 Service Unavailable for PHP

PHP-FPM may be stopped, or Apache may not have its FastCGI configuration enabled. Check the service log and the expected Unix socket before restarting the two services.

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

If the socket is missing, fix the FPM service error first. Do not expose the FPM socket or port to the public network.

SSH stopped working after UFW was enabled

The firewall was activated without a rule for the actual SSH port. Open VPS control panel (Virtualizor) → VNC console from the VPSLake client area, allow the port, and then test a second SSH connection.

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

Replace YOUR_SSH_PORT with the numeric port shown by ss. Keep the existing session open until the new connection works.

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 bound to local access and leave port 3306 closed unless a separate host truly needs database access; if it does, restrict the source IP and use encrypted connections.
  • Keep SSH key authentication enabled, remove unused UFW rules, and review the policy with the VPSLake UFW firewall generator.
  • Back up /var/www and MariaDB separately, then apply Ubuntu security updates regularly.

FAQ

Which PHP version comes with Ubuntu 24.04?

Ubuntu 24.04’s standard repositories provide the PHP 8.3 package series. This guide names the versioned FPM service so Apache and systemd use the same runtime rather than whichever PHP version happens to be selected by a future package change.

Is libapache2-mod-php required for this installation?

No. Apache forwards PHP requests to php8.3-fpm through proxy_fcgi, so the embedded mod_php module is not part of this setup. That keeps the PHP worker pool independently restartable.

Should I open port 3306 for WordPress or another PHP website?

No, not when the application and MariaDB share the VPS. The application can use localhost, while a remote database connection should be a deliberate exception restricted by source IP and encryption.

Can I use this LAMP stack without a domain?

Yes. Apache and the local health check work with the server IP address. Add the domain later, configure the virtual host, and enable TLS after DNS resolves to the VPS.

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

Remote Desktop Black Screen: 9 Fixes That Work

Next Article

Rust Server Requirements: RAM, CPU and Hosting Cost

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 ✨