feat(deploy): add deployment configs, post-receive hook, and nginx reverse proxy for foxxio
This commit is contained in:
1 parent
0b6e5310c0
commit
dd79519bf7
7 files changed
+471
No files matched your search
@@ -0,0 +1,151 @@
|
||||
# Foxxio Platform Deployment Guide & System Architecture
|
||||
### Target Domain: `foxxio.ddcsprocket.com`
|
||||
### Target Server: `sql@192.168.1.26`
|
||||
### Gateway Server: `adminweb@192.168.1.24`
|
||||
|
||||
---
|
||||
|
||||
## 1. บทบาทของเซิร์ฟเวอร์ (System Architecture & Role)
|
||||
|
||||
* **Application Server:** `192.168.1.26` (Hostname: `sql`, User: `sql`)
|
||||
* **บทบาท:** Application Runner (PM2 / Bun / Node) + Static Nginx Host สำหรับ Admin Panel
|
||||
* **ขอบเขต:**
|
||||
* รัน API Service (Elysia / Bun) บน Port **`3006`**
|
||||
* รัน Website SSR (Next.js / Node.js) บน Port **`3003`**
|
||||
* รัน Admin Panel (React Vite SPA) ผ่าน Nginx Port **`80`** ชี้ไปยัง `/var/www/html/foxxio_admin`
|
||||
* **Gateway / Reverse Proxy:** `192.168.1.24` (Docker Nginx)
|
||||
* จัดการ SSL Termination (Let's Encrypt Certbot)
|
||||
* Proxy Traffic ตาม Domain เข้ามายัง `192.168.1.26`
|
||||
|
||||
```
|
||||
[ User Request ]
|
||||
│
|
||||
▼
|
||||
[ Gateway: 192.168.1.24 (Docker Nginx + SSL) ]
|
||||
│
|
||||
├── https://foxxio.ddcsprocket.com ──────► http://192.168.1.26:3003 (Next.js SSR)
|
||||
├── https://foxxio.ddcsprocket.com/api/ ──► http://192.168.1.26:3006 (Elysia API)
|
||||
├── https://api.foxxio.ddcsprocket.com ───► http://192.168.1.26:3006 (Elysia API)
|
||||
└── https://admin.foxxio.ddcsprocket.com ─► http://192.168.1.26:80 (Admin Static SPA)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. โครงสร้าง Git Repository & การเชื่อมต่อ (Git Info)
|
||||
|
||||
### ฝั่ง Server (`192.168.1.26`)
|
||||
| รายการ | ตำแหน่ง Path บน Server | รายละเอียด |
|
||||
| :--- | :--- | :--- |
|
||||
| **Bare Repository** | `/home/sql/git/foxxio.git` | จุดรับ Git Push จากเครื่อง Local |
|
||||
| **Symlink Alias** | `/home/sql/git/foxxio.ddcsprocket.com.git` | ชี้ไปยัง `foxxio.git` |
|
||||
| **Working Directory** | `/home/sql/code/foxxio` | โฟลเดอร์แตกไฟล์ Source Code และรันโปรเจกต์ |
|
||||
| **Hook Script** | `/home/sql/git/foxxio.git/hooks/post-receive` | สคริปต์สั่ง Build และ Restart PM2 อัตโนมัติ (`chmod +x`) |
|
||||
|
||||
### ฝั่ง Local (`d:\cms`)
|
||||
* **Git Remote Name:** `foxxio`
|
||||
* **Remote URL:** `sql@192.168.1.26:/home/sql/git/foxxio.git`
|
||||
* **คำสั่ง Deploy:**
|
||||
```powershell
|
||||
git remote add foxxio sql@192.168.1.26:/home/sql/git/foxxio.git
|
||||
git push foxxio master
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. แผนผังพอร์ต & การจัดการ PM2 (Process Management)
|
||||
|
||||
### พอร์ตและ Process ของ Foxxio
|
||||
| Service | PM2 Process Name | Port | Runtime / Command | CWD |
|
||||
| :--- | :--- | :---: | :--- | :--- |
|
||||
| **Backend API** | `foxxio-backend` | **`3006`** | `bun run src/index.ts` | `/home/sql/code/foxxio/apps/backend` |
|
||||
| **Website (SSR)** | `foxxio-website` | **`3003`** | `node server.js` หรือ `bun run start` | `/home/sql/code/foxxio/apps/website` |
|
||||
| **Admin Panel** | *N/A (Nginx Static)* | **`80`** | Nginx Static Build | `/var/www/html/foxxio_admin` |
|
||||
|
||||
### ตารางพอร์ตบน Server 192.168.1.26
|
||||
| Port | Service ที่ใช้งาน | สถานะ |
|
||||
| :---: | :--- | :---: |
|
||||
| **3001** | `shop-ddcth` (Next.js) | ใช้งานอยู่ |
|
||||
| **3002** | `bigbike-website` (Next.js SSR) | ใช้งานอยู่ |
|
||||
| **3003** | `foxxio-website` (Next.js SSR) | **Foxxio** |
|
||||
| **3005** | `bigbike-backend` (Elysia API) | ใช้งานอยู่ |
|
||||
| **3006** | `foxxio-backend` (Elysia API) | **Foxxio** |
|
||||
| **4000** | `aroka-backend` | ใช้งานอยู่ |
|
||||
| **5000** | `ddc hr backend` | ใช้งานอยู่ |
|
||||
|
||||
---
|
||||
|
||||
## 4. การตั้งค่าฐานข้อมูล & ไฟล์ .env
|
||||
|
||||
### 1) ฐานข้อมูล PostgreSQL บน `192.168.1.26`
|
||||
* **Database Name:** `foxxio_db`
|
||||
* **Connection String:**
|
||||
```text
|
||||
DATABASE_URL="postgresql://postgres:password@localhost:5432/foxxio_db?schema=public"
|
||||
```
|
||||
|
||||
### 2) Environment Variables
|
||||
* **Backend (`/home/sql/code/foxxio/apps/backend/.env`):**
|
||||
```env
|
||||
PORT=3006
|
||||
DATABASE_URL="postgresql://postgres:password@localhost:5432/foxxio_db?schema=public"
|
||||
NODE_ENV=production
|
||||
APP_URL="https://api.foxxio.ddcsprocket.com"
|
||||
BACKEND_PUBLIC_URL="https://api.foxxio.ddcsprocket.com"
|
||||
CORS_ORIGIN="https://admin.foxxio.ddcsprocket.com,https://spa.foxxio.ddcsprocket.com,https://foxxio.ddcsprocket.com,http://localhost:3000,http://localhost:3003"
|
||||
JWT_SECRET="foxxio-secret-key-prod-2026-ddc"
|
||||
OMISE_SECRET_KEY=""
|
||||
```
|
||||
|
||||
* **Website SSR (`/home/sql/code/foxxio/apps/website/.env`):**
|
||||
```env
|
||||
PORT=3003
|
||||
NODE_ENV=production
|
||||
NEXT_PUBLIC_API_URL="https://api.foxxio.ddcsprocket.com/api"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. คำสั่ง Sudo One-Liner สำหรับ Server 192.168.1.26
|
||||
|
||||
รันคำสั่งด้านล่างนี้บน `sql@192.168.1.26` เพียงครั้งเดียว:
|
||||
|
||||
```bash
|
||||
# 1. ปลดบล็อก UFW พอร์ต 3003 และ 3006
|
||||
sudo ufw allow 3003/tcp comment "Foxxio Website SSR"
|
||||
sudo ufw allow 3006/tcp comment "Foxxio Backend API"
|
||||
sudo ufw reload
|
||||
|
||||
# 2. สร้างโฟลเดอร์ Web Root สำหรับ Admin Panel
|
||||
sudo mkdir -p /var/www/html/foxxio_admin
|
||||
sudo chown -R sql:sql /var/www/html/foxxio_admin
|
||||
|
||||
# 3. ติดตั้ง Nginx Virtual Host สำหรับ Admin Panel
|
||||
sudo tee /etc/nginx/sites-available/admin.foxxio.ddcsprocket.com.conf > /dev/null << 'EOF'
|
||||
server {
|
||||
listen 80;
|
||||
server_name admin.foxxio.ddcsprocket.com;
|
||||
root /var/www/html/foxxio_admin;
|
||||
index index.html;
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
||||
expires 30d;
|
||||
add_header Cache-Control "public, no-transform";
|
||||
}
|
||||
error_log /var/log/nginx/foxxio_admin_error.log;
|
||||
access_log /var/log/nginx/foxxio_admin_access.log;
|
||||
}
|
||||
EOF
|
||||
|
||||
sudo ln -sf /etc/nginx/sites-available/admin.foxxio.ddcsprocket.com.conf /etc/nginx/sites-enabled/
|
||||
sudo nginx -t && sudo systemctl reload nginx
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. การตั้งค่าบน Gateway 192.168.1.24 (`adminweb@192.168.1.24`)
|
||||
|
||||
1. **`foxxio.ddcsprocket.com.conf`**: Proxy `/` ไปยัง `192.168.1.26:3003` และ `/api/` ไปยัง `192.168.1.26:3006`
|
||||
2. **`admin.foxxio.ddcsprocket.com.conf`**: Proxy ไปยัง `192.168.1.26:80` (พร้อม SSL เดิม)
|
||||
3. **`api.foxxio.ddcsprocket.com.conf`**: Proxy ไปยัง `192.168.1.26:3006`
|
||||
@@ -0,0 +1,21 @@
|
||||
# Foxxio Git Repository & Deployment Info
|
||||
|
||||
### Server: `sql@192.168.1.26`
|
||||
|
||||
| Parameter | Value |
|
||||
| :--- | :--- |
|
||||
| **Bare Repository** | `/home/sql/git/foxxio.git` |
|
||||
| **Symlink Alias** | `/home/sql/git/foxxio.ddcsprocket.com.git` |
|
||||
| **Working Directory** | `/home/sql/code/foxxio` |
|
||||
| **Hook Script** | `/home/sql/git/foxxio.git/hooks/post-receive` |
|
||||
| **Branch** | `master` |
|
||||
|
||||
### Local Remote Configuration (`d:\cms`)
|
||||
|
||||
```powershell
|
||||
# Add remote
|
||||
git remote add foxxio sql@192.168.1.26:/home/sql/git/foxxio.git
|
||||
|
||||
# Deploy
|
||||
git push foxxio master
|
||||
```
|
||||
@@ -0,0 +1,128 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ==============================================================================
|
||||
# Foxxio Project (foxxio.ddcsprocket.com)
|
||||
# Deployment Script (post-receive hook) on sql@192.168.1.26
|
||||
# Role: Application Runner (PM2 / Bun / Node) - No local Nginx
|
||||
# ==============================================================================
|
||||
|
||||
set -e
|
||||
export PATH="/home/sql/.bun/bin:/usr/local/bin:/usr/bin:/bin:$PATH"
|
||||
|
||||
# 1. PATH CONFIG
|
||||
GIT_DIR="/home/sql/git/foxxio.git"
|
||||
WORK_DIR="/home/sql/code/foxxio"
|
||||
TARGET_ADMIN="/var/www/html/foxxio_admin"
|
||||
BRANCH="master"
|
||||
TEMP_DIR="/tmp/foxxio_build_$(date +%s)"
|
||||
|
||||
# 2. RUNTIMES & EXECUTABLES
|
||||
BUN="/home/sql/.bun/bin/bun"
|
||||
NODE="/usr/bin/node"
|
||||
PM2="/usr/bin/pm2"
|
||||
|
||||
# 3. PM2 PROCESS NAMES
|
||||
PM2_NAME_BACKEND="foxxio-backend"
|
||||
PM2_NAME_WEBSITE="foxxio-website"
|
||||
|
||||
# 4. ASSIGNED PORTS
|
||||
BACKEND_PORT=3006
|
||||
SSR_PORT=3003
|
||||
|
||||
# 5. ENVIRONMENT VARIABLES
|
||||
export BACKEND_PORT="$BACKEND_PORT"
|
||||
export SSR_PORT="$SSR_PORT"
|
||||
export VITE_API_URL="https://api.foxxio.ddcsprocket.com/api"
|
||||
export NEXT_PUBLIC_API_URL="https://api.foxxio.ddcsprocket.com/api"
|
||||
|
||||
echo "========================================================"
|
||||
echo "🚀 [Foxxio] Starting Deployment on 192.168.1.26..."
|
||||
echo "========================================================"
|
||||
|
||||
# A. Checkout code to Work Directory
|
||||
mkdir -p "$WORK_DIR"
|
||||
echo ">>> Checking out branch $BRANCH to $WORK_DIR..."
|
||||
git --work-tree="$WORK_DIR" --git-dir="$GIT_DIR" checkout -f "$BRANCH"
|
||||
|
||||
cd "$WORK_DIR"
|
||||
|
||||
# B. Install dependencies
|
||||
if [ -f "package.json" ]; then
|
||||
echo ">>> Installing dependencies with Bun..."
|
||||
$BUN install
|
||||
fi
|
||||
|
||||
# C. Setup Backend (if apps/backend exists)
|
||||
if [ -d "apps/backend" ]; then
|
||||
echo ">>> Setting up Backend..."
|
||||
cd "$WORK_DIR/apps/backend"
|
||||
|
||||
if [ -f "prisma/schema.prisma" ]; then
|
||||
echo ">>> Generating Prisma Client..."
|
||||
$BUN x prisma generate
|
||||
|
||||
echo ">>> Auto-updating Database Schema..."
|
||||
if [ -d "prisma/migrations" ]; then
|
||||
echo ">>> Running Prisma migrate deploy..."
|
||||
$BUN x prisma migrate deploy || echo "⚠️ migrate deploy had notices, falling back to db push..."
|
||||
fi
|
||||
echo ">>> Syncing schema with db push..."
|
||||
$BUN x prisma db push --accept-data-loss
|
||||
fi
|
||||
|
||||
echo ">>> Restarting PM2 process: $PM2_NAME_BACKEND..."
|
||||
if $PM2 describe "$PM2_NAME_BACKEND" > /dev/null 2>&1; then
|
||||
PORT=$BACKEND_PORT $PM2 restart "$PM2_NAME_BACKEND" --update-env
|
||||
else
|
||||
echo ">>> Starting $PM2_NAME_BACKEND for the first time on port $BACKEND_PORT..."
|
||||
PORT=$BACKEND_PORT $PM2 start "$BUN" --name "$PM2_NAME_BACKEND" --cwd "$WORK_DIR/apps/backend" -- run src/index.ts
|
||||
fi
|
||||
cd "$WORK_DIR"
|
||||
fi
|
||||
|
||||
# D. Build & Deploy Admin Panel (React Vite SPA)
|
||||
if [ -d "apps/admin" ]; then
|
||||
echo ">>> Building Admin Panel (React Vite SPA)..."
|
||||
cd "$WORK_DIR/apps/admin"
|
||||
export VITE_API_URL="https://api.foxxio.ddcsprocket.com/api"
|
||||
export VITE_FRONTEND_URL="https://foxxio.ddcsprocket.com"
|
||||
$BUN run build
|
||||
|
||||
if [ -d "$TARGET_ADMIN" ]; then
|
||||
echo "Deploying Admin Panel to $TARGET_ADMIN..."
|
||||
rsync -av --delete dist/ "$TARGET_ADMIN"/
|
||||
else
|
||||
echo "⚠️ Target directory $TARGET_ADMIN not found. Please create it once with:"
|
||||
echo " sudo mkdir -p $TARGET_ADMIN && sudo chown -R sql:sql $TARGET_ADMIN"
|
||||
fi
|
||||
cd "$WORK_DIR"
|
||||
fi
|
||||
|
||||
# E. Setup Website SSR (if apps/website exists)
|
||||
if [ -d "apps/website" ]; then
|
||||
echo ">>> Building Next.js Website..."
|
||||
cd "$WORK_DIR/apps/website"
|
||||
NODE_ENV=production $BUN run build
|
||||
|
||||
echo ">>> Restarting PM2 process: $PM2_NAME_WEBSITE..."
|
||||
if $PM2 describe "$PM2_NAME_WEBSITE" > /dev/null 2>&1; then
|
||||
PORT=$SSR_PORT $PM2 restart "$PM2_NAME_WEBSITE" --update-env
|
||||
else
|
||||
echo ">>> Starting $PM2_NAME_WEBSITE for the first time on port $SSR_PORT..."
|
||||
# Check standalone server or standard next start
|
||||
if [ -f ".next/standalone/server.js" ]; then
|
||||
PORT=$SSR_PORT $PM2 start "$NODE" --name "$PM2_NAME_WEBSITE" --cwd "$WORK_DIR/apps/website/.next/standalone" -- server.js
|
||||
else
|
||||
PORT=$SSR_PORT $PM2 start "$BUN" --name "$PM2_NAME_WEBSITE" --cwd "$WORK_DIR/apps/website" -- run start
|
||||
fi
|
||||
fi
|
||||
cd "$WORK_DIR"
|
||||
fi
|
||||
|
||||
# F. Save PM2 state
|
||||
$PM2 save
|
||||
|
||||
echo "========================================================"
|
||||
echo "✅ [Foxxio] Deployment Finished Successfully!"
|
||||
echo "========================================================"
|
||||
$PM2 status
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
server {
|
||||
listen 80;
|
||||
server_name admin.foxxio.ddcsprocket.com;
|
||||
|
||||
# ตำแหน่งโฟลเดอร์ Build ของ React Admin Panel
|
||||
root /var/www/html/foxxio_admin;
|
||||
index index.html;
|
||||
|
||||
# สำคัญมากสำหรับ React Router SPA:
|
||||
# ถ้าไม่พบไฟล์ ($uri) ให้ส่งกลับไปที่ index.html เสมอ
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
# ตั้งค่า Cache สำหรับ Static Assets เพื่อความเร็ว
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
||||
expires 30d;
|
||||
add_header Cache-Control "public, no-transform";
|
||||
}
|
||||
|
||||
# Logging
|
||||
error_log /var/log/nginx/foxxio_admin_error.log;
|
||||
access_log /var/log/nginx/foxxio_admin_access.log;
|
||||
}
|
||||
Reference in new issue
Block a user