Automating the installation of a LAMP stack on Ubuntu 22.04

Automating the installation of a LAMP stack on Ubuntu 22.04

A LAMP stack is a combination of at least 4 different technologies used in tandem to create a fully functioning web server or web application.

The complete components of a LAMP stack include:

Linux machine (Ubuntu)

Apache web server

MySQL database

PHP

The installation of this stack will be done using a executable bash script that can be used on any debian based linux distro.

First create a file named LAMP.sh which is the file where our bash script will be stored, and will also be later executed.

Next make the file executable by executing the following command

chmod -x LAMP.sh

We can now begin scripting the installation file

#!/bin/bash

sudo apt-get update
sudo apt install -y mysql-server mysql-client expect apache2 php libapache2-mod-php php-mysql php8.2 php8.2-curl php8.2-dom php8.2-xml php8.2-mysql php8.2-sqlite3 php8.3 php8.3-curl php8.3-dom php8.3-xml php8.3-mysql php8.3-sqlite3
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update
echo “Done with installations”

sudo systemctl start mysql.service
sudo systemctl start apache2.service

sudo expect <<EOF
spawn mysql_secure_installation

set timeout 1
expect {
“Press y|Y for Yes, any other key for No:” {
send “yr”
expect “Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:”
send “0r”
}
“The ‘validate_password’ component is installed on the server.” {
send_user “Skipping VALIDATE PASSWORD section as it is already installed.n”
}
}

expect “Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:”
send “0r”

expect “Remove anonymous users? (Press y|Y for Yes, any other key for No) :”
send “yr”

expect “Disallow root login remotely? (Press y|Y for Yes, any other key for No) :”
send “nr”

expect “Remove test database and access to it? (Press y|Y for Yes, any other key for No) :”
send “yr”

expect “Reload privilege tables now? (Press y|Y for Yes, any other key for No) :”
send “yr”

expect eof
EOF

echo “MySQL secure installation setup complete.”

sudo systemctl start mysql
# Execute MySQL commands to create the database, user, and grant privileges
sudo mysql -uroot <<MYSQL_SCRIPT
CREATE DATABASE IF NOT EXISTS webserver;
CREATE USER IF NOT EXISTS ‘User1’@’localhost’ IDENTIFIED BY ‘Password123’;
GRANT ALL PRIVILEGES ON webserver.your_table TO ‘User1’@’localhost’ WITH GRANT OPTION;
FLUSH PRIVILEGES;
MYSQL_SCRIPT

echo “Database and user created.”

sudo a2enmod rewrite
sudo mkdir -p /var/www/demo.com
sudo chown -R $USER:$USER /var/www/demo.com
sudo chmod -R 755 /var/www/demo.com
# Write the HTML content to the file
sudo bash -c ‘cat <<EOF > /var/www/demo.com/index.html
<html>
<head>
<title>Welcome to Your_domain!</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>
EOF’

echo “HTML file created at /var/www/demo.com/index.html”

# Configure Virtual hosting
sudo bash -c ‘cat <<EOF > /etc/apache2/sites-available/demo.com.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName demo.com
ServerAlias www.demo.com
DocumentRoot /var/www/demo.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

echo “Virtual hosting configured”

sudo a2ensite demo.com.conf
sudo a2dissite 000-default.conf
sudo systemctl restart apache2

Execute the script using this command

./LAMP.sh

or using

bash LAMP.sh
Please follow and like us:
Pin Share