Skip to content

Calculate Vehicle Emissisons in Pounds of Carbon Per Mile

Notifications You must be signed in to change notification settings

aaronblondeau/mpg2co2

Repository files navigation

mpg2co2

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

Database

The database for this application is hosted on bit.io : https://bit.io/aaronblondeau/mpg2co2

Development

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

Hosting

Local PM2

To start locally with PM2

pm2 start ecosystem.config.js

To stop locally with PM2

pm2 stop ecosystem.config.js

Hosting on Vultr w/ Caddy and PM2

Staging

1. Deploy new instance
  • 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.

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
3. Configure firewall
sudo ufw allow 80
sudo ufw allow 443

Also added and applied "web" firewall group (allows : ssh, http, https, 3000) in the vultr console.

4. Install node
sudo apt update
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
sudo apt -y install nodejs
5. Install pm2 and yarn
npm install -g yarn pm2
6. Configure PM2 to run on startup : https://pm2.keymetrics.io/docs/usage/startup/
pm2 startup
7. Install git and setup ssh key
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
8. Create app directory
mkdir /var/www
9. Configure env vars

Add to .bashrc

export PGHOST=db.bit.io
export PGUSER=HIDDEN
export PGPASSWORD=HIDDEN
export PGPORT=5432
export PGDATABASE=HIDDEN
export PGSSL=yes
10. Run PM2 setup and deploy

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
11. Setup DNS records

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

12. Configure caddyfile reverse proxy

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

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}
}

Github Action Setup Notes

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

About

Calculate Vehicle Emissisons in Pounds of Carbon Per Mile

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published