How to Install LAMP Stack on Ubuntu 22.04

Install LAMP stack on Ubuntu 22.04 with Apache, MariaDB, PHP-FPM, UFW, and a safe health check for a secure web server you can deploy today.
A polished 3D illustration of an Ubuntu server, web gateway, PHP processor, and MariaDB cylinder connected by glowing blue and orange trails.

Install LAMP stack on Ubuntu 22.04 with Apache, MariaDB, PHP 8.1, PHP-FPM, and a UFW firewall in about ten minutes. You will finish with a web server, a local application database account, and a temporary health check that proves PHP is executing.

Prerequisites

  • A fresh 64-bit Ubuntu 22.04 (Jammy Jellyfish) VPS with at least 2 GB of RAM and 10 GB of free disk space.
  • SSH access as root, or a normal user that can run commands with sudo.
  • A public IP address and outbound access to Ubuntu’s package repositories.
  • A domain is optional for this base setup, but you need one before configuring trusted HTTPS for a production website.
  • A VPSLake Linux VPS if you need a root-access Ubuntu server.

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

Step 1: Confirm Ubuntu 22.04 and apply updates

Checking the release first keeps Jammy-specific PHP and service commands from being used on another 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 should show Ubuntu 22.04 with the codename jammy; a typical VPS reports x86_64 for its architecture. The exact package patch levels will change as Ubuntu publishes updates.

PRETTY_NAME="Ubuntu 22.04.x LTS"
VERSION_ID="22.04"
VERSION_CODENAME=jammy
x86_64

If the upgrade installs a new kernel, keep this SSH session open until you know the VPS can reboot and accept a new connection.

Step 2: Install Apache and protect web traffic

Apache serves the site, while UFW limits public access to SSH and the two standard web ports.

sudo apt install -y apache2 apache2-utils curl ufw
sudo systemctl enable --now apache2
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
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 relying on the OpenSSH application profile. The service check should print active, and the firewall should list SSH, port 80, and port 443.

Test Apache locally before troubleshooting DNS or a provider-level firewall.

sudo apachectl configtest
curl -fsSI http://127.0.0.1/
Syntax OK
HTTP/1.1 200 OK

Step 3: Install and secure MariaDB

MariaDB provides the SQL database layer; keeping its application account on localhost avoids exposing database traffic 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 script is interactive. On a new Ubuntu installation, press Enter if it asks for an existing root password, keep Unix-socket authentication if that option is offered, remove anonymous users, disallow remote root login, remove the test database, and reload the privilege tables. MariaDB explains these prompts in its official mariadb-secure-installation documentation.

Create a database and a separate login for an application. Replace YOUR_DB_PASSWORD with a long, unique password before running this block; do not use the placeholder as the real secret.

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 a file under /var/www/html.

Step 4: Install PHP 8.1 with PHP-FPM

Ubuntu 22.04’s native PHP packages use the 8.1 series, and PHP-FPM keeps PHP workers separate from Apache instead of using the older embedded mod_php model. PHP’s Apache installation guidance recommends PHP-FPM with Apache’s mod_proxy_fcgi for modern deployments.

sudo apt install -y php8.1-fpm php8.1-cli php8.1-mysql php8.1-curl php8.1-gd php8.1-mbstring php8.1-xml php8.1-zip php8.1-opcache
php -v
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
sudo systemctl enable --now php8.1-fpm
sudo systemctl reload apache2

The version command should begin with PHP 8.1. The Apache module and configuration commands make .php requests go to the FPM socket instead of returning PHP source code.

Create a short-lived endpoint that returns a fixed string. This checks PHP execution without publishing the detailed server information 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

Verify the LAMP stack

Run these checks from SSH to confirm that the three services start at boot, PHP has its MariaDB connector, Apache accepts its configuration, and the web server answers locally.

sudo systemctl is-active apache2 php8.1-fpm mariadb
sudo systemctl is-enabled apache2 php8.1-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 lines, three enabled lines, Syntax OK, the 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

From your computer, browse to http://SERVER_IP/ and replace SERVER_IP with the VPS public address. You should see Apache’s default page; a domain and HTTPS certificate can be added when you deploy the application.

Troubleshooting

Unit php8.1-fpm.service not found

The FPM package is missing, or the machine is not actually running Ubuntu 22.04 repositories. Confirm the release and install the Jammy package rather than adding a third-party PHP repository.

grep -E '^(PRETTY_NAME|VERSION_ID|VERSION_CODENAME)=' /etc/os-release
sudo apt update
sudo apt install -y php8.1-fpm
sudo systemctl enable --now php8.1-fpm

If the codename is not jammy, stop and use instructions for the installed Ubuntu release instead of mixing package sources.

PHP source appears in the browser or Apache returns 503 Service Unavailable

Apache is not connected to the FPM socket, or the FPM service failed to start. Re-enable the packaged configuration, inspect the service log, and then reload Apache.

sudo systemctl status php8.1-fpm --no-pager
sudo journalctl -u php8.1-fpm -n 40 --no-pager
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
sudo apachectl configtest
sudo systemctl restart php8.1-fpm apache2

Recreate the temporary health.php file from Step 4, test it with curl, and remove it again. Do not leave a diagnostic file in the public document root.

SSH stops working after enabling UFW

The firewall was enabled before the correct SSH port was allowed, or SSH is listening on a nonstandard port. Use VPS control panel (Virtualizor) → VNC console from the VPSLake client area to regain console access, then check the listener and add the actual port.

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

Replace YOUR_SSH_PORT with the numeric port shown by ss. Keep the current session open and test a second SSH connection before removing any old firewall rule.

Access denied for user 'lampuser'@'localhost'

The application password does not match the value used when the account was created, or the application is connecting with a different database host. Test the credentials locally, then reset the account only if needed.

mariadb -u lampuser -p lampdb -e "SELECT 1;"
sudo mariadb -e "ALTER USER 'lampuser'@'localhost' IDENTIFIED BY 'YOUR_NEW_DB_PASSWORD';"

Use the real password at the prompt, not the placeholder. If you reset it, update the application configuration at the same time.

Hardening

  • Keep port 3306 closed in UFW. An application on this VPS can use localhost without a public MariaDB listener.
  • Point your domain to the server, issue a trusted TLS certificate, and redirect HTTP to HTTPS before accepting logins or other sensitive data.
  • Remove temporary health checks, keep database credentials out of the document root, and apply Ubuntu security updates regularly.

FAQ

Does Ubuntu 22.04 include PHP 8.1?

Yes. Ubuntu 22.04’s Jammy repositories provide the PHP 8.1 package series used in this guide. Installing the versioned packages keeps the Apache FPM configuration and systemd service name aligned with that release.

Why use PHP-FPM instead of libapache2-mod-php?

PHP-FPM runs PHP as a separate FastCGI service and works with Apache’s event-based worker model. It also lets you restart PHP independently from Apache, which is a better starting point for a new deployment.

Should I open MariaDB port 3306?

Not for a normal single-server website. The lampuser account is restricted to localhost, so the application can connect locally while UFW leaves the database unreachable from the public Internet.

Do I need a domain to install LAMP?

No. You can verify Apache with the VPS public IP while building the stack. You need a domain later for a clean production URL and a trusted HTTPS certificate.

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 on Rocky Linux 9 (Step-by-Step)

Next Article

Send TradingView to MT5 Alerts From a VPS

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 ✨