How to Install LAMP Stack on Debian 11

Install LAMP stack on Debian 11 with Apache, PHP 7.4-FPM, MariaDB, UFW, and a safe health check, then verify your VPS in about ten minutes today.
A polished 3D illustration of a Debian server stack with a web gateway, PHP processor, and database cylinder.

Install LAMP stack on Debian 11 and you will have Apache serving web requests, PHP 7.4 running through PHP-FPM, and MariaDB ready for a local application database. Allow about ten minutes for a fresh VPS, including a firewall and a safe PHP test.

Debian 11 is an older release, so use these instructions for an existing Bullseye server and plan a migration to a supported Debian release for a new production deployment.

Prerequisites

  • A 64-bit Debian 11 (Bullseye) VPS with at least 1 GB of RAM and 10 GB of free disk space.
  • SSH access as root, or a normal account with working sudo privileges.
  • Outbound Internet access to the configured Debian package repositories.
  • A public IPv4 address for testing Apache. A domain is optional during installation but needed for a production TLS certificate.
  • A VPSLake Linux VPS if you need a root-access server for this stack.

The commands use sudo for a non-root account. If you are already connected as root, remove sudo from the commands.

Step 1: Confirm Bullseye and update packages

Checking the release first prevents you from applying Debian 12 or Debian 13 instructions to this PHP 7.4-based system. Updating the package index and installed packages also applies the latest packages available to your configured Bullseye repositories.

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

The release should identify Debian GNU/Linux 11 and bullseye; a typical VPS reports x86_64 for the architecture. The exact package versions can differ from the examples below.

PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
VERSION_ID="11"
VERSION_CODENAME=bullseye
x86_64

If the upgrade installs a new kernel, keep this SSH session open until you have confirmed that the server can reboot and reconnect.

Step 2: Install Apache and configure UFW

Apache serves static files and forwards PHP requests later, while UFW keeps the public firewall limited to SSH and web traffic.

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 ufw status verbose
sudo systemctl is-active apache2

Add the SSH rule before enabling UFW. If SSH listens on a custom port, allow that port instead of relying only on the OpenSSH application profile. The status should show an active firewall with ports 22, 80, and 443 allowed according to your rules.

Test Apache locally before using a browser. A successful response proves the service is listening even if an upstream VPS firewall still blocks the public address.

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

Step 3: Install PHP 7.4 and connect PHP-FPM to Apache

Bullseye’s native PHP metapackages select PHP 7.4, and PHP-FPM executes scripts through FastCGI without loading PHP directly into every Apache worker. The official PHP-FPM documentation describes this process manager’s role.

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

The first line should begin with PHP 7.4. The package php-json is not needed separately here because JSON support is included in the PHP 7.4 common package.

Enable Apache’s FastCGI proxy module and the FPM configuration that matches the installed PHP minor version.

PHP_VERSION="$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;')"
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf "php${PHP_VERSION}-fpm"
sudo systemctl enable --now "php${PHP_VERSION}-fpm"
sudo systemctl reload apache2

Create a temporary endpoint that returns a fixed value instead of exposing phpinfo(), which can reveal server configuration. The final command removes the file as soon as the request 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 supplies the SQL database, and a user restricted to localhost lets the web application 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

When the security script asks questions, remove anonymous users, disallow remote root login, remove the test database, and reload the privilege tables. On MariaDB 10.5, keeping local Unix-socket authentication for the administrative account is fine; the application should use its own account. See MariaDB’s official mariadb-secure-installation reference if a prompt differs.

Create a database and a separate login for the application. Replace YOUR_DB_PASSWORD with a long, unique password before running this block.

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. Keep the real database password in your application’s secret configuration, never in /var/www/html.

Verify the LAMP stack

Run these checks together to confirm that Apache, the matching PHP-FPM unit, and MariaDB are running and enabled at boot. The final requests also prove that Apache accepts its configuration, PHP has the MariaDB connector, and the web server answers locally.

PHP_VERSION="$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;')"
sudo systemctl is-active apache2 "php${PHP_VERSION}-fpm" mariadb
sudo systemctl is-enabled apache2 "php${PHP_VERSION}-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, the PHP module names, a MariaDB version, and an HTTP success response.

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

Troubleshooting

404 Not Found or an expired release during apt update

Debian 11 is an older release and its normal security repositories may no longer be available after the Bullseye support window. For a new deployment, rebuild the VPS with a supported Debian release; do not keep installing security-sensitive software from random mirrors.

If an existing Bullseye server must remain temporarily, inspect the configured entries and use Debian’s official archive instructions for that release. Archived packages do not restore current security support, so schedule the migration before exposing the site to users.

grep -RInE '(^deb .*bullseye|^deb-src .*bullseye)' /etc/apt/sources.list /etc/apt/sources.list.d 2>/dev/null

Unit php7.4-fpm.service not found

The FPM package is missing, or the package index does not belong to Bullseye. Confirm the release, install the versioned FPM package, and enable the matching Apache configuration.

grep -E '^(PRETTY_NAME|VERSION_ID|VERSION_CODENAME)=' /etc/os-release
sudo apt install -y php7.4-fpm
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.4-fpm
sudo systemctl enable --now php7.4-fpm
sudo systemctl reload apache2

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

Apache is not handing .php files to FPM, or the FPM service failed to start. Check the service log and Apache configuration, then restart after correcting the reported error.

sudo systemctl status php7.4-fpm --no-pager
sudo journalctl -u php7.4-fpm -n 40 --no-pager
sudo apachectl configtest
sudo systemctl restart php7.4-fpm apache2

Recreate the short-lived health.php test from Step 3. If PHP source is still shown, check that /etc/apache2/conf-enabled/php7.4-fpm.conf exists and that proxy_fcgi is enabled.

Access denied for user 'lampuser'@'localhost'

The application password does not match the value used when the account was created, or the account was created for a different host. Test the account through the local socket before changing grants.

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

Enter the real password, not the placeholder. If it is wrong, reset it from the administrative account and update the application configuration at the same time.

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

Hardening

  • Keep MariaDB’s port 3306 closed. A local application can use localhost or the Unix socket; if you later need remote access, restrict it to a specific source address and require encrypted connections.
  • Point YOUR_DOMAIN to the VPS, install a trusted TLS certificate, and redirect HTTP to HTTPS before accepting credentials or personal data.
  • Keep SSH protected with keys, disable password login after confirming key access, and review the final UFW rules with the VPSLake UFW firewall generator.
  • Remove test files, back up /var/www/html and MariaDB databases, and plan the move from Bullseye to a supported Debian release.

FAQ

What does LAMP include on Debian 11?

LAMP means Linux, Apache, MariaDB, and PHP. This guide uses the apache2 service, PHP 7.4 through PHP-FPM, and the mariadb service with a local application database.

Is PHP 7.4 the correct PHP version for Bullseye?

Yes. Debian 11’s native PHP metapackages resolve to PHP 7.4. If your application requires a newer PHP branch, use a supported Debian release or a carefully maintained, release-compatible package source rather than mixing packages from another Debian version.

Should I open port 3306 for WordPress or another website?

No. A website running on the same VPS can connect to MariaDB locally, so it does not need a public database port. Leave 3306 closed unless a separate host genuinely needs access.

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

PHP-FPM keeps the interpreter in its own service and lets Apache pass PHP requests through FastCGI. It also makes the active PHP service explicit, which is useful when checking logs or changing PHP versions later.

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 Can't Connect? A 10-Step Checklist

Next Article

How to Install LAMP Stack on Debian 13 (Step-by-Step)

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 ✨