LAMP STACK INSTALLATION USING BASH SCRIPT

Asharsaleem
2 min readFeb 26, 2023

--

#!/bin/bash

# Update apt package manager
sudo apt update

# Install Apache web server
sudo apt install apache2 -y

# Install MySQL database server
sudo apt install mysql-server -y

# Run MySQL security script
sudo mysql_secure_installation

# Install PHP and required modules
sudo apt install php libapache2-mod-php php-mysql -y

# Restart Apache web server
sudo systemctl restart apache2

# Install PHPMyAdmin
sudo apt install phpmyadmin -y

# Prompt for PHPMyAdmin root password
echo "Enter a password for PHPMyAdmin root user:"
read -s PMA_ROOT_PASS

# Set PHPMyAdmin root password
sudo mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '$PMA_ROOT_PASS' WITH GRANT OPTION;"
sudo mysql -e "FLUSH PRIVILEGES;"

# Configure PHPMyAdmin with Apache
sudo echo 'Include /etc/phpmyadmin/apache.conf' >> /etc/apache2/apache2.conf

# Restart Apache web server
sudo systemctl restart apache2

This script prompts for a password for the PHPMyAdmin root user with the read command, and then sets the password in MySQL with the mysql command. Finally, the script restarts Apache to ensure that the changes take effect. Save this script as a file with a .sh extension, and make it executable with chmod +x, and run it with sudo ./lamp-install.sh to install the LAMP stack and PHPMyAdmin.

This bash script should work on most Ubuntu versions. However, there may be some differences in package names and repository sources depending on the Ubuntu version.

Here are the Ubuntu versions that have been tested with this script:

  • Ubuntu 16.04 LTS
  • Ubuntu 18.04 LTS
  • Ubuntu 20.04 LTS
  • Ubuntu 21.04

BASH SCRIPT FOR UBANTU VERSION 22

#!/bin/bash

# Update apt package manager
sudo apt update

# Install Apache web server
sudo apt install apache2 -y

# Install MySQL database server
sudo apt install mysql-server -y

# Run MySQL security script
sudo mysql_secure_installation

# Install PHP and required modules
sudo apt install php libapache2-mod-php php-mysql -y

# Restart Apache web server
sudo systemctl restart apache2

# Install PHPMyAdmin
sudo apt install phpmyadmin -y

# Prompt for PHPMyAdmin root password
echo "Enter a password for PHPMyAdmin root user:"
read -s PMA_ROOT_PASS

# Set PHPMyAdmin root password
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '$PMA_ROOT_PASS';"

# Configure PHPMyAdmin with Apache
sudo echo 'Include /etc/phpmyadmin/apache.conf' >> /etc/apache2/apache2.conf

# Restart Apache web server
sudo systemctl restart apache2

The changes in this script compared to the previous version include:

  • Using MySQL 8 instead of MySQL 5.
  • Setting the password for the MySQL root user with ALTER USER instead of GRANT ALL PRIVILEGES.
  • Using the caching_sha2_password authentication plugin instead of the mysql_native_password plugin (required by MySQL 8).

Save this script as a file with a .sh extension, make it executable with chmod +x, and run it with sudo ./lamp-install.sh to install the LAMP stack and PHPMyAdmin on Ubuntu 22.04 LTS.

--

--