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
- How to Install Nginx on Ubuntu & Debian (Complete Guide)
- How to Install and Secure MySQL 8 on Ubuntu & Debian
- Top 10 Applications You Should Install on a Debian VPS
Browse more articles in Debian VPS Tutorials.