This commit is contained in:
unknown committed 2026-10-17 13:34:07 +07:00
1 parent 2364aa32b0
commit 5ba4afb2de
7 files changed
+756

No files matched your search

@@ -0,0 +1,157 @@
# Bigbike Platform Deployment Guide & System Architecture
### Target Domain: `bigbike.ddcsprocket.com`
### Target Server: `sql@192.168.1.26`
---
## 1. บทบาทของเซิร์ฟเวอร์ (System Architecture & Role)
* **Server IP:** `192.168.1.26` (Hostname: `sql`, User: `sql`)
* **บทบาท (Role):** **Application Runner เท่านั้น (Node.js / Bun / PM2)**
* **ขอบเขตการทำงาน:**
* รัน API Service (Elysia / Bun) และ Website SSR (Next.js Standalone / Node.js) ภายใต้การควบคุมของ PM2
* **ไม่ต้องตั้งค่า Nginx บนเครื่องนี้** เนื่องจากระบบมี Gateway / Reverse Proxy ภายนอกทำการ Port-Forward หรือ Proxy ส่ง Traffic เข้ามายังพอร์ตของแต่ละ Service โดยตรง
```
[ User Request ]
│
▼
[ External Gateway / Firewall / Nginx Proxy ]
│
├── Forward Traffic (Domain: bigbike.ddcsprocket.com) ──► 192.168.1.26:3002 (Next.js SSR)
└── Forward Traffic (Domain: api.bigbike.ddcsprocket.com) ─► 192.168.1.26:3005 (Elysia API)
```
---
## 2. โครงสร้าง Git Repository & การเชื่อมต่อ (Git Info)
### ฝั่ง Server (`192.168.1.26`)
| รายการ | ตำแหน่ง Path บน Server | รายละเอียด |
| :--- | :--- | :--- |
| **Bare Repository** | `/home/sql/git/bigbike.git` | จุดรับ Git Push จากเครื่อง Local |
| **Symlink Alias** | `/home/sql/git/bigbike.ddcsprocket.com.git` | ชี้ไปยัง `bigbike.git` |
| **Working Directory** | `/home/sql/code/bigbike` | โฟลเดอร์แตกไฟล์ Source Code และรันโปรเจกต์ |
| **Hook Script** | `/home/sql/git/bigbike.git/hooks/post-receive` | สคริปต์สั่ง Build และ Restart PM2 อัตโนมัติ (`chmod +x`) |
### ฝั่ง Local (`d:\cms`)
* **Git Remote Name:** `bigbike`
* **Remote URL:** `sql@192.168.1.26:/home/sql/git/bigbike.git`
* **คำสั่ง Deploy:**
```powershell
git push bigbike master
```
---
## 3. Runtimes & สภาพแวดล้อมที่ใช้ในการประมวลผล
| เครื่องมือ | Version | Path บนเครื่อง 192.168.1.26 | หน้าที่ในไปป์ไลน์ |
| :--- | :--- | :--- | :--- |
| **Bun** | `1.3.8` | `/home/sql/.bun/bin/bun` | รัน `bun install`, `bun run build`, รัน Elysia Backend |
| **Node.js** | `v25.2.0` | `/usr/bin/node` | รัน Next.js Standalone SSR Server |
| **PM2** | `6.0.14` | `/usr/bin/pm2` | Process Manager ควบคุม Service ให้รันตลอดเวลา |
| **PostgreSQL** | `14.24` | `localhost:5432` | ฐานข้อมูล Local |
> ⚠️ **ข้อควรระวังเรื่อง PATH ใน Post-Receive Hook:**
> SSH non-interactive session จะไม่โหลด `~/.bashrc` อัตโนมัติ ในหัวสคริปต์ `post-receive` ต้อง export PATH เสมอ:
> ```bash
> export PATH="/home/sql/.bun/bin:/usr/local/bin:/usr/bin:/bin:$PATH"
> ```
---
## 4. แผนผังพอร์ต & การจัดการ PM2 (Process Management)
### พอร์ตและ Process ของ Bigbike (เปิดใหม่)
| Service | PM2 Process Name | Port | Runtime / Command | CWD |
| :--- | :--- | :---: | :--- | :--- |
| **Backend API** | `bigbike-backend` | **`3005`** | `bun run src/index.ts` | `/home/sql/code/bigbike/apps/backend` |
| **Website (SSR)** | `bigbike-website` | **`3002`** | `node server.js` (.next/standalone) | `/home/sql/code/bigbike/apps/website` |
### ตารางตรวจสอบพอร์ตเดิมบน Server (เพื่อป้องกันพอร์ตชน)
| Port | Service เดิมที่ใช้งานอยู่ | สถานะ |
| :---: | :--- | :---: |
| **3001** | `shop-ddcth` (Next.js) | ใช้งานอยู่ |
| **3002** | *ว่าง* ➔ **กำหนดให้ `bigbike-website`** | ✅ ใช้งานได้ |
| **3005** | *ว่าง* ➔ **กำหนดให้ `bigbike-backend`** | ✅ ใช้งานได้ |
| **3306** | MariaDB Server | ใช้งานอยู่ |
| **4000** | `aroka-backend` | ใช้งานอยู่ |
| **4545** | `rebrand-dev` (Vite) | ใช้งานอยู่ |
| **5000** | `ddc hr backend` (Bun) | ใช้งานอยู่ |
| **5432** | PostgreSQL 14 Local | ใช้งานอยู่ |
| **6333 / 6334** | Qdrant Vector DB | ใช้งานอยู่ |
| **8000** | `check-order-production` (Python FastAPI) | ใช้งานอยู่ |
---
## 5. การตั้งค่าฐานข้อมูล & ไฟล์ .env (Database & Environment Config)
### 1) สถานะฐานข้อมูล PostgreSQL
* **สถานะปัจจุบัน:** ✅ **สร้าง Database `bigbike_db` บน `192.168.1.26` เรียบร้อยแล้ว**
* **Host:** `localhost:5432`
* **User:** `postgres`
* **Password:** `password`
* **Connection String:**
```text
DATABASE_URL="postgresql://postgres:password@localhost:5432/bigbike_db?schema=public"
```
### 2) ตำแหน่งไฟล์ .env ที่ติดตั้งไว้บน Server (`192.168.1.26`)
* **Backend API:** `/home/sql/code/bigbike/apps/backend/.env`
```env
PORT=3005
DATABASE_URL="postgresql://postgres:password@localhost:5432/bigbike_db?schema=public"
NODE_ENV=production
APP_URL="https://api.bigbike.ddcsprocket.com"
BACKEND_PUBLIC_URL="https://api.bigbike.ddcsprocket.com"
CORS_ORIGIN="https://admin.bigbike.ddcsprocket.com,https://spa.bigbike.ddcsprocket.com,https://bigbike.ddcsprocket.com,http://localhost:3000,http://localhost:3002"
JWT_SECRET="bigbike-secret-key-prod-2026-ddc"
OMISE_SECRET_KEY=""
```
* **Website (Next.js SSR):** `/home/sql/code/bigbike/apps/website/.env`
```env
PORT=3002
NODE_ENV=production
NEXT_PUBLIC_API_URL="https://api.bigbike.ddcsprocket.com/api"
```
*(ไฟล์ template ถูกสำรองไว้ใน [backend.env](file:///d:/cms/serverinfo/192.168.1.26/bigbike.ddcsprocket.com/backend.env) และ [website.env](file:///d:/cms/serverinfo/192.168.1.26/bigbike.ddcsprocket.com/website.env))*
---
## 6. วงจรการทำงานของ Post-Receive Hook (Deployment Pipeline)
เมื่อมีการ `git push bigbike master` สคริปต์ `/home/sql/git/bigbike.git/hooks/post-receive` จะทำงานตามลำดับดังนี้:
1. **Checkout Source Code:** แตกไฟล์ล่าสุดจาก Git ลงที่ `/home/sql/code/bigbike`
2. **Install Dependencies:** สั่งรัน `$BUN install` ใน root monorepo
3. **Backend Service & Auto Database Migration:**
* รัน `$BUN x prisma generate` (อัปเดต Prisma Client Types)
* รัน `$BUN x prisma migrate deploy` (นำ migration files ใหม่ไป Apply ลง DB อัตโนมัติ)
* รัน `$BUN x prisma db push --accept-data-loss` (ซิงก์ schema เข้า DB ทันที ป้องกันกรณีแก้ schema.prisma โดยไม่ได้รัน migrate dev)
* สั่ง `pm2 restart bigbike-backend --update-env` (หากยังไม่เคยรัน จะสั่ง `pm2 start` บนพอร์ต `3005`)
4. **Website Service (Next.js SSR):**
* บิลด์ Next.js ด้วย `NODE_ENV=production $BUN run build`
* สั่ง `pm2 restart bigbike-website --update-env` (หากยังไม่เคยรัน จะสั่งรัน standalone `server.js` บนพอร์ต `3002`)
5. **Persist State:** สั่ง `pm2 save` เพื่อบันทึกรายชื่อโปรเซส ให้ฟื้นตัวอัตโนมัติเมื่อเซิร์ฟเวอร์รีบูต
---
## 7. คำสั่งที่ใช้ในการตรวจสอบและแก้ไขปัญหา (Runbook & Troubleshooting)
### ตรวจสอบสถานะและ Logs บน Server:
```bash
# ดูสถานะ PM2 ทั้งหมด
ssh sql@192.168.1.26 "pm2 status"
# ดู Logs สดของ Backend
ssh sql@192.168.1.26 "pm2 logs bigbike-backend --lines 100"
# ดู Logs สดของ Website SSR
ssh sql@192.168.1.26 "pm2 logs bigbike-website --lines 100"
# ทดสอบ Curl พอร์ตภายในเครื่อง
ssh sql@192.168.1.26 "curl -I http://localhost:3005"
ssh sql@192.168.1.26 "curl -I http://localhost:3002"
```
@@ -0,0 +1,23 @@
# Git Configuration for bigbike.ddcsprocket.com
## Remote Configuration
* **Server:** `192.168.1.26`
* **SSH User:** `sql`
* **Bare Repo (Server):** `/home/sql/git/bigbike.git`
* **Symlink (Server):** `/home/sql/git/bigbike.ddcsprocket.com.git`
* **Working Directory (Server):** `/home/sql/code/bigbike`
* **Hook File (Server):** `/home/sql/git/bigbike.git/hooks/post-receive`
## Local Git Setup (Machine)
* **Remote Name:** `bigbike`
* **Remote Push URL:** `sql@192.168.1.26:/home/sql/git/bigbike.git`
* **Remote Fetch URL:** `sql@192.168.1.26:/home/sql/git/bigbike.git`
## Deploy Commands
```powershell
# Deploy branch master
git push bigbike master
# Dry run test
git push --dry-run bigbike master
```
@@ -0,0 +1,110 @@
#!/bin/bash
# ==============================================================================
# Bigbike Project (bigbike.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/bigbike.git"
WORK_DIR="/home/sql/code/bigbike"
BRANCH="master"
TEMP_DIR="/tmp/bigbike_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="bigbike-backend"
PM2_NAME_WEBSITE="bigbike-website"
# 4. ASSIGNED PORTS (Direct forwarded from outer proxy / gateway)
# (Port 3001 is shop-ddcth, 4000 is aroka, 5000 is ddc hr)
BACKEND_PORT=3005
SSR_PORT=3002
# 5. ENVIRONMENT VARIABLES
export PORT="$SSR_PORT"
export BACKEND_PORT="$BACKEND_PORT"
export VITE_API_URL="https://api.bigbike.ddcsprocket.com/api"
export NEXT_PUBLIC_API_URL="https://api.bigbike.ddcsprocket.com/api"
echo "========================================================"
echo "🚀 [Bigbike] 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
$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. 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
# E. Save PM2 state
$PM2 save
echo "========================================================"
echo "✅ [Bigbike] Deployment Finished Successfully!"
echo "========================================================"
$PM2 status