How to Set Up a Complete LAMP/LEMP Stack on Debian

LAMP (Linux, Apache, MySQL, PHP) and LEMP (Linux, nginX, MySQL, PHP) are the two most common web server stacks. This guide walks through setting up either one, specifically on Debian, from a fresh server to a working PHP application environment.

Choosing Between LAMP and LEMP

Both accomplish the same goal; Nginx (LEMP) generally handles high concurrency more efficiently, while Apache (LAMP) has broader legacy compatibility with older .htaccess-dependent applications. If you don't have a specific reason to choose one, LEMP is a solid modern default.

Step 1 — Update the System

sudo apt update && sudo apt upgrade -y

Step 2A — Install Nginx (for LEMP)

sudo apt install nginx -y
sudo systemctl enable --now nginx

Step 2B — OR Install Apache (for LAMP)

sudo apt install apache2 -y
sudo systemctl enable --now apache2

Step 3 — Install MySQL (or MariaDB)

sudo apt install mariadb-server -y
sudo mysql_secure_installation

MariaDB is the default MySQL-compatible database on Debian and works as a drop-in replacement for most applications.

Step 4 — Install PHP

sudo apt install php php-fpm php-mysql php-cli php-curl php-xml php-mbstring -y

Adjust the extension list based on your specific application's requirements (WordPress, Laravel, and others each need slightly different extensions).

Step 5 — Configure PHP-FPM with Nginx (LEMP)

sudo nano /etc/nginx/sites-available/mysite
server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/mysite;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Step 5 (Alternative) — Configure PHP with Apache (LAMP)

sudo apt install libapache2-mod-php -y
sudo systemctl restart apache2

Apache with libapache2-mod-php processes PHP automatically for any .php file in the document root, with no additional configuration needed for basic use.

Step 6 — Create a Test PHP File

echo "<?php phpinfo(); ?>" | sudo tee /var/www/mysite/info.php

Visit http://your-server-ip/info.php to confirm PHP is processing correctly. Delete this file immediately after testing — it exposes detailed server configuration information.

sudo rm /var/www/mysite/info.php

Step 7 — Create the Application Database

sudo mysql
CREATE DATABASE myapp;
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'CHANGE_ME_STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 8 — Add HTTPS

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com

Substitute python3-certbot-apache and --apache if using LAMP instead.

Step 9 — Set Up the Firewall

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Common Errors

502 Bad Gateway (LEMP) — verify the PHP-FPM socket path in your Nginx config matches the actual installed PHP version; check with ls /run/php/.

PHP files download instead of executing (LAMP) — verify libapache2-mod-php is installed and Apache was restarted afterward.

Best Practices

  • Always remove test/info PHP files after use
  • Use dedicated database users per application, never a shared root database account
  • Keep PHP, the web server, and MySQL/MariaDB updated regularly

Continue Reading

Browse more articles in Debian VPS Tutorials.

  • lamp stack debian, lemp stack debian, debian web server setup, php mysql debian
  • 0 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Relaterade artiklar

What Is Debian? Advantages, Disadvantages, and Debian vs Other Linux Distributions

Introduction Debian is one of the oldest, most stable, and most trusted Linux distributions...

Debian VPS vs Ubuntu VPS: Which One Should You Choose?

Introduction When ordering a VPS, one of the first decisions you'll make is choosing the...

Top 10 Reasons to Choose a Debian VPS for Your Server

Introduction Debian has earned a reputation as one of the most reliable Linux operating systems...

Top 10 Applications You Should Install on a Debian VPS

Introduction One of Debian's greatest strengths is its enormous software repository and...

How to Upgrade Debian 11 to Debian 12 (Complete Step-by-Step Guide)

Introduction Upgrading your Debian VPS to the latest stable release ensures better security,...