Install script
This commit is contained in:
1 parent
40585c18ac
commit
7af482bf20
3 files changed
+241
No files matched your search
@@ -0,0 +1,14 @@
|
||||
module.exports = {
|
||||
apps: [{
|
||||
name: "llm-proxy-api",
|
||||
script: "node_modules/.bin/next",
|
||||
args: "start -p 3000",
|
||||
env: {
|
||||
NODE_ENV: "production",
|
||||
},
|
||||
instances: 1,
|
||||
autorestart: true,
|
||||
max_memory_restart: "512M",
|
||||
log_date_format: "YYYY-MM-DD HH:mm:ss",
|
||||
}],
|
||||
};
|
||||
Executable
+208
@@ -0,0 +1,208 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# ─── LLM Proxy API — First-time Setup Script ───
|
||||
# Run this on a fresh Proxmox LXC (Debian/Ubuntu) as root.
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m'
|
||||
|
||||
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
|
||||
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
||||
error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
|
||||
|
||||
# ─── Check root ───
|
||||
[[ $EUID -eq 0 ]] || error "Run this script as root."
|
||||
|
||||
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
APP_USER="llmproxy"
|
||||
APP_PORT=3000
|
||||
|
||||
info "Starting LLM Proxy API installation..."
|
||||
|
||||
# ─── 1. System packages ───
|
||||
info "Installing system dependencies..."
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq curl gnupg git build-essential ca-certificates > /dev/null
|
||||
|
||||
# ─── 2. Node.js (LTS via NodeSource) ───
|
||||
if ! command -v node &>/dev/null; then
|
||||
info "Installing Node.js 22 LTS..."
|
||||
curl -fsSL https://deb.nodesource.com/setup_22.x | bash - > /dev/null 2>&1
|
||||
apt-get install -y -qq nodejs > /dev/null
|
||||
fi
|
||||
info "Node.js $(node -v)"
|
||||
|
||||
# ─── 3. MongoDB ───
|
||||
if ! command -v mongod &>/dev/null; then
|
||||
info "Installing MongoDB 7.0..."
|
||||
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
|
||||
gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
|
||||
# Detect distro
|
||||
DISTRO_CODENAME=$(lsb_release -cs 2>/dev/null || echo "jammy")
|
||||
echo "deb [signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] https://repo.mongodb.org/apt/ubuntu ${DISTRO_CODENAME}/mongodb-org/7.0 multiverse" \
|
||||
> /etc/apt/sources.list.d/mongodb-org-7.0.list
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq mongodb-org > /dev/null
|
||||
fi
|
||||
systemctl enable mongod
|
||||
systemctl start mongod
|
||||
info "MongoDB running."
|
||||
|
||||
# ─── 4. Redis ───
|
||||
if ! command -v redis-server &>/dev/null; then
|
||||
info "Installing Redis..."
|
||||
apt-get install -y -qq redis-server > /dev/null
|
||||
fi
|
||||
systemctl enable redis-server
|
||||
systemctl start redis-server
|
||||
info "Redis running."
|
||||
|
||||
# ─── 5. PM2 ───
|
||||
if ! command -v pm2 &>/dev/null; then
|
||||
info "Installing PM2..."
|
||||
npm install -g pm2 > /dev/null 2>&1
|
||||
fi
|
||||
|
||||
# ─── 6. Create app user ───
|
||||
if ! id "$APP_USER" &>/dev/null; then
|
||||
info "Creating system user '$APP_USER'..."
|
||||
useradd -r -m -s /bin/bash "$APP_USER"
|
||||
fi
|
||||
|
||||
# ─── 7. Generate .env if not present ───
|
||||
ENV_FILE="$APP_DIR/.env"
|
||||
if [[ ! -f "$ENV_FILE" ]]; then
|
||||
info "Generating production .env file..."
|
||||
|
||||
NEXTAUTH_SECRET=$(openssl rand -base64 48)
|
||||
ENCRYPTION_KEY=$(openssl rand -hex 32)
|
||||
|
||||
cat > "$ENV_FILE" <<EOF
|
||||
# Database
|
||||
DATABASE_URL=mongodb://localhost:27017/llm_proxy
|
||||
|
||||
# Redis
|
||||
REDIS_URL=redis://localhost:6379
|
||||
|
||||
# Auth
|
||||
NEXTAUTH_URL=http://localhost:${APP_PORT}
|
||||
NEXTAUTH_SECRET=${NEXTAUTH_SECRET}
|
||||
|
||||
# Encryption (AES-256 — auto-generated, do not lose this)
|
||||
ENCRYPTION_KEY=${ENCRYPTION_KEY}
|
||||
|
||||
# App
|
||||
NEXT_PUBLIC_APP_NAME=LLM Proxy
|
||||
NEXT_PUBLIC_APP_URL=http://localhost:${APP_PORT}
|
||||
NODE_ENV=production
|
||||
EOF
|
||||
|
||||
chmod 600 "$ENV_FILE"
|
||||
info ".env created with auto-generated secrets."
|
||||
warn "Edit .env to set your domain in NEXTAUTH_URL and NEXT_PUBLIC_APP_URL if needed."
|
||||
else
|
||||
info ".env already exists, skipping."
|
||||
fi
|
||||
|
||||
# ─── 8. Install dependencies & build ───
|
||||
info "Installing npm dependencies..."
|
||||
cd "$APP_DIR"
|
||||
npm ci --omit=dev 2>&1 | tail -1
|
||||
# Need dev deps for build (tsx, typescript, etc.)
|
||||
npm install 2>&1 | tail -1
|
||||
|
||||
info "Building application..."
|
||||
npm run build
|
||||
|
||||
# ─── 9. Seed database ───
|
||||
info "Seeding database..."
|
||||
npm run db:seed || warn "Seeding failed or already seeded — check output above."
|
||||
|
||||
# ─── 10. Set ownership ───
|
||||
chown -R "$APP_USER":"$APP_USER" "$APP_DIR"
|
||||
|
||||
# ─── 11. PM2 ecosystem file ───
|
||||
PM2_CONFIG="$APP_DIR/ecosystem.config.cjs"
|
||||
cat > "$PM2_CONFIG" <<EOF
|
||||
module.exports = {
|
||||
apps: [{
|
||||
name: "llm-proxy-api",
|
||||
script: "node_modules/.bin/next",
|
||||
args: "start -p ${APP_PORT}",
|
||||
cwd: "${APP_DIR}",
|
||||
env: {
|
||||
NODE_ENV: "production",
|
||||
},
|
||||
instances: 1,
|
||||
autorestart: true,
|
||||
max_memory_restart: "512M",
|
||||
log_date_format: "YYYY-MM-DD HH:mm:ss",
|
||||
}],
|
||||
};
|
||||
EOF
|
||||
chown "$APP_USER":"$APP_USER" "$PM2_CONFIG"
|
||||
|
||||
# ─── 12. Start with PM2 ───
|
||||
info "Starting application with PM2..."
|
||||
sudo -u "$APP_USER" bash -c "cd $APP_DIR && pm2 start ecosystem.config.cjs"
|
||||
sudo -u "$APP_USER" bash -c "pm2 save"
|
||||
|
||||
# ─── 13. PM2 startup on boot ───
|
||||
env PATH="$PATH:/usr/bin" pm2 startup systemd -u "$APP_USER" --hp "/home/$APP_USER" > /dev/null 2>&1 || true
|
||||
|
||||
# ─── 14. Create 'update' alias ───
|
||||
UPDATE_SCRIPT="$APP_DIR/update.sh"
|
||||
cat > "$UPDATE_SCRIPT" <<'SCRIPT'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
cd "$APP_DIR"
|
||||
|
||||
echo "⏳ Pulling latest changes..."
|
||||
git pull --ff-only
|
||||
|
||||
echo "⏳ Installing dependencies..."
|
||||
npm install
|
||||
|
||||
echo "⏳ Building..."
|
||||
npm run build
|
||||
|
||||
echo "⏳ Restarting..."
|
||||
pm2 restart llm-proxy-api
|
||||
|
||||
echo "✅ Update complete."
|
||||
SCRIPT
|
||||
chmod +x "$UPDATE_SCRIPT"
|
||||
chown "$APP_USER":"$APP_USER" "$UPDATE_SCRIPT"
|
||||
|
||||
# Add alias for all users
|
||||
cat > /etc/profile.d/llmproxy-aliases.sh <<EOF
|
||||
alias update='${UPDATE_SCRIPT}'
|
||||
EOF
|
||||
|
||||
# Also add to app user's bashrc
|
||||
grep -q "alias update=" "/home/$APP_USER/.bashrc" 2>/dev/null || \
|
||||
echo "alias update='${UPDATE_SCRIPT}'" >> "/home/$APP_USER/.bashrc"
|
||||
|
||||
# ─── Done ───
|
||||
echo ""
|
||||
info "============================================"
|
||||
info " LLM Proxy API installed successfully!"
|
||||
info "============================================"
|
||||
echo ""
|
||||
info "App running at: http://localhost:${APP_PORT}"
|
||||
info "Default admin: [email protected] / admin123"
|
||||
warn "⚠ Change the admin password immediately!"
|
||||
echo ""
|
||||
info "Commands:"
|
||||
info " update — Pull, build & restart"
|
||||
info " pm2 logs — View logs"
|
||||
info " pm2 status — Check status"
|
||||
info " pm2 restart all — Manual restart"
|
||||
echo ""
|
||||
info "Config: ${ENV_FILE}"
|
||||
info "Update NEXTAUTH_URL and NEXT_PUBLIC_APP_URL with your domain."
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
cd "$APP_DIR"
|
||||
|
||||
echo "⏳ Pulling latest changes..."
|
||||
git pull --ff-only
|
||||
|
||||
echo "⏳ Installing dependencies..."
|
||||
npm install
|
||||
|
||||
echo "⏳ Building..."
|
||||
npm run build
|
||||
|
||||
echo "⏳ Restarting..."
|
||||
pm2 restart llm-proxy-api
|
||||
|
||||
echo "✅ Update complete."
|
||||
Reference in new issue
Block a user