The Complete LAMP Stack Guide: Debian, Ubuntu & Mint
From zero to a fully functioning web server. Install, secure, and deploy with single commands.
What is LAMP & Why It Matters
LAMP is the foundational stack that powers over half the internet:
Linux (Operating System)
Apache (Web Server)
MariaDB/MySQL (Database)
PHP (Programming Language)
This guide covers Debian 12+, Ubuntu 22.04+, and Linux Mint 21+ with modern package versions and security best practices.
Linux (Operating System)
Apache (Web Server)
MariaDB/MySQL (Database)
PHP (Programming Language)
This guide covers Debian 12+, Ubuntu 22.04+, and Linux Mint 21+ with modern package versions and security best practices.
Part 1: The One-Command Installation
Update your system first:
sudo apt update && sudo apt upgrade -yThe magic command:
sudo apt install apache2 mariadb-server php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
What this installs:
apache2: The Apache web server (version 2.4+)mariadb-server: Modern drop-in replacement for MySQL (version 10.11+)php: PHP 8.1+ (8.2/8.3 on newer systems) with core functionalitylibapache2-mod-php: Apache module to process PHP filesphp-mysql: PHP module for MariaDB/MySQL connectivity- Plus essential extensions: cURL, GD graphics, MBstring, XML, Zip
Part 2: Verification & Basic Configuration
Test Apache (Web Server):
sudo systemctl status apache2 # If not running: sudo systemctl start apache2 # Enable auto-start: sudo systemctl enable apache2Open your browser to http://localhost or your server's IP address. You should see the Apache default page.
Test PHP:
Create a test file:echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.phpVisit http://localhost/phpinfo.php. You'll see a detailed PHP configuration page. Delete this file after testing:
sudo rm /var/www/html/phpinfo.php
Configure MariaDB (Security Hardening):
Run the interactive security script:sudo mysql_secure_installationFollow prompts to:
- Set a strong root password (different from your system password)
- Remove anonymous users (Y)
- Disallow root login remotely (Y)
- Remove test database (Y)
- Reload privilege tables (Y)
Part 3: Creating Your First Website
1. Set Proper Permissions:
sudo chown -R $USER:$USER /var/www/html sudo chmod -R 755 /var/www/html
2. Create a Virtual Host (Recommended for multiple sites):
Create a configuration file:sudo nano /etc/apache2/sites-available/yourdomain.confAdd this configuration (replace
yourdomain with your actual domain):
<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
3. Enable the Site:
sudo mkdir /var/www/yourdomain sudo a2ensite yourdomain.conf sudo a2dissite 000-default.conf sudo systemctl reload apache2
4. Create a PHP Test Page:
echo '<?php
echo "<h1>LAMP Stack is Working!</h1>";
echo "<p>Server Time: " . date("Y-m-d H:i:s") . "</p>";
// Test database connection
$conn = new mysqli("localhost", "root", "YOUR_PASSWORD");
if ($conn->connect_error) {
echo "<p style=\"color:red\">Database connection failed: " . $conn->connect_error . "</p>";
} else {
echo "<p style=\"color:green\">Database connection successful!</p>";
$conn->close();
}
?>' | sudo tee /var/www/yourdomain/index.php
Replace YOUR_PASSWORD with your MariaDB root password.
Part 4: Essential Security & Optimization
1. Configure PHP for Production:
Edit the PHP configuration:sudo nano /etc/php/8.2/apache2/php.iniFind and change these lines (PHP version might be 8.1 or 8.3):
expose_php = Off max_execution_time = 30 upload_max_filesize = 2M post_max_size = 8M display_errors = Off log_errors = On error_log = /var/log/php_errors.logCreate the error log and restart Apache:
sudo touch /var/log/php_errors.log sudo chown www-data:www-data /var/log/php_errors.log sudo systemctl restart apache2
2. Install Fail2ban (Brute Force Protection):
sudo apt install fail2ban -y sudo systemctl enable fail2ban
3. Enable HTTPS with Let's Encrypt:
sudo apt install certbot python3-certbot-apache -y sudo certbot --apache -d yourdomain.com -d www.yourdomain.comFollow the interactive prompts to get a free SSL certificate.
4. Configure Firewall (UFW):
sudo ufw allow 22/tcp comment 'SSH' sudo ufw allow 80/tcp comment 'HTTP' sudo ufw allow 443/tcp comment 'HTTPS' sudo ufw --force enable sudo ufw status
Part 5: Troubleshooting & Maintenance
Common Issues & Solutions:
Apache won't start:sudo apache2ctl configtest # Check configuration sudo systemctl status apache2 --full # Detailed status sudo journalctl -xe -u apache2 # View logsPHP file downloads instead of executes:
sudo a2enmod php # Ensure PHP module is enabled sudo systemctl restart apache2Database connection issues:
sudo systemctl status mariadb sudo mysql -u root -p # Test login # If you forgot password: sudo mysql --skip-grant-tables
Useful Maintenance Commands:
# Check disk space df -h # Check memory usage free -h # Update all packages weekly sudo apt update && sudo apt upgrade -y # Backup website and database sudo tar -czf /backup/website_$(date +%Y%m%d).tar.gz /var/www/yourdomain sudo mysqldump -u root -p --all-databases > /backup/database_$(date +%Y%m%d).sql
Deployment Ready
Your LAMP stack is now fully installed, secured, and optimized for production. You can deploy WordPress, Laravel, custom PHP applications, or any web software that requires this standard stack.
Next Steps:
Next Steps:
- Install WordPress:
wget https://wordpress.org/latest.tar.gz - Set up phpMyAdmin for database management
- Configure automated backups
- Set up monitoring with UptimeRobot or similar
© 2026 Secupgrade.com – Enterprise web infrastructure made simple.
Apache Docs | MariaDB Docs | PHP Docs
Apache Docs | MariaDB Docs | PHP Docs
