End-to-End Static Website Auto Deployment Using Bash Scripts


Tired of manually copying and pasting files every time you deploy your application? You're in the right place. In this tutorial, we'll learn how to write a Bash script that automates the deployment of static files to a server simply by providing the path to a zipped file. This will work on every server.
Prerequisite(s)
- Basic knowledge of bash script
Objective(s)
- Write a Bash Script that auto deploys a static website
The first thing to do is to create a file and name it website_deploy.sh. You can use any editor you like but in this tutorial I will be using vi.
touch website_deploy.sh
- Open the file
sudo vi website_deploy.sh
Add shebang
#!/bin/bash
- Declare important variables
The variable contains the list of packages (e.g., nginx
, wget
) to be installed, the target service (nginx
), and the temporary directory path where the zipped files will be extracted during deployment.
#Variable Declaration
PACKAGE="nginx wget unzip"
SVC="nginx"
TEMP_DIR="/tmp/webfiles"
- Installation of packages
We will install all the applications declared in the package variable. you will be wondering what > /dev/null means. It means I don’t want the output to display on the terminal
# Installing Dependencies
echo "######################################################"
echo "Installing Dependencies"
echo "#######################################################"
sudo apt install $PACKAGE -y > /dev/null
- Start and Enable nginx
# Start & Enable Service
echo "######################################################"
echo "Start & Enable nginx"
echo "######################################################"
sudo systemctl start $SVC
sudo systemctl enable $SVC
echo
- Create a temporary Directory for artifact
You should be confused of what $1 means. When running the file you will need to pass 2 arguments. The path to the zipped file and the directory where the zipped files will be extracted to. $1 will get the path to the zipped file argument while $2 will get the directory where the zipped file will be extracted to.
# Creating Temp Directory
echo "######################################################"
echo "Starting Artifact Deployment"
echo "######################################################"
mkdir -p $TEMP_DIR
cd $TEMP_DIR
wget $1 > /dev/null
echo
Unzip the download zipped file
copy the unzipped file the /var/www/html
unzip $2.zip > /dev/null
sudo cp -r $2/* /var/www/html/
echo "######################################################"
echo "Artifact Deployment Completed"
echo "######################################################"
echo
- Clean up
echo "######################################################"
echo "Cleanup"
echo "######################################################"
# Cleanup
rm -rf $TEMP_DIR
echo
Putting everything together
#!/bin/bash
#Variable Declaration
PACKAGE="nginx wget unzip"
SVC="nginx"
TEMP_DIR="/tmp/webfiles"
# Installing Dependencies
echo "######################################################"
echo "Installing Dependencies"
echo "#######################################################"
sudo apt install $PACKAGE -y > /dev/null
# Start & Enable Service
echo "######################################################"
echo "Start & Enable nginx"
echo "######################################################"
sudo systemctl start $SVC
sudo systemctl enable $SVC
echo
# Creating Temp Directory
echo "######################################################"
echo "Starting Artifact Deployment"
echo "######################################################"
mkdir -p $TEMP_DIR
cd $TEMP_DIR
wget $1 > /dev/null
echo
unzip $2.zip > /dev/null
sudo cp -r $2/* /var/www/html/
echo "######################################################"
echo "Artifact Deployment Completed"
echo "######################################################"
echo
echo "######################################################"
echo "Cleanup"
echo "######################################################"
# Cleanup
rm -rf $TEMP_DIR
echo
Save by pressing escape and type wq!
Press enter
Change the permission of the file
sudo chmod +x ./website_deploy.sh
To run the application
./website_deploy.sh https://www.tooplate.com/zip-templates/2137_barista_cafe.zip 2137_barista_cafe
Copy the ip address of the server and paste on your browser
If you are using aws then you need to open port 80
Subscribe to my newsletter
Read articles from Oshaba Samson directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Oshaba Samson
Oshaba Samson
I am a software developer with 5 years + experience. I have working on web apps ecommerce, e-learning, hrm web applications and many others