The apache web app script that was used was gotten from the github repository of "y2o-dev", a member of the "DevOps Trio" Study Group!!!
- AWS Account
- Ubuntu Based EC2 instance
- Edit the inbound rule of the instance to allow ssh traffic from port "22", http from port "80" and a custom TCP from port "3000" for the node js application.
#!/bin/bash
# Installing Dependencies
echo "Installing Packages"
echo "################################################"
sudo apt update && sudo apt install wget unzip apache2 -y > /dev/null
echo
# Start & Enable Services
echo "Start & Enable Services"
echo "################################################"
sudo systemctl start apache2
sudo systemctl enable apache2
echo
#Creating a Temp Directory
echo "Start Artifact Deployment"
echo "################################################"
mkdir -p /tmp/webfiles
cd /tmp/webfiles
echo
wget https://www.tooplate.com/zip-templates/2098_health.zip > /dev/null
unzip 2098_health.zip
sudo cp -r 2098_health/* /var/www/html/
#Bounce Service
echo "Restarting apache2 Service"
echo "################################################"
sudo systemctl restart apache2
echo
#Clean up
echo "Removing Temporary Files"
echo "################################################"
rm -rf /tmp/webfiles
sudo systemctl status apache2
ls -l /var/www/html/
The above script will download the application file, unzip it and copy it to the web root directory "/var/www/html" after which the apache service is restarted.
-
Create a directory "node". The directory contains an index.js file and a "public" folder which houses the index.html file.
-
Populate the index.js file with the content below:
const express = require('express');
const app = express();
const port = 3000;
// Serve static files from the 'public' folder
app.use(express.static('public'));
app.get('/', (req, res) => {
res.send('Hello, world!');
});
// app.listen(port, () => {
// console.log(`App listening at http://localhost:${port}`);
// });
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
and the "index.html" file within the public folder with;
<!DOCTYPE html>
<html>
<head>
<title>My Node.js App</title>
</head>
<body>
<h1>Welcome to my Node.js app!</h1>
<p>This is a simple node.js app with which I practised case-statement.</p>
</body>
</html>
- In the same node directory, create your "node.sh" file (and make it executable using
chmod a+x node.sh
). This file contains the concatenated commands to set up your node.js application.
#!/bin/bash
PACKAGE_JSON_PATH="/home/ubuntu/node/package.json"
# Update and upgrade packages
sudo apt update && sudo apt upgrade -y
echo
# Install node.js,npm and pm2(a process manager)
echo "Installing node js, npm and pm2....."
sudo apt install nodejs -y
sudo apt install npm
sudo npm install pm2 -g
echo
# Verify installation
node --version
npm --version
pm2 --version
echo
# Initialize the node js project and install express framework
npm init
npm install express -y
echo
# Using sed to modify the npm script to use pm2 as the nodejs process manager:
echo "Beginning substitution with sed command"
sed -i 's/"scripts": {/"scripts": {\n "start": "pm2 start index.js",/' "$PACKAGE_JSON_PATH"
# Start the nodejs app
echo "starting the application...."
npm run start
-
Create a "case-statement.sh" file. This script will be executed to deploy your node.js application and apache web app.
-
Make the script executable by changing file permissions;
chmod +x case-statement.sh
-
Here's my case statement script ;
read -p "Hi, Welcome to meempharm Hub! Your name please: " Name
echo "
Hi "${Name}, which web application would you like to deploy?"
1) Node.js
2) Apache
"
read -r answer
case $answer in
1)
echo " Change directory to node and run the script"
cd /home/ubuntu/node && ./node.sh
;;
2)
echo "Running the apache script"
./script.sh
;;
*) echo "You are only allowed to select from the above listed choices." ;;
esac
The script changes directory to the node directory and the "node.sh" script therein is executed.
Within the node.sh script, sed command was used to insert a start script which allows pm2 to start the node.js app in the background.
Shown below, pm2 successfully started the node.js application.
The node.js app as shown in the web browser;
The apache web app is as shown;
THE END