Install LAMP Stack on Ubuntu 24.04/Debian 12


#!/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
echo "Securing MariaDB..."
sudo mysql_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
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)
read -p "Do you want to install phpMyAdmin? (y/n): " install_pma
if [[ "$install_pma" == "y" || "$install_pma" == "Y" ]]; then
    echo "Installing phpMyAdmin..."
    sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl -y
    sudo phpenmod mbstring
    sudo systemctl restart apache2
    echo "phpMyAdmin installed. Configure Apache if necessary."
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..."
    sudo ufw allow 'Apache Full'
    sudo ufw enable
    echo "Firewall rules updated."
fi

echo "LAMP stack installation with MariaDB completed!"

Previous Next