Skip to content

Deployment

vps guide

idk about serverless / platform deployments (cf workers, vercel, etc) … and honestly i don’t give a shit, at least not right now

we’re deploying on a private vps like our grandparents used to do

astro using node ssr adapter…how hard could it be?

dw deployment guide

  • (see server ref vps astro deployment instructions)
  • best practices: make sure you’re backing up your db
    • ref (good idea) - s3mysqlbackup solution (purhost, reininghost, eqkh)
      • s3 media/static files backup script too (using on rh & purhost)

honestly i’m super stoked at how easy this can be to set up on a vps. you just need ssh & node (nvm & pm2, in my case…super easy and super reliable) this will work on “modern” infra too (host it with vercel or cloudflare, idgaf)

?? where do you deploy it? wherever the f you want

  • railway, render (or like netlify, vercel, fly, etc, idgaf)
  • your own vps!

for this example, let’s say the app is called dwsite

  • ssh setup for user (not root)
  • ssh as user
    • install nvm for user
    • install pm2 for user
    • add all the ecosystem.config stuff (pm2 config) for astro locally (ok to put in repo, shouldn’t ever need to change tho)
    • add codebase via git (set up .env w/ correct production settings)
  • pm2 start ecosystem.config.cjs
    • pm2 save
    • pm2 startup
      • [run the command it gives you as root]
      • will be something like sudo env PATH=$PATH:/home/dwsite/.nvm/versions/node/v20.19.4/bin /home/dwsite/.nvm/versions/node/v20.19.4/lib/node_modules/pm2/bin/pm2 startup systemd -u dwsite --hp /home/dwsite
      • use the “bin” version of pm2 (ex, replace ‘/home/dwsite/.nvm/versions/node/v20.19.4/lib/node_modules/pm2/bin/pm2’ with ‘/home/dwsite/.nvm/versions/node/v20.19.4/bin/pm2’)
      • ugh this part sucks, i hate it
        • systemctl status pm2-dwsite — if it gives you shit:
          • systemctl stop pm2-dwsite || true
          • # replace lib/node_modules/... with bin/pm2 on ExecStart/Reload/Stop
            sed -i 's#/home/dwsite/.nvm/versions/node/v20.19.4/lib/node_modules/pm2/bin/pm2#/home/dwsite/.nvm/versions/node/v20.19.4/bin/pm2#g' /etc/systemd/system/pm2-dwsite.service
            # add a small delay after start (only once; safe to re-run)
            grep -q '^ExecStartPost=' /etc/systemd/system/pm2-dwsite.service || \
            sed -i '/^ExecStart=.*resurrect/a ExecStartPost=/bin/sleep 1' /etc/systemd/system/pm2-dwsite.service
            # optional: reduce restart hammering
            grep -q '^RestartSec=' /etc/systemd/system/pm2-dwsite.service || \
            sed -i '/^Restart=on-failure/a RestartSec=1' /etc/systemd/system/pm2-dwsite.service
          • systemctl daemon-reload
            systemctl enable pm2-dwsite
            systemctl start pm2-dwsite
            systemctl status pm2-dwsite -l
            journalctl -u pm2-dwsite -n 50 --no-pager
    • pm2 save (run again, as user)
  • .htaccess proxy
    Terminal window
    DirectoryIndex disabled # disable default redirect to index.php
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RequestHeader set X-Forwarded-Host "%{HTTP_HOST}e"
    RewriteRule ^/?(.*)$ http://localhost:3669/$1 [P,L]

and if it goes offline

  • cd /home/dwsite/astro // (directory where the site is deployed on your server)
  • su dwsite
  • pm2 start ecosystem.config.cjs
  • pm2 save

init deploy notes

  • see lwoa deploy.sh script
  • need to run npm run init (via cli) when installing the site on the server
    • ?? can we do this w/ cf/serverless?
    • OR should we recommend just dumping a copy of the dev db?
  • all the pm2 stuff too for vps (ecosystem.config.cjs in lwoa)

field guide - deployment

  • never thought i’d see the day when node apps are easier to deploy than php apps
  • if you use cpanel do this (whatever the solution is)
  • otherwise use pm2 on a vps
  • or use railway or fly or the other modern node hosts
    • this shit is trivially easy to set up
    • git push and don’t worry about it

??

dw - “deploy to render” solution

  • recipe or whatever to do a simple install on render
  • instructions for how to work w/ this (ftp or set up your own env locally idk)
  • add a button to the readme

./deploy.sh

#!/bin/bash
GIT_BRANCH="main"
COMMIT_MESSAGE="Updates - $(date +"%Y-%m-%d %T")"
SSH_USER="dwsite"
SSH_SERVER="xxx.xxx.xxx.xxx"
SSH_PORT="22"
DEPLOYMENT_PATH="/home/dwsite/astro"
# (build step optional, uncomment to enable)
## Build FE assets
# npm run build
## Add new files to repo
git add --all
## Prompt for commit message (and provide a default)
echo "Enter Git commit message (default: $COMMIT_MESSAGE)"
read NEW_MESSAGE
[ -n "$NEW_MESSAGE" ] && COMMIT_MESSAGE=$NEW_MESSAGE
git commit -am "$COMMIT_MESSAGE"
## Push to origin branch
git push origin $GIT_BRANCH
## Pull on remote via ssh
ssh $SSH_USER@$SSH_SERVER -p $SSH_PORT -t "cd $DEPLOYMENT_PATH && git restore package-lock.json && git pull origin $GIT_BRANCH && npm install && npm run build && npm run migrate && pm2 reload lwoa"
exit

./monitor.sh

#!/bin/bash
SSH_USER="dwsite"
SSH_SERVER="xxx.xxx.xxx.xxx"
SSH_PORT="22"
## just monitoring pm2 from the home dir
## Pull on remote via ssh
ssh $SSH_USER@$SSH_SERVER -p $SSH_PORT -t "pm2 logs"
exit