Goodnews Azonubi
Backend/DevOps Engineer
6 min read
Automating Deployment of a Laravel LAMP Stack Application using Bash Script introduces an efficient method for deploying web applications built with Laravel, a popular PHP framework known for its elegant syntax and powerful features.
In this article we’ll explore automating the deployment of a Laravel-based web application on a LAMP (Linux, Apache, MySQL, PHP) stack using Bash Scripting. By automating this process, we can ensure consistency, reduce manual errors, and speed up deployment times. Whether you’re a developer aiming to simplify deployment or an operations engineer looking to streamline processes, this guide will walk you through the steps to automate your Laravel LAMP stack deployments effectively.
A Bash script is a text file containing a series of commands that are executed by the Bash shell (Bourne Again SHell), which is the default command-line interpreter for Unix-like operating systems, including Linux and macOS. Bash scripts are commonly used for automating repetitive tasks, system administration, and complex command-line operations. They can be executed directly from the command line or scheduled to run at specific times using cron jobs or other scheduling tools.
Step 1: Create a script file laravel.sh and make it an executable.
touch laravel_app.sh && chmod +x laravel_app.sh
We will be using vim as our text editor but you can use nano if that’s what you’re familiar with
Step 2: Open the file in a text editor and press "i" to enter insert mode.
vim laravel_app.sh
Step 3: Let’s start writing our script first by declaring our shebang.
#!/bin/bash
Our script will be divided into functions to handle different tasks. You can declare your function in two ways
function setup() {
}
setup() {
}
and it consists of a
function declaration — “function” (optional)
function name — “setup”
function parameters — “( )” optional
code block — “{ }”
A function will not execute unless it is called. To call a function simply type out the function name (setup)
setup
Step 4: We need a way to keep track of the line our script is currently running so let’s manipulate the echo command output to make it interactive while running the script. Write a function that changes the colour of our echo output.
# Function to display messages in gold
function gold_echo() {
echo -e "\e[38;5;220m$@\e[0m"
}
Step 5: Write a function that installs the lamp stack (Apache, MySQL and PHP).
function install_lamp() {
# Install PHP
gold_echo "---------------------update php repository-----------------------"
sudo apt update
sudo add-apt-repository ppa:ondrej/php -y
gold_echo "-----------------------Installing Php8.2-----------------------------------------"
sudo apt install php8.2 -y
gold_echo "-------------------------------------Installing php dependencies----------------------------"
sudo apt install php8.2-curl php8.2-dom php8.2-mbstring php8.2-xml php8.2-mysql zip unzip -y
gold_echo "-------------------------- php done ----------------------------------"
#Install Apache web server
gold_echo "----------------------------Installing Apache--------------------------------------------------"
sudo apt install apache2 -y
sudo apt update
sudo systemctl restart apache2
#Install Mysql-server
gold_echo "------------------------------------Installing mysql-server----------------------------------------------"
sudo apt install mysql-server -y
}
Check if there are other PHP extensions that are not currently present in your server.
php -m
Laravel PHP extensions requirements:
✅ Ctype PHP Extension ❌ cURL PHP Extension ❌ DOM PHP Extension ✅ Fileinfo PHP Extension ✅ Filter PHP Extension ✅ Hash PHP Extension ❌ Mbstring PHP Extension ✅ OpenSSL PHP Extension ✅ PCRE PHP Extension ✅ PDO PHP Extension ✅ Session PHP Extension ✅ Tokenizer PHP Extension ❌ XML PHP Extension
The items marked in red were not currently present on my server, so I'll be install them.
sudo apt install php8.2-curl php8.2-dom php8.2-mbstring php8.2-xml php8.2-mysql zip unzip -y
We also need to install zip and unzip which will be needed later by composer
Step 6: Write a function to install and setup composer
function composer_setup() {
cd ~
if [ -d "$HOME/composer" ]; then
gold_echo "--------------------------------------Composer Directory Exists--------------------------------------------"
else
mkdir composer
cd composer
gold_echo "-------------------Directory created successfully--------------------------------"
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
gold_echo "------------------- Composer Added Successfully-------------------------"
fi
}
Step 7: Write a function that clones and setup laravel app
function setup_laravel_app() {
cd /var/www/
sudo rm -r ./*
sudo git clone https://github.com/laravel/laravel
sudo chown -R $USER:$USER laravel
cd laravel
#Install dependencies using composer
composer install
cp .env.example .env
php artisan key:generate
sudo chown -R www-data bootstrap/cache
sudo chown -R www-data storage
}
GitHub repo to laravel app
Step 8: Write a function that configures MySQL Server (create database and user)
function conf_mysql() {
cd /var/www/laravel
gold_echo "-------------Setup mysql database and user--------------------------------"
# Configure MySQL database
sudo mysql -uroot -e "CREATE DATABASE laravel_db;"
sudo mysql -uroot -e "CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY '000000';"
sudo mysql -uroot -e "GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';"
sudo mysql -uroot -e "FLUSH PRIVILEGES;"
gold_echo "-----------------Done with database setup--------------"
gold_echo "------------------Editing .env file--------------------"
sed -i 's/DB_CONNECTION=sqlite/DB_CONNECTION=mysql/' .env
sed -i 's/# DB_HOST=127.0.0.1/DB_HOSTS=127.0.0.1/' .env
sed -i 's/# DB_PORT=3306/DB_PORT=3306/' .env
sed -i 's/# DB_DATABASE=laravel/DB_DATABASE=laravel_db/' .env
sed -i 's/# DB_USERNAME=root/DB_USERNAME=laravel_user/' .env
sed -i 's/# DB_PASSWORD=/DB_PASSWORD=000000/' .env
gold_echo "-------Migrating database--------"
php artisan migrate
}
Username: laravel_user
Database name: laravel_db
Password: 000000
Step 9: Write a function to create a new Apache virtual host to point our new laravel app.
function apache_conf() {
#Setup Virtual host for app
cd ~
sudo tee /etc/apache2/sites-available/laravel.conf <<EOF
<VirtualHost *:80 *:3000>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/laravel/public/
<Directory /var/www/laravel/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
cd ~
sudo a2dissite 000-default.conf
sudo a2enmod rewrite
sudo a2ensite laravel.conf
sudo systemctl restart apache2
}
Step 10: Write a function to call our functions
#Functions
function main() {
install_lamp
composer_setup
setup_laravel_app
conf_mysql
apache_conf
}
#Execute All Functions
main
Save and exit
Execute script
./laravel_app.sh
Output:
This guide has provided a step-by-step approach to automating the deployment of a Laravel LAMP stack application using Bash script on an Ubuntu server. By automating these processes, developers and operations engineers can ensure consistency, reduce errors, and expedite deployment times, leading to a more efficient and streamlined application environment.