#!/bin/bash
# Update the system
echo "Updating the system..."
sudo apt update && sudo apt upgrade -y
# Install Apache
echo "Installing Apache..."
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
echo "Apache installed and started."
# Install MariaDB
echo "Installing MariaDB..."
sudo apt install mariadb-server mariadb-client -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
echo "MariaDB installed and started."
# Secure MariaDB (Debian 13 uses mariadb-secure-installation)
echo "Securing MariaDB..."
sudo mariadb-secure-installation
# Add admin user to MariaDB with full privileges
read -sp "Enter a password for the MariaDB 'admin' user: " admin_password
echo
sudo mysql -e "GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY '$admin_password' WITH GRANT OPTION;"
sudo mysql -e "FLUSH PRIVILEGES;"
echo "Admin user 'admin' with full privileges created in MariaDB."
# Install PHP
echo "Installing PHP..."
sudo apt install php libapache2-mod-php php-mysql -y
# Configure Apache to prioritize PHP files (Debian 13 default dir.conf already has index.php but re-order is safe)
echo "Configuring Apache to work with PHP..."
sudo sed -i "s/index.html/index.php index.html/" /etc/apache2/mods-enabled/dir.conf
sudo systemctl restart apache2
echo "PHP installed and configured with Apache."
# Create a test PHP file
echo "Creating a test PHP file..."
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test.php
echo "Visit http://localhost/test.php to confirm PHP is working."
# Install phpMyAdmin (Optional) - Pre-seed debconf to avoid interactive hang
read -p "Do you want to install phpMyAdmin? (y/n): " install_pma
if [[ "$install_pma" == "y" || "$install_pma" == "Y" ]]; then
echo "Installing phpMyAdmin..."
# Pre-seed answers: select Apache2, skip dbconfig-common automatic database setup (to avoid failures)
echo "phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2" | sudo debconf-set-selections
echo "phpmyadmin phpmyadmin/dbconfig-install boolean false" | sudo debconf-set-selections
# Install with non-interactive frontend to ensure no prompt blocks the script
sudo DEBIAN_FRONTEND=noninteractive apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl -y
sudo phpenmod mbstring
sudo systemctl restart apache2
echo "phpMyAdmin installed. You may need to manually configure database access."
fi
# Allow HTTP/HTTPS traffic in the firewall
read -p "Do you want to configure the firewall for HTTP and HTTPS? (y/n): " configure_firewall
if [[ "$configure_firewall" == "y" || "$configure_firewall" == "Y" ]]; then
echo "Allowing HTTP and HTTPS traffic in the firewall..."
if ! command -v ufw &> /dev/null; then
echo "UFW is not installed. Installing..."
sudo apt install ufw -y
fi
# Ensure SSH is allowed first to prevent lockout (if this is a remote server)
sudo ufw allow ssh
sudo ufw allow 'Apache Full'
sudo ufw --force enable
echo "Firewall rules updated."
fi
echo "LAMP stack installation with MariaDB completed!"
Install LAMP Stack on Debian 13