This is a small sample application used to experiment with various application deployment methods.
Data From : https://www.fueleconomy.gov/feg/download.shtml
Live Url : https://mpg2co2.com
The database for this application is hosted on bit.io : https://bit.io/aaronblondeau/mpg2co2
This application consists of an API based on express.
The web frontend is a plain HTML file that utilizes Alpine.JS.
Run the server with:
yarn install
yarn dev
To start locally with PM2
pm2 start ecosystem.config.js
To stop locally with PM2
pm2 stop ecosystem.config.js
- Cloud Compute
- Intel Regular Performance
- New York
- Debian 11
- $3.50 Box
- No Backups
- Added id_ed25519.pub key
- hostname & label : staging.mpg2co2.com
Note - Had to re-apply ssh key in the vultr console because I was unable to ssh to the box after initial boot.
2. Install caddy : https://caddyserver.com/docs/install#debian-ubuntu-raspbian
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
sudo ufw allow 80
sudo ufw allow 443
Also added and applied "web" firewall group (allows : ssh, http, https, 3000) in the vultr console.
sudo apt update
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
sudo apt -y install nodejs
npm install -g yarn pm2
6. Configure PM2 to run on startup : https://pm2.keymetrics.io/docs/usage/startup/
pm2 startup
sudo apt -y install git
Setup key so machine can access git:
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
ssh-keygen -t ed25519 -C "your_email@example.com"
Then copy contents of ~/.ssh/id_ed25519.pub into a new SSH key in GitHub user account settings.
Run a test clone to accept github fingerprint so that PM2 doesn't throw errors during deploys:
cd
git clone git@github.com:aaronblondeau/mpg2co2.git
rm -Rf mpg2co2
mkdir /var/www
Add to .bashrc
export PGHOST=db.bit.io
export PGUSER=HIDDEN
export PGPASSWORD=HIDDEN
export PGPORT=5432
export PGDATABASE=HIDDEN
export PGSSL=yes
These commands are run from your local development environment in the same directory as this readme.
Note, if on Windows, you may get a "spawn sh ENOENT" error. You can resolve this by making sure "sh" is in your path : Unitech/pm2#3839 (comment)
pm2 deploy ecosystem.staging.config.js staging setup
pm2 deploy ecosystem.staging.config.js staging
Do this first, before configuring Caddyfile, so that it can complete the Let's Encrypt SSL setup on the first go.
A : staging -> 104.238.135.191
Back on the server, add the following to /etc/caddy/Caddyfile
staging.mpg2co2.com {
reverse_proxy localhost:3000
}
Reload caddy (run this in /etc/caddy)
sudo caddy reload
Production deploy was done on staging box (different port and caddyfile entry) to save $.
pm2 deploy ecosystem.production.config.js production
For production domain setup A record for @ and www, and did www strip in Caddyfile:
mpg2co2.com {
reverse_proxy localhost:3001
}
www.mpg2co2.com {
redir https://mpg2co2.com{uri}
}
First, create an ssh key
name = ./github_action_key passprase = empty
ssh-keygen -t ed25519 -C "github_deploy_action"
ssh in to application's server and add github_action_key.pub content as new line in ~/.ssh/authorized_keys
Then in GitHub, go to Repo Settings > Secrets > Actions > New Repository Secret
put github_action_key file's contents into repo secret named SSH_PRIVATE_KEY
Connect to server with new key (ssh -i .\github_action_key root@104.238.135.191) and get generated entry from known hosts, add it to repo secret SSH_KNOWN_HOSTS