For this application, we make use of NodeJS and MySQL to power our backend API and database respectively. For our frontend which is used to demonstrate the capabilities of our API, we have employed Apache2 HTTP Server. These tools are available on a variety of operating systems and as a result, this setup can be hosted on a variety of machines.
For our demonstration purposes, we hosted on Ubuntu Server 16.04, making use of Microsoft Azure's virtual machine service. As part of this setup, HTTP traffic on port 80 needs to be allowed. This can be done on the configuration page of the Azure portal or any hosting service you make use of.
For our application, we will be installing:
Additionally we will install Git for deployment purposes.
The following code will install the necessary packages on Ubuntu.
sudo apt-get update
sudo apt-get install apache2 mysql nodejs
# Installs NPM, package manager for Node
sudo apt-get install npm
# Install Git
sudo apt-get install git
Thanks to Apache, our frontend will respond to HTTP requests when someone accesses the website. However, the backend is run using NodeJS and is required to be started otherwise our frontend application will be unable to communicate with the database.
To get around this, we setup a service on the server which starts the NodeJS backend whenever it crashes or the server restarts. Additionally, it will give us an easy way of manually starting/stopping the application ourselves should we need to debug or automate (this will come up later).
First we create the service file
[Unit]
Description=ATOS Time Machine
[Service]
ExecStart=/var/www/backend/app.js
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/backend
Restart=on-failure
RestartSec=10s
[Install]
WantedBy=multi-user.target
Alias=timemachine.service
Then we enable the service:
sudo systemctl enable timemachiine.service
sudo systemctl daemon-reload
sudo systemctl restart timemachine.service
Provided all is working, we will now be able to manage the service using the commands
# Check service status
service timemachine status
# Start service
sudo service timemachine start
# Stop service
sudo service timemachine stop
# Restart service
sudo service timemachine restart
/var/repo.
# Make the parent repo directory
cd /var/
sudo mkdir repo && cd repo/
# Give ourselves ownership of the directory.
# While we're at it, claim /var/www/html where apache2 hosts
sudo chown -R `whoami`:`id -gn` /var/repo
sudo chown -R `whoami`:`id -gn` /var/www/html
# Make the 2 directories for our repositories
mkdir frontend && mkdir backend
Now that we've made the folders for our repositories, we need to create the bare git repos. Cd into both directories and do the following
git init --bare
Now we need to create a hook to deploy the systems. We will start with the frontend:
# First cd into the hooks directory for our frontend
cd /var/repo/frontend/hooks
# Create and edit a file called post-receive
vim post-receive
# Make file executable
chmod +x post-receive
Our post-receive file looks like this:
#!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch..."
git --work-tree=/var/www/html --git-dir=/var/repo/frontend checkout -f
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
This will result in new pushes to the master branch automatically being unpackaged in the /var/www/html directory
which is the root of Apache's host directory. There are also some output messages to provide confirmation.
Now create a backend post-receive hook:
cd /var/repo/backend/hooks
vim post-receive
chmod +x post-receive
This post-receive hook will be different as it needs to manage our NodeJS application:
#!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch..."
git --work-tree=/var/www/backend --git-dir=/var/repo/backend checkout -f
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
chmod +x /var/www/backend/app.js
if (service timemachine status > /dev/null)
then
echo "Service running. Stopping service..."
sudo service timemachine stop > /dev/null
echo "Service stopped."
else
echo "Service not running.."
fi
echo "Installing packages.."
npm --prefix /var/www/backend/ install /var/www/backend
echo "Starting service..."
sudo service timemachine start > /dev/null
if (service timemachine status > /dev/null)
then
echo "Timemachine service started successfully."
else
echo "Timemachine service unable to be started. Please debug."
fi
This script checks if the application service is running and stops it if it is. It will then perform an NPM install in case
any new dependency packages are required for the application and will then start the service.
The frontend and backend can now be deployed to by pushing to it.
# Add remote to your local git clone of the frontend repository
git remote add live ssh://[[user]]@[[server address]]:/var/repo/frontend
git push live master
# For the backend
git remote add live ssh://[[user]]@[[server address]]:/var/repo/backend
git push live master