Logo

dev-resources.site

for different kinds of informations.

Automating the installation of a LAMP stack on Ubuntu 22.04

Published at
6/10/2024
Categories
linux
apache
sql
php
Author
oyololatoni
Categories
4 categories in total
linux
open
apache
open
sql
open
php
open
Author
11 person written this
oyololatoni
open
Automating the installation of a LAMP stack on Ubuntu 22.04

LAMP

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:

  1. Linux machine (Ubuntu)

  2. Apache web server

  3. MySQL database

  4. 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 going to the folder where the file is located and execute the following command

chmod -x LAMP.sh
Enter fullscreen mode Exit fullscreen mode

We can now begin scripting the installation file by editing the LAMP.sh file. You can do this using a cli based file editor such as vim or nano, or you can use a gui based text editor (recommended for beginners).

#!/bin/bash

# Update the package lists for upgrades and new package installations
sudo apt-get update 

# Install MySQL server and client, Expect, Apache2, PHP, and various PHP extensions
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

# Add the PPA repository for PHP maintained by Ondřej Surý
sudo add-apt-repository -y ppa:ondrej/php

# Update the package lists again to include packages from the new repository
sudo apt-get update


echo "Done with installations"

# Start the MySQL service
sudo systemctl start mysql.service

# Start the Apache2 service
sudo systemctl start apache2.service

# Start the MySQL secure installation process
# The expect command is used to automate responses to interactive prompts
sudo expect <<EOF
spawn mysql_secure_installation

# Set the timeout for expect commands
set timeout 1

# Handle the password validation prompt. If not present, skip.
expect {
    "Press y|Y for Yes, any other key for No:" {
        send "y\r"
        expect "Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:"
        send "0\r"
    }
    "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 "0\r"

expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "y\r"

expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
send "n\r"

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

expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
send "y\r"

expect eof
EOF

echo "MySQL secure installation setup complete."

# Ensure MySQL service is started
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."

# Enable the Apache mod_rewrite module
sudo a2enmod rewrite

# Create the directory for the new virtual host
sudo mkdir -p /var/www/demo.com

# Change the ownership of the directory to the current user
sudo chown -R $USER:$USER /var/www/demo.com

# Set permissions for the directory
sudo chmod -R 755 /var/www/demo.com

# Create an index.html file with a simple HTML content and save it in the relevant 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"

# Create the virtual host configuration file for 'demo.com'
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>
EOF'

echo "Virtual hosting configured"

# Enable the new virtual host configuration
sudo a2ensite demo.com.conf

# Disable the default virtual host configuration
sudo a2dissite 000-default.conf

# Restart Apache2 service to apply the changes
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Execute the script using this command

./LAMP.sh
Enter fullscreen mode Exit fullscreen mode

or using

bash LAMP.sh
Enter fullscreen mode Exit fullscreen mode
apache Article's
30 articles in total
Favicon
Power Up Your AWS Game: Create EC2 Instances, Install Apache, and Connect with PowerShell
Favicon
AutoMQ: A Revolutionary Cloud-First Alternative to Kafka
Favicon
Laravel 11: Allowed memory size of 134217728 bytes exhausted (tried to allocate 23085056 bytes)
Favicon
Seamlessly Migrate PostgreSQL to YugabyteDB in Minutes!
Favicon
Apache Log Parser and Data Normalization Application
Favicon
Unlock 10% Discounts in 5 Minutes: Build a Drools Project with Maven
Favicon
[pt-BR] Como criei minha própria imagem Docker do Apache Benchmark para testes de stress em servidores web
Favicon
What Goes Into a Major OSS Release? A CouchDB Story
Favicon
Monitor Apache Ignite in 5 Minutes: Fix Cluster Issues Fast!
Favicon
Mastering Data Routing in Apache Camel: Leveraging the Splitter Pattern
Favicon
Exploring Core Features and Components of Apache Camel
Favicon
Practical Guide to Apache Camel with Quarkus: Building an ETL Application
Favicon
Implementation of Missing Security Header Vulnerability in Apache (Part 2)
Favicon
Join Apache Answer at CommunityOverCode Asia 2024
Favicon
Implementation of Missing Security Header Vulnerability in Apache (Part 1)
Favicon
Installing Apache, PHP, and MySQL on Oracle Linux 8
Favicon
Install LEMP LAMP LLMP LEPP LAPP or LLPP using parameters only
Favicon
Deploying an Application Using Apache as a Web Server
Favicon
My first experience with the LAMP stack
Favicon
Shades of Open Source - Understanding The Many Meanings of "Open"
Favicon
Updating the solr client(org.apache.solr.solr-core) from 8.11.2 to 9.6.0
Favicon
Automating the installation of a LAMP stack on Ubuntu 22.04
Favicon
How to setup an Apache server on Ubuntu 22.04.4 LTS with Virtual hosting
Favicon
Build a Safe and Respectful Community with Answer 1.3.1
Favicon
Apache Spark 101
Favicon
Apply for Apache Answer’s Project at OSPP 2024
Favicon
Understanding Kappa Architecture and Kafka: Empowering Real-Time Data Processing
Favicon
Run Laravel locally on Ubuntu using Apache virtual host
Favicon
Deploy Sendy on AWS EC2 with Apache in Ubuntu
Favicon
Docker LAMP Stack With Composer PSR-4 Autoloading – Apache Server

Featured ones: