From 5d36c635cbc2873dc43f514ad7aebca5ae912559 Mon Sep 17 00:00:00 2001 From: Renato Date: Tue, 28 Jul 2026 01:29:11 +0200 Subject: [PATCH] fix: swap creation + China npm mirror for 1GB VPS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Creates 1GB swap file before build (prevents OOM on low-RAM VPS) - Auto-detects China network block on npmjs.org → switches to npmmirror - deploy/setup.sh: minimal script for pre-built tarball deployment --- deploy/setup.sh | 131 ++++++++++++++++++++++++++++++++++++++++++++++++ install.sh | 21 ++++++++ 2 files changed, 152 insertions(+) create mode 100644 deploy/setup.sh diff --git a/deploy/setup.sh b/deploy/setup.sh new file mode 100644 index 0000000..35cdc2b --- /dev/null +++ b/deploy/setup.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash +set -euo pipefail + +# ────────────────────────────────────────────── +# worst-scan-web — VPS minimal setup +# Run AFTER: scp deploy.tar.gz to ~/ and extract +# tar xzf ~/deploy.tar.gz -C ~/worst-scan-web +# ────────────────────────────────────────────── + +APP_DIR="${HOME}/worst-scan-web" +SERVICE_NAME="worst-scan-web" +CADDYFILE="/etc/caddy/Caddyfile" + +RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; NC='\033[0m' +log() { printf "${GREEN}✓${NC} %s\n" "$1"; } +warn() { printf "${YELLOW}⚠${NC} %s\n" "$1"; } +err() { printf "${RED}✗${NC} %s\n" "$1"; } +info() { printf "${CYAN}→${NC} %s\n" "$1"; } + +# ── Sudo ────────────────────────────────────── +sudo -v || (err "sudo required" && exit 1) +SU() { echo 'Wlillidan1.' | sudo -S "$@"; } + +# ── Swap (prevent OOM during compile) ───────── +if [ "$(free -m | awk '/^Swap:/{print $2}')" -lt 512 ]; then + info "Creating 1GB swap file..." + SU dd if=/dev/zero of=/swapfile bs=1M count=1024 status=none + SU chmod 600 /swapfile + SU mkswap /swapfile >/dev/null + SU swapon /swapfile + if ! grep -q /swapfile /etc/fstab 2>/dev/null; then + echo '/swapfile none swap sw 0 0' | SU tee -a /etc/fstab >/dev/null + fi + log "Swap created" +fi + +# ── Node.js + Caddy ────────────────────────── +if ! command -v node &>/dev/null || [ "$(node -v | cut -d. -f1 | tr -d v)" -lt 22 ]; then + info "Installing Node.js 22..." + curl -fsSL https://deb.nodesource.com/setup_22.x | SU bash + SU apt install -y -qq nodejs + log "Node.js $(node -v) installed" +fi + +if ! command -v caddy &>/dev/null; then + info "Installing Caddy..." + SU apt install -y -qq debian-keyring debian-archive-keyring apt-transport-https + curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/setup.deb.sh' | SU bash + SU apt install -y -qq caddy + log "Caddy installed" +fi + +# ── China npm mirror ───────────────────────── +npm config set registry https://registry.npmmirror.com + +# ── App dir ────────────────────────────────── +mkdir -p "$APP_DIR/data" + +# ── .env ───────────────────────────────────── +if [ ! -f "$APP_DIR/.env" ]; then + cat > "$APP_DIR/.env" << 'ENV' +API_BASE_URL=http://127.0.0.1:8080/api/v1 +API_KEY= +WEB_PASSWORD= +WEBHOOK_SECRET= +DB_PATH=/home/ren/worst-scan-web/data/worst-scan.db +COVERS_DIR=/home/ren/worst-scan-web/data/covers +ENV + log ".env created" +fi + +# ── systemd service ────────────────────────── +SU tee "/etc/systemd/system/$SERVICE_NAME.service" > /dev/null </dev/null; then + warn "Caddy already configured" +else + SU tee -a "$CADDYFILE" > /dev/null </dev/null || true +SU systemctl restart caddy + +# ── Health check ───────────────────────────── +info "Waiting for web..." +for i in $(seq 1 15); do + if curl -fsS http://127.0.0.1:3000/api/health >/dev/null 2>&1; then + log "Web is healthy"; break + fi + sleep 2 +done + +IP=$(curl -fsSL http://checkip.amazonaws.com 2>/dev/null || echo "localhost") +printf "\n${GREEN}WORST-SCAN-WEB READY${NC}\n" +printf " http://%s/setup\n" "$IP" +printf "\n" diff --git a/install.sh b/install.sh index a4a003a..c2a34d7 100755 --- a/install.sh +++ b/install.sh @@ -32,6 +32,20 @@ bail() { err "Unsupported OS: $OS. Install manually: $1"; exit 1; } # ── Sudo check ───────────────────────────────── sudo -v || (err "sudo required" && exit 1) +SU() { echo "$SUDO_PASS" | sudo -S "$@" 2>/dev/null || sudo "$@"; } + +# ── Swap (prevent OOM on low-RAM VPS) ────────── +if [ "$(free -m | awk '/^Swap:/{print $2}')" -lt 512 ]; then + info "Creating 1GB swap file..." + SU dd if=/dev/zero of=/swapfile bs=1M count=1024 status=none + SU chmod 600 /swapfile + SU mkswap /swapfile >/dev/null + SU swapon /swapfile + if ! grep -q /swapfile /etc/fstab 2>/dev/null; then + echo '/swapfile none swap sw 0 0' | SU tee -a /etc/fstab >/dev/null + fi + log "Swap created (1GB)" +fi # ── git + curl ───────────────────────────────── for cmd in git curl; do @@ -84,6 +98,13 @@ else log "Node.js $(node -v) already installed" fi +# ── npm mirror (auto-detect China) ────────────── +if curl -sI --connect-timeout 3 https://registry.npmjs.org/ 2>&1 | grep -q "timed out\|Connection refused\|Could not resolve\|Network"; then + info "npmjs.org unreachable — switching to China mirror" + npm config set registry https://registry.npmmirror.com + log "Using npmmirror.com" +fi + # ── Caddy ──────────────────────────────────────── if ! command -v caddy &>/dev/null; then info "Installing Caddy..."