[comment]: # "markdown: { smartypants: true }" ### Deploying # Node.js #### in 2022
a (very simple) beginner's guide
# ðŸ¤
https://beyondcodebootcamp.github.io/presos/deploying-nodejs-in-2022/
- Caddy: https://webinstall.dev/caddy - DuckDNS: https://duckdns.org - Digital Ocean: https://m.do.co/c/ad3b82be23f6 - Express Starter: https://github.com/BeyondCodeBootcamp/node-express-starter/ - Name.com: https://www.name.com/referral/13d0ac - Node: https://webinstall.dev/node - Serviceman: https://webinstall.dev/serviceman - Webi: https://webinstall.dev/
![](https://coolaj86.com/assets/media/coolaj86-2017.jpg) AJ ONeal
[@\_beyondcode](https://twitter.com/@_beyondcode)
[twitch.tv/coolaj86](https://twitch.tv/coolaj86)
Technophobic Technologist Dangerous Wrong Thinker Equal Opportunity Offender
https://nodejs.org/en/about/resources/
## Overview 1. Public Key (for Login) 2. Server (VPS) 3. DNS (Domain Name) 4. App "User" 5. Launcher (systemd)
... and YOUR PROJECT
(duh!)
(Node.js + Git)
## Bonus Material
(there will be some bonus material)
## 1. Public Key (secure)
## SSH Public Key
webinstall.dev/ssh-pubkey
(ssh-keygen)
```bash curl -f https://webinstall.dev/ssh-pubkey | bash ```
```txt ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDC6aziziaVOvXTaHd5t+ez6vtQBIBbsHmZW/aEZgiaFxQ/IcdusfPqzyCCC7c6lhck2xDYgtg6mF4ASZ5rg0n7YvVlI70ULQE/aMp9n/p/Nb1p7qDpJLpjblFOR/p9Ewr/SI9yo44N5ElFKMvICG1LVI1uO9KEfAKs2gazRXqO6cGt0jwLwmyHdBLho8wRj89pHCXINKJ3K8SdfCCVJ/csG1IIk7Vlf+CvOInkGY2hCU1iCsUVL5fqG3+5nnaTRJQUBR2npGYJ0M4d1UjpQSKrkQyDheCJDltu4y2jpyH13y9CEW8GtIJOF4iIu+eScEdpLL6nJ8cV+Nui9vBSV+Wp app@beta.rootprojects.org ```
```txt # My First Public Key ssh-rsa
```
## [Digital Ocean](https://m.do.co/c/ad3b82be23f6)
$100 / 60 days free
| $5/month | 1 vCPU / 1GB / 20GB | | -------- | ----------------------- | | $1/month | Backups | | $0 | 100% Uptime w/ Failover | | $0 | IPv6 + Monitoring | | $0 | Secure SSH Login |
### Other VPSes - Vultr - OVH - Scaleway
## Domain Services | Name.com | .com, etc |
affiliate link
| | -------- | ------------ | ----------------------------------------------------------------- | | DuckDNS | .duckdns.org |
duckdns.org
|
## DNS Setup - A - AAAA
## User Setup Create the `app` user with your Public Key.
```bash ssh root@YOUR-PROJECT.duckdns.org \ 'curl https://webinstall.dev/ssh-adduser | bash' ```
```bash ssh app@YOUR-PROJECT.duckdns.org ```
## Project Setup - git - node - YOUR-PROJECT
### Git Setup
Person Access Token: https://github.com/settings/tokens
```bash export TOKEN=ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ``` ```bash git --version || sudo apt install -y git ``` ```bash git config --global url."https://api:$TOKEN@github.com/".insteadOf "https://github.com/" git config --global url."https://ssh:$TOKEN@github.com/".insteadOf "ssh://git@github.com/" git config --global url."https://git:$TOKEN@github.com/".insteadOf "git@github.com:" ```
(todo: git-credentials)
### Clone Project
```bash git clone ssh://git@github.com:YOUR-ORG/YOUR-PROJECT.git pushd ./YOUR-PROJECT/ ```
### Project Config ```bash npm ci --only=production ```
### Full-Blown IDE ```bash webi vim-essentials ```
(todo: vim-utf8)
### Project ENVs ```bash rsync -avhP ./example.env ./.env vim ./.env ```
12factor.net
## Launcher
```bash npm start ```
### serviceman
(systemd on Linux)
```bash sudo env PATH="${PATH}" \ serviceman add --username "$(whoami)" \ --cap-net-bind -- \ npm start ```
(todo: add-service)
## Bonus Material
## Just the Basics
(avoid useless abstractions, learn useful skills)
(2mb of JS for [6 lines of git](https://therootcompany.com/blog/how-github-does-smarter-shallow-clones/))
"Linux" vs "Deploy Node.js App"
"Web Security" vs "Deploy Node.js App"
"High-Availability Kubernetes Cluster Containerization" vs "Deploy Node.js App"
"Black Box" vs "Deploy Node.js App"
## npm start ```txt package.js.main ``` ```bash server.js app.js ``` ```bash NODE_ENV=development PORT=3000 ```
## Caddy ```bash webi caddy ``` ```bash mkdir -p ~/srv/ touch ~/srv/Caddyfile ```
Caddyfile QuickStart
https://webinstall.dev/caddy
```bash sudo env PATH="${PATH}" \ serviceman add --username "$(whoami)" \ --name caddy --cap-net-bind -- \ caddy run --config ./srv/Caddyfile ```
## Logs
Enable `journalctl`: ```bash sudo systemctl enable systemd-journald sudo systemctl restart systemd-journald ```
If that didn't work: ```bash sudo mkdir -p /var/log/journal sudo chown -R root:systemd-journal /var/log/journal/ sudo sed -i 's/#\?Storage=.*/Storage=persistent/' /etc/systemd/journald.conf sudo systemctl restart systemd-journald ``` https://gist.github.com/JPvRiel/b7c185833da32631fa6ce65b40836887
```bash sudo systemctl restart YOUR-PROJECT sudo journalctl -xefu YOUR-PROJECT ```
## SSH Config
```ssh Host YOUR-PROJECT Hostname YOUR-PROJECT.duckdns.org Host * User app ```
## Save 💰 w/ Swap Space
https://webinstall.dev/vps-addswap
```bash export NODE_OPTIONS="--max-old-space-size=4096" ``` ```bash sudo fallocate -l 4G /var/swapfile sudo chmod 0600 /var/swapfile sudo mkswap /var/swapfile ```