Servers

A live server can be easily set up to serve over local http port to the internet at droplet.bci4kids.ca/service with no additional DNS setup required.

Application1

As the communal ‘git’ user,

mkdir ~/my-service
pm2 start <entry_script> -n <service_name>
More About PM2

You can use the following commands to manage applications running on pm2, providing an application name, process id, or “all” where relevant. Note: pm2 processes are tied to the user account that started them.

pm2 start
pm2 list
om2 stop
pm2 delete
pm2 restart

By default, processes will be killed on a system shutdown/reboot.
To address this, you can add a pm2 startup script:

pm2 startup

If you already have a startup script, new processes are added to it once started by saving the list

pm2 save

Nginx2

sudo vi /etc/nginx/sites-available/droplet.bci4kids.ca
server {
  # base configuration and other locations
  ...
  location /service/ {
    proxy_pass http://localhost:port_number/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
  ...
  # certbot certificate block
  ...
}
sudo nginx -t
sudo systemctl restart nginx

CI/CD

Repository setup will differ depending on your desired build process. I have generally preferred to keep a separate bare repository by convention, but that is a matter of style.

Post-Receive Hook Example3

#!/bin/bash

TARGET="$HOME/my-service"
GIT_DIR="$HOME/my-service.git"

PROCESS=service

echo "Push received, deploying..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f

cd $TARGET
echo "installing node packages"
npm install

echo "restarting ${PROCESS} process"
pm2 restart process

References


  1. PM2 ↩︎

  2. Setting up NginX Server Blocks ↩︎

  3. Deploying Code with a Git Hook ↩︎