From 537b6d3bca3885db8648ee73c68f146cae93134d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 12 Jul 2026 06:35:19 -0400 Subject: [PATCH] x --- .../pkh-nginx/admin.pkh.kitizone.com.conf | 31 ++++ .../pkh-nginx/api.pkh.kitizone.com.conf | 31 ++++ serverinfo/pkh-nginx/pkh.kitizone.com.conf | 40 +++++ .../pkh-nginx/spa.pkh.kitizone.com.conf | 32 ++++ serverinfo/pkh-post-receive | 153 ++++++++++++++++++ serverinfo/text.txt | 6 - 6 files changed, 287 insertions(+), 6 deletions(-) create mode 100644 serverinfo/pkh-nginx/admin.pkh.kitizone.com.conf create mode 100644 serverinfo/pkh-nginx/api.pkh.kitizone.com.conf create mode 100644 serverinfo/pkh-nginx/pkh.kitizone.com.conf create mode 100644 serverinfo/pkh-nginx/spa.pkh.kitizone.com.conf create mode 100644 serverinfo/pkh-post-receive diff --git a/serverinfo/pkh-nginx/admin.pkh.kitizone.com.conf b/serverinfo/pkh-nginx/admin.pkh.kitizone.com.conf new file mode 100644 index 0000000..7390f84 --- /dev/null +++ b/serverinfo/pkh-nginx/admin.pkh.kitizone.com.conf @@ -0,0 +1,31 @@ +server { + listen 80; + server_name admin.pkh.kitizone.com; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl; + server_name admin.pkh.kitizone.com; + + ssl_certificate /etc/letsencrypt/live/admin.pkh.kitizone.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/admin.pkh.kitizone.com/privkey.pem; + + location / { + root /usr/share/nginx/html/pkh/admin; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + # Caching for static assets + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires max; + log_not_found off; + } +} diff --git a/serverinfo/pkh-nginx/api.pkh.kitizone.com.conf b/serverinfo/pkh-nginx/api.pkh.kitizone.com.conf new file mode 100644 index 0000000..53da0b5 --- /dev/null +++ b/serverinfo/pkh-nginx/api.pkh.kitizone.com.conf @@ -0,0 +1,31 @@ +server { + listen 80; + server_name api.pkh.kitizone.com; + + location ~ /.well-known/acme-challenge { + allow all; + root /var/www/certbot; + } + location / { return 301 https://$host$request_uri; } +} + +server { + listen 443 ssl; + server_name api.pkh.kitizone.com; + + client_max_body_size 20m; + + ssl_certificate /etc/letsencrypt/live/api.pkh.kitizone.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/api.pkh.kitizone.com/privkey.pem; + + location / { + proxy_pass http://192.168.1.167:3052; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +} diff --git a/serverinfo/pkh-nginx/pkh.kitizone.com.conf b/serverinfo/pkh-nginx/pkh.kitizone.com.conf new file mode 100644 index 0000000..0aa30e1 --- /dev/null +++ b/serverinfo/pkh-nginx/pkh.kitizone.com.conf @@ -0,0 +1,40 @@ +server { + listen 80; + server_name pkh.kitizone.com; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl; + server_name pkh.kitizone.com; + + ssl_certificate /etc/letsencrypt/live/pkh.kitizone.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/pkh.kitizone.com/privkey.pem; + + # Next.js SSR + location / { + proxy_pass http://192.168.1.167:3003; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_cache_bypass $http_upgrade; + } + + # Next.js static assets caching + location /_next/static/ { + proxy_pass http://192.168.1.167:3003; + proxy_http_version 1.1; + proxy_set_header Host $host; + add_header Cache-Control "public, max-age=31536000, immutable"; + } +} diff --git a/serverinfo/pkh-nginx/spa.pkh.kitizone.com.conf b/serverinfo/pkh-nginx/spa.pkh.kitizone.com.conf new file mode 100644 index 0000000..21c8c32 --- /dev/null +++ b/serverinfo/pkh-nginx/spa.pkh.kitizone.com.conf @@ -0,0 +1,32 @@ +server { + listen 80; + server_name spa.pkh.kitizone.com; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + location / { + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl; + server_name spa.pkh.kitizone.com; + + ssl_certificate /etc/letsencrypt/live/spa.pkh.kitizone.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/spa.pkh.kitizone.com/privkey.pem; + + root /usr/share/nginx/html/pkh/spa; + index index.html index.htm; + + location / { + try_files $uri $uri/ /index.html; + } + + # Caching for static assets + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires max; + log_not_found off; + } +} diff --git a/serverinfo/pkh-post-receive b/serverinfo/pkh-post-receive new file mode 100644 index 0000000..47971c7 --- /dev/null +++ b/serverinfo/pkh-post-receive @@ -0,0 +1,153 @@ +#!/bin/bash + +# ============================================================================== +# PKH Project - Integrated Deployment Script (post-receive hook) +# ============================================================================== + +# 1. TARGET PATHS (Nginx & PM2) +TARGET_SPA="/home/ti/projects/MyNginxMain/nginx/html/pkh/spa" +TARGET_ADMIN="/home/ti/projects/MyNginxMain/nginx/html/pkh/admin" +TARGET_BACKEND="/home/ti/projects/pkh-api" +TARGET_WEBSITE="/home/ti/projects/pkh-website" # <-- Next.js SSR App + +# 2. REPOSITORY CONFIG +REPO="/home/ti/git/pkh.git" +BRANCH="master" +TEMP_DIR="/tmp/pkh_build_$(date +%s)" + +# 3. BINARY PATHS & PM2 +BUN="/home/ti/.bun/bin/bun" +PM2="/home/ti/.nvm/versions/node/v22.17.1/bin/pm2" +PM2_NAME_BACKEND="pkh-backend" +PM2_NAME_WEBSITE="pkh-website" # <-- PM2 process name สำหรับ SSR +export PATH="$PATH:/home/ti/.bun/bin:/home/ti/.nvm/versions/node/v22.17.1/bin" + +# 4. BUILD ENVIRONMENT +export VITE_API_URL="https://api.pkh.kitizone.com/api" +export NEXT_PUBLIC_API_URL="https://api.pkh.kitizone.com/api" # <-- สำหรับ SSR (Client-side fetch) + +# Port ของ services (Nginx จะ proxy_pass มาที่นี่) +BACKEND_PORT=3052 +SSR_PORT=3003 + +# ------------------------------------------------------------------------------ +# START DEPLOYMENT +# ------------------------------------------------------------------------------ +set -e +echo "------- 🚀 Starting Full Deployment: PKH Platform -------" + +# A. Prepare Temp Source from Checkout +echo "Cloning branch $BRANCH to temporary directory: $TEMP_DIR" +mkdir -p $TEMP_DIR +git --work-tree=$TEMP_DIR --git-dir=$REPO checkout -f $BRANCH + +cd $TEMP_DIR +echo "Installing monorepo dependencies..." +$BUN install + +# B. Build & Deploy SPA (Frontend) +echo ">>> Building SPA..." +cd apps/spa +$BUN run build +echo "Deploying SPA to $TARGET_SPA..." +mkdir -p $TARGET_SPA +rm -rf $TARGET_SPA/* +cp -rv dist/* $TARGET_SPA/ +cd ../.. + +# C. Build & Deploy ADMIN PANEL +echo ">>> Building Admin Panel..." +cd apps/admin +$BUN run build +echo "Deploying Admin Panel to $TARGET_ADMIN..." +mkdir -p $TARGET_ADMIN +rm -rf $TARGET_ADMIN/* +cp -rv dist/* $TARGET_ADMIN/ +cd ../.. + +# D. Deploy & Manage BACKEND (API) +echo ">>> Updating Backend at $TARGET_BACKEND..." +mkdir -p $TARGET_BACKEND +mkdir -p $TARGET_BACKEND/apps +mkdir -p $TARGET_BACKEND/packages + +# Sync Backend Source inside a monorepo-like layout so workspace deps resolve +rsync -av --delete \ + --exclude='.env' \ + --exclude='public/' \ + --exclude='node_modules/' \ + apps/backend/ $TARGET_BACKEND/apps/backend/ + +# Sync Shared Packages +rsync -av packages/ $TARGET_BACKEND/packages/ + +cp package.json bun.lock ecosystem.config.js $TARGET_BACKEND/ + +cd $TARGET_BACKEND +echo "Installing backend dependencies..." +$BUN install + +# สร้าง public/ folder สำหรับ static files (upload images) +mkdir -p $TARGET_BACKEND/apps/backend/public + +cd $TARGET_BACKEND/apps/backend + +# Generate Prisma Client (สำคัญมาก! ต้องรันทุกครั้งหลัง install) +echo ">>> Generating Prisma Client..." +$BUN x prisma generate + +# E. Database Migration (Safe Method) +if [ ! -f "$TARGET_BACKEND/.env" ]; then + echo "❌ Error: .env file is missing at $TARGET_BACKEND/.env. Deployment aborted." + exit 1 +fi + +# Copy .env to apps/backend so Prisma and the app can find it +cp "$TARGET_BACKEND/.env" "$TARGET_BACKEND/apps/backend/.env" +echo ">>> Running Prisma Migrate Deploy..." +$BUN x prisma migrate deploy + +# F. PM2 Management - BACKEND +echo ">>> Managing PM2 Process: $PM2_NAME_BACKEND..." +$PM2 startOrReload $TARGET_BACKEND/ecosystem.config.js --only $PM2_NAME_BACKEND --env production --update-env +echo "✅ PM2 process '$PM2_NAME_BACKEND' is running." + +# G. Build & Deploy WEBSITE (Next.js SSR) +echo ">>> Building Next.js SSR App inside monorepo temp dir..." +cd $TEMP_DIR/apps/website +$BUN run build # build จาก monorepo (workspace:* resolve ได้) + +# H. Prepare Next.js SSR +echo ">>> Preparing Next.js SSR App..." +rsync -av --delete \ + $TEMP_DIR/apps/website/.next/standalone/ $TARGET_WEBSITE/ +rsync -av \ + $TEMP_DIR/apps/website/public/ $TARGET_WEBSITE/apps/website/public/ +rsync -av \ + $TEMP_DIR/apps/website/.next/static/ $TARGET_WEBSITE/apps/website/.next/static/ + +cp $TEMP_DIR/ecosystem.config.js $TARGET_WEBSITE/ + +# I. Fix Permissions +echo ">>> Fixing Permissions..." +chown -R ti:ti $TARGET_BACKEND $TARGET_WEBSITE + +# J. PM2 Management - SSR Next.js +cd $TARGET_WEBSITE +echo ">>> Creating symlink for .next..." +ln -sf apps/website/.next .next +echo ">>> Managing PM2 Process: $PM2_NAME_WEBSITE..." +$PM2 startOrReload $TARGET_WEBSITE/ecosystem.config.js --only $PM2_NAME_WEBSITE --env production --update-env +echo "✅ PM2 process '$PM2_NAME_WEBSITE' is running." + +# K. Cleanup +echo "Cleaning up build artifacts..." +rm -rf $TEMP_DIR + +echo "------- ✅ ALL DEPLOYMENTS COMPLETED SUCCESSFULLY -------" +echo "" +echo "📋 Service Summary:" +echo " 🌐 Website (SSR) → https://pkh.kitizone.com (PM2: $PM2_NAME_WEBSITE, Port: $SSR_PORT)" +echo " 📱 SPA → https://spa.pkh.kitizone.com" +echo " 🔧 Admin Panel → https://admin.pkh.kitizone.com" +echo " 🔌 API Backend → https://api.pkh.kitizone.com (PM2: $PM2_NAME_BACKEND, Port: $BACKEND_PORT)" diff --git a/serverinfo/text.txt b/serverinfo/text.txt index 3454872..e69de29 100644 --- a/serverinfo/text.txt +++ b/serverinfo/text.txt @@ -1,6 +0,0 @@ -ti@pc-server:~/projects/MyNginxMain/nginx/html/irelaxaroma.shop$ ls -la -total 16 -drwxrwxr-x 4 ti ti 4096 Apr 5 04:30 . -drwxrwxr-x 24 ti ti 4096 Apr 5 04:29 .. -drwxrwxr-x 2 ti ti 4096 Apr 5 04:30 admin -drwxrwxr-x 2 ti ti 4096 Apr 5 04:29 website