← back to Abramsagency
golive-port-fix.sh
28 lines
#!/usr/bin/env bash
# Fix port collision: move abramsagency to a free port on the box + sync the vhost.
set -euo pipefail
SRV=root@45.61.58.125
ssh -o StrictHostKeyChecking=accept-new "$SRV" 'bash -s' <<'EOS'
set -e
DIR=/var/www/agency.agentabrams.com
VHOST=/etc/nginx/conf.d/agency.agentabrams.com.conf
# what currently owns 9788?
echo "== :9788 owner =="; ss -ltnp 2>/dev/null | grep ":9788 " || echo "(nothing? app may be crash-looping)"
# pick a free port in 9600-9699
PORT=""
for p in $(seq 9600 9699); do
if ! ss -ltn 2>/dev/null | grep -q ":$p "; then PORT=$p; break; fi
done
[ -z "$PORT" ] && { echo "NO FREE PORT"; exit 1; }
echo "== chosen free PORT=$PORT =="
# restart our app cleanly on that port
pm2 delete abramsagency >/dev/null 2>&1 || true
cd "$DIR" && PORT=$PORT pm2 start server.js --name abramsagency && pm2 save >/dev/null
sleep 1
echo "== our app healthz on :$PORT (want service:abramsagency) =="; curl -s -m 5 "http://127.0.0.1:$PORT/healthz"; echo
# point the vhost at the new port
sed -i -E "s#proxy_pass http://127.0.0.1:[0-9]+#proxy_pass http://127.0.0.1:$PORT#" "$VHOST"
nginx -t && systemctl reload nginx && echo RELOADED
echo "== through nginx https (want abramsagency JSON) =="; curl -s -m 8 https://agency.agentabrams.com/healthz; echo
EOS