Update install.sh
This commit is contained in:
1 parent
72b6593384
commit
bc8be00bd4
1 file changed
+96
-39
+96
-39
@@ -2,7 +2,7 @@
|
||||
set -euo pipefail
|
||||
|
||||
# ─── LLM Proxy API — First-time Setup Script ───
|
||||
# Run this on a fresh Proxmox LXC (Debian/Ubuntu) as root.
|
||||
# Run this on a fresh Proxmox LXC (Ubuntu 24.04) as root.
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
@@ -17,15 +17,15 @@ error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
|
||||
[[ $EUID -eq 0 ]] || error "Run this script as root."
|
||||
|
||||
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
APP_USER="llmproxy"
|
||||
APP_PORT=3000
|
||||
CREDENTIALS_FILE="$APP_DIR/.credentials"
|
||||
|
||||
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
|
||||
apt-get install -y -qq curl gnupg git build-essential ca-certificates lsb-release > /dev/null
|
||||
|
||||
# ─── 2. Node.js (LTS via NodeSource) ───
|
||||
if ! command -v node &>/dev/null; then
|
||||
@@ -50,7 +50,53 @@ systemctl enable mongod
|
||||
systemctl start mongod
|
||||
info "MongoDB running."
|
||||
|
||||
# ─── 4. Redis ───
|
||||
# ─── 4. MongoDB auth & remote access ───
|
||||
MONGO_PASS=$(openssl rand -base64 24 | tr -d '/+=')
|
||||
MONGO_USER="llmproxy"
|
||||
MONGO_DB="llm_proxy"
|
||||
|
||||
# Check if auth is already configured
|
||||
if ! mongosh --quiet --eval "db.runCommand({connectionStatus:1})" admin 2>/dev/null | grep -q "authenticatedUsers" 2>/dev/null || \
|
||||
! grep -q "authorization: enabled" /etc/mongod.conf 2>/dev/null; then
|
||||
|
||||
info "Configuring MongoDB authentication & remote access..."
|
||||
|
||||
# Create user before enabling auth
|
||||
mongosh --quiet <<MONGOEOF
|
||||
use admin
|
||||
db.createUser({
|
||||
user: "${MONGO_USER}",
|
||||
pwd: "${MONGO_PASS}",
|
||||
roles: [
|
||||
{ role: "readWrite", db: "${MONGO_DB}" },
|
||||
{ role: "dbAdmin", db: "${MONGO_DB}" }
|
||||
]
|
||||
})
|
||||
MONGOEOF
|
||||
|
||||
# Bind to all interfaces
|
||||
sed -i 's/^ bindIp:.*/ bindIp: 0.0.0.0/' /etc/mongod.conf
|
||||
|
||||
# Enable authentication
|
||||
if ! grep -q "^security:" /etc/mongod.conf; then
|
||||
cat >> /etc/mongod.conf <<'EOF'
|
||||
|
||||
security:
|
||||
authorization: enabled
|
||||
EOF
|
||||
fi
|
||||
|
||||
systemctl restart mongod
|
||||
info "MongoDB auth enabled, bound to 0.0.0.0."
|
||||
else
|
||||
info "MongoDB auth already configured, skipping."
|
||||
# Try to read password from credentials file
|
||||
if [[ -f "$CREDENTIALS_FILE" ]]; then
|
||||
MONGO_PASS=$(grep "^MONGO_PASS=" "$CREDENTIALS_FILE" | cut -d= -f2-)
|
||||
fi
|
||||
fi
|
||||
|
||||
# ─── 5. Redis ───
|
||||
if ! command -v redis-server &>/dev/null; then
|
||||
info "Installing Redis..."
|
||||
apt-get install -y -qq redis-server > /dev/null
|
||||
@@ -59,18 +105,12 @@ systemctl enable redis-server
|
||||
systemctl start redis-server
|
||||
info "Redis running."
|
||||
|
||||
# ─── 5. PM2 ───
|
||||
# ─── 6. 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
|
||||
@@ -81,7 +121,7 @@ if [[ ! -f "$ENV_FILE" ]]; then
|
||||
|
||||
cat > "$ENV_FILE" <<EOF
|
||||
# Database
|
||||
DATABASE_URL=mongodb://localhost:27017/llm_proxy
|
||||
DATABASE_URL=mongodb://${MONGO_USER}:${MONGO_PASS}@localhost:27017/${MONGO_DB}?authSource=admin
|
||||
|
||||
# Redis
|
||||
REDIS_URL=redis://localhost:6379
|
||||
@@ -101,28 +141,38 @@ 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."
|
||||
warn "Edit .env to set your domain in NEXTAUTH_URL and NEXT_PUBLIC_APP_URL."
|
||||
else
|
||||
info ".env already exists, skipping."
|
||||
# Update DATABASE_URL with auth credentials if it doesn't have them
|
||||
if grep -q "mongodb://localhost:27017" "$ENV_FILE" && ! grep -q "@localhost" "$ENV_FILE"; then
|
||||
info "Updating .env with MongoDB credentials..."
|
||||
sed -i "s|^DATABASE_URL=.*|DATABASE_URL=mongodb://${MONGO_USER}:${MONGO_PASS}@localhost:27017/${MONGO_DB}?authSource=admin|" "$ENV_FILE"
|
||||
fi
|
||||
info ".env already exists, updated if needed."
|
||||
fi
|
||||
|
||||
# ─── 8. Install dependencies & build ───
|
||||
# ─── 8. Save credentials ───
|
||||
cat > "$CREDENTIALS_FILE" <<EOF
|
||||
# Auto-generated credentials — keep this safe!
|
||||
MONGO_USER=${MONGO_USER}
|
||||
MONGO_PASS=${MONGO_PASS}
|
||||
MONGO_DB=${MONGO_DB}
|
||||
MONGO_URI=mongodb://${MONGO_USER}:${MONGO_PASS}@<YOUR_LXC_IP>:27017/${MONGO_DB}?authSource=admin
|
||||
EOF
|
||||
chmod 600 "$CREDENTIALS_FILE"
|
||||
|
||||
# ─── 9. 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 ───
|
||||
# ─── 10. 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
|
||||
@@ -142,17 +192,18 @@ module.exports = {
|
||||
}],
|
||||
};
|
||||
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"
|
||||
pm2 delete llm-proxy-api 2>/dev/null || true
|
||||
pm2 start ecosystem.config.cjs
|
||||
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
|
||||
pm2 startup systemd 2>/dev/null || true
|
||||
pm2 save
|
||||
|
||||
# ─── 14. Create 'update' alias ───
|
||||
# ─── 14. Create 'update' command alias ───
|
||||
UPDATE_SCRIPT="$APP_DIR/update.sh"
|
||||
cat > "$UPDATE_SCRIPT" <<'SCRIPT'
|
||||
#!/usr/bin/env bash
|
||||
@@ -161,47 +212,53 @@ set -euo pipefail
|
||||
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
cd "$APP_DIR"
|
||||
|
||||
echo "⏳ Pulling latest changes..."
|
||||
echo "Pulling latest changes..."
|
||||
git pull --ff-only
|
||||
|
||||
echo "⏳ Installing dependencies..."
|
||||
echo "Installing dependencies..."
|
||||
npm install
|
||||
|
||||
echo "⏳ Building..."
|
||||
echo "Building..."
|
||||
npm run build
|
||||
|
||||
echo "⏳ Restarting..."
|
||||
echo "Restarting..."
|
||||
pm2 restart llm-proxy-api
|
||||
|
||||
echo "✅ Update complete."
|
||||
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"
|
||||
# Also add to root bashrc
|
||||
grep -q "alias update=" /root/.bashrc 2>/dev/null || \
|
||||
echo "alias update='${UPDATE_SCRIPT}'" >> /root/.bashrc
|
||||
|
||||
# ─── Done ───
|
||||
LXC_IP=$(hostname -I | awk '{print $1}')
|
||||
|
||||
echo ""
|
||||
info "============================================"
|
||||
info " LLM Proxy API installed successfully!"
|
||||
info "============================================"
|
||||
echo ""
|
||||
info "App running at: http://localhost:${APP_PORT}"
|
||||
info "App running at: http://${LXC_IP}:${APP_PORT}"
|
||||
info "Default admin: [email protected] / admin123"
|
||||
warn "⚠ Change the admin password immediately!"
|
||||
warn "Change the admin password immediately!"
|
||||
echo ""
|
||||
info "MongoDB remote connection:"
|
||||
info " mongodb://${MONGO_USER}:${MONGO_PASS}@${LXC_IP}:27017/${MONGO_DB}?authSource=admin"
|
||||
echo ""
|
||||
info "Credentials saved to: ${CREDENTIALS_FILE}"
|
||||
echo ""
|
||||
info "Commands:"
|
||||
info " update — Pull, build & restart"
|
||||
info " pm2 logs — View logs"
|
||||
info " pm2 logs — View server 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."
|
||||
warn "Update NEXTAUTH_URL and NEXT_PUBLIC_APP_URL with your domain."
|
||||
Reference in new issue
Block a user