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

Install LAMP stack on Debian 12 with Apache, PHP 8.2-FPM, MariaDB, UFW, and a safe PHP check for a production-ready VPS in about ten minutes.
A polished 3D illustration of a Debian server with a web gateway, PHP processor, and MariaDB database cylinder.

Install LAMP stack on Debian 12 and you will have Apache serving web requests, PHP 8.2 processing dynamic pages through PHP-FPM, and MariaDB ready for application data. A fresh VPS takes about ten minutes to configure, including a firewall and a temporary health check.

This guide uses Debian 12 Bookworm’s native packages and keeps the database on the local server. You can add a domain and HTTPS after the basic stack is responding.

Prerequisites

  • A 64-bit Debian 12 (Bookworm) 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 Debian package repositories and a public IP address for testing Apache.
  • A domain is optional during installation, but you need one for a production TLS certificate.
  • A VPSLake Linux VPS with root access if you need a server for this stack.

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

Step 1: Confirm Debian 12 and update the server

Confirming the release first prevents package and service mismatches, while a full upgrade applies current Bookworm security updates before you expose a web server.

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

The release should report Debian 12 and bookworm; x86_64 is the usual architecture for a 64-bit VPS. Package patch versions can differ as Debian publishes updates.

PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
VERSION_ID="12"
VERSION_CODENAME=bookworm
x86_64

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

Step 2: Install Apache and allow web traffic

Apache handles HTTP requests, and UFW limits the public surface to SSH plus the HTTP and HTTPS ports needed by a website.

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

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

Test the web server locally. This distinguishes an Apache problem from a provider-level firewall or routing problem when you later test the public IP.

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

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

Debian 12 provides PHP 8.2, and PHP-FPM runs PHP through FastCGI so Apache does not need the older embedded PHP module. The PHP-FPM manual documents the process manager used by this setup.

Install PHP and the extensions commonly required by database-backed applications.

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

The version line should begin with PHP 8.2.

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

Enable Apache’s FastCGI support, activate the PHP-FPM configuration, and start the matching service at boot.

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

The final line should print active. Create a short-lived endpoint that returns a fixed string rather than exposing the detailed server information produced 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

Step 4: Install MariaDB and secure the database server

MariaDB stores application data, while a separate account limited to localhost lets a local website connect without making port 3306 public.

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. If this is a new installation, press Enter when it asks for an empty current root password if appropriate, remove anonymous users, reject remote root login, remove the test database, and reload the privilege tables. MariaDB explains these prompts in its official mariadb-secure-installation reference; local Unix-socket authentication for the administrative account is normal on modern MariaDB.

Create an application database and a least-privilege login. Replace YOUR_DB_PASSWORD with a long, unique password before running this block, and keep the real secret out of the public document root.

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 a row named lampdb.

Verify the LAMP stack

Run these checks after the installation to confirm that Apache, PHP-FPM, and MariaDB are running, enabled at boot, and connected through the expected PHP modules.

sudo systemctl is-active apache2 php8.2-fpm mariadb
sudo systemctl is-enabled apache2 php8.2-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, 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

Unable to locate package php8.2-fpm

The APT index is stale, or the server is not actually using Debian 12 repositories. Check the release and repository codename before trying to install packages from another Debian version.

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

Do not mix Debian 11, Debian 12, and Debian 13 repositories. Mixed PHP libraries can leave both Apache and FPM unable to start.

Unit php8.2-fpm.service not found

PHP-FPM was not installed, or the package installation stopped with an error. Install the versioned FPM package, enable its Apache configuration, and start the service.

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

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

Apache is not forwarding PHP files to FPM, or the FPM unit failed to start. Inspect the service log and Apache configuration before restarting both services.

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

Confirm that /etc/apache2/conf-enabled/php8.2-fpm.conf exists and that proxy_fcgi is enabled. Recreate the temporary health.php test only long enough to check the result, then remove it.

SSH stops working after enabling UFW

The firewall may have been enabled before the right SSH rule was added, or SSH may listen on a nonstandard port. Use VPS control panel (Virtualizor) → VNC console from the VPSLake client area to recover console access, then allow the actual port.

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 current session open and test a second SSH connection before removing any old rule.

Hardening

  • Keep port 3306 closed. A local PHP application can use localhost or the Unix socket; remote database access should be limited to a known source address and encrypted.
  • Point YOUR_DOMAIN to the VPS, configure an Apache virtual host, and install a trusted TLS certificate before accepting credentials or personal data.
  • Keep SSH key authentication enabled, remove unused UFW rules, and review the policy with the VPSLake UFW firewall generator.
  • Remove temporary test files, back up /var/www/html and MariaDB databases, and apply Debian security updates regularly.

FAQ

Which PHP version does Debian 12 use for LAMP?

Debian 12’s native PHP packages provide PHP 8.2. This guide names the 8.2 packages and service explicitly so Apache uses the same PHP-FPM branch that the VPS is running.

Do I need libapache2-mod-php with PHP-FPM?

No. Apache passes PHP requests through proxy_fcgi to the php8.2-fpm service, so the embedded Apache PHP module is not needed. Keeping one request path also makes service logs and failures easier to diagnose.

Should I open MariaDB port 3306 for a PHP website?

No. A website on the same VPS connects locally, so MariaDB does not need a public firewall rule. Only consider remote connections for a separate host, and restrict the source address instead of opening the port globally.

Can I install this stack without a domain name?

Yes. Apache and the PHP health check work with 127.0.0.1 or the VPS IP. Add the domain later, create the virtual host, and issue a TLS certificate after DNS points to the server.

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

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 ✨