Deploy WordPress ด้วย Git บน Cloud VPS (Version Control)

การพัฒนา WordPress Theme และ Plugin อย่างมืออาชีพ ต้องอาศัยเครื่องมือที่ช่วยให้การจัดการโค้ด Control Changes และ Deploy ไปยัง Production Server ได้อย่างปลอดภัยและมีประสิทธิภาพ คำตอบคือการใช้ Git Version Control บทความนี้จะแนะนำวิธีการตั้งค่า Git-based Deployment สำหรับ WordPress บน Cloud VPS ของ de.co.th อย่างละเอียด

ความสำคัญของการใช้ Git Version Control สำหรับ WordPress Development

Git คือ Distributed Version Control System ที่ช่วยให้นักพัฒนาติดตามการเปลี่ยนแปลงทุกครั้งที่เกิดขึ้นในโค้ด โปรแกรมเมอร์ React, Laravel, Django และ WordPress ใช้ Git เพื่อควบคุมการพัฒนา ทีมงาน ลดปัญหาการขัดแย้ง และ Deploy อย่างปลอดภัย

  • การติดตามการเปลี่ยนแปลง (History Tracking) – Git เก็บประวัติการเปลี่ยนแปลงทั้งหมด ทำให้คุณสามารถย้อนกลับไปเฟื่อใดก็ได้ หรือดูว่าใครเปลี่ยนแปลงอะไร เมื่อไร
  • การทำงานเป็นทีม (Collaboration) – หลายคนสามารถทำงานบนไฟล์เดียวกันได้พร้อมกัน Git จะจัดการการผสานโค้ด (Merge) อย่างชาญฉลาด
  • การแยกเบิ่ง (Branching) – สามารถสร้างเบิ่งแยกสำหรับพัฒนาฟีเจอร์ใหม่ โดยไม่กระทบต่อเบิ่ง Main Production
  • ความปลอดภัย (Security) – ใช้ SSH Key แทนรหัสผ่าน ป้องกันการเข้าถึงที่ไม่ได้รับอนุญาต
  • Deploy อัตโนมัติ (Automated Deployment) – ตั้งค่า Git Hooks เพื่อ Deploy โค้ดอัตโนมัติเมื่อ Push ไปยัง Repository
  • การลดความเสี่ยง (Risk Reduction) – สามารถ Rollback กลับไปเวอร์ชันเก่าได้ทันที หากมีปัญหา

คำศัพท์พื้นฐาน Git ที่จำเป็นต้องรู้

ก่อนเริ่มต้น คุณต้องเข้าใจคำศัพท์พื้นฐานต่อไปนี้:

  • Repository (Repo) – โปรเจกต์ของคุณที่ Git ติดตาม เหมือนโฟลเดอร์ที่มีประวัติการเปลี่ยนแปลง
  • Commit – การบันทึกการเปลี่ยนแปลงที่มี Message อธิบายว่าเปลี่ยนแปลงอะไร
  • Branch – เบิ่งแยกของ Repository สำหรับทำงานแยกต่างหาก เช่น develop, feature/new-button
  • Main/Master Branch – เบิ่งหลัก ซึ่งมักใช้สำหรับ Production Code
  • Remote Repository – Repository ที่เก็บไว้บน Server เช่น GitHub, GitLab หรือ VPS ของคุณเอง
  • Push – การส่ง Commits ไปยัง Remote Repository
  • Pull – การดึง Commits ล่าสุดจาก Remote Repository ลงมาเครื่องเรา
  • Merge – การผสาน Code จาก Branch หนึ่งไปยัง Branch อื่น
  • Conflict – ความขัดแย้งที่เกิดขึ้นเมื่อ Merge Code ที่มีการเปลี่ยนแปลงในส่วนเดียวกัน

ตั้งค่า Git บน Cloud VPS ของ de.co.th

ขั้นแรก เข้า SSH ไปยัง Cloud VPS ของคุณ จากนั้นรันคำสั่งต่อไปนี้เพื่อติดตั้ง Git:

sudo apt update
sudo apt install git -y
git --version

คำสั่งนี้จะติดตั้ง Git บน VPS ของคุณ เมื่อเสร็จสิ้น ให้ตั้งค่า Git Configuration พื้นฐาน:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

สร้าง Git Repository สำหรับ WordPress Theme

แทนที่จะ Track ไฟล์ WordPress ทั้งหมด ให้ Track เฉพาะ Theme และ Plugin ที่คุณพัฒนาเอง:

cd /var/www/html/wp-content/themes/my-custom-theme
git init
git config user.name "Your Name"
git config user.email "[email protected]"
git add .
git commit -m "Initial commit: custom WordPress theme"
git branch -M main

คำสั่งเหล่านี้ทำดังต่อไปนี้:

  • cd /var/www/html/wp-content/themes/my-custom-theme – เข้าไปในโฟลเดอร์ Theme ของคุณ
  • git init – เริ่มต้น Git Repository ใหม่
  • git add . – เพิ่มไฟล์ Theme ทั้งหมดเข้า Staging Area
  • git commit -m “..” – สร้าง Commit แรกด้วย Message อธิบายว่าเป็น Initial Commit
  • git branch -M main – เปลี่ยนชื่อเบิ่งจาก Master เป็น Main (มาตรฐานปัจจุบัน)

สร้าง Bare Repository บน VPS เพื่อรับ Push

Bare Repository คือ Repository ที่ไม่มี Working Directory เหมาะสำหรับรับ Push จากเครื่อง Development ของคุณ สร้างด้วยคำสั่งต่อไปนี้:

mkdir -p /home/git/repos
cd /home/git/repos
mkdir my-custom-theme.git
cd my-custom-theme.git
git init --bare

Bare Repository นี้จะทำหน้าที่เป็น Central Repository บน VPS ที่คุณ Push โค้ดไปถึง

ตั้งค่า Git Hook สำหรับ Auto-Deploy

Git Hooks คือสคริปต์ที่ทำงานอัตโนมัติเมื่อเหตุการณ์บางอย่างเกิดขึ้น Post-Receive Hook คือ Hook ที่ทำงานหลังจากได้รับ Push ใช้เพื่อ Auto-Deploy โค้ดไปยัง Production Directory:

nano /home/git/repos/my-custom-theme.git/hooks/post-receive

วางรหัสต่อไปนี้ลงในไฟล์:

#!/bin/bash

# ตัวแปรสำคัญ
TARGET="/var/www/html/wp-content/themes/my-custom-theme"
GIT_DIR="/home/git/repos/my-custom-theme.git"
BRANCH="main"
LOG_FILE="/var/log/git-deploy.log"

# อ่าน Ref ที่ได้รับ Push
while read oldrev newrev ref
do
    # ตรวจสอบว่า Push มาจาก Main Branch
    if [ "$ref" = "refs/heads/$BRANCH" ]; then
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] Deploying $BRANCH to $TARGET..." >> $LOG_FILE
        
        # Checkout ไฟล์จาก Git ไปยัง Production Directory
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
        
        # ตรวจสอบ PHP Syntax
        if ! find $TARGET -name "*.php" -exec php -l {} \; 2>&1 | grep -q "Parse error"; then
            echo "[$(date '+%Y-%m-%d %H:%M:%S')] Deploy successful!" >> $LOG_FILE
        else
            echo "[$(date '+%Y-%m-%d %H:%M:%S')] Deploy failed! PHP syntax error detected." >> $LOG_FILE
        fi
    fi
done

บันทึกไฟล์ (กด Ctrl+X, Y, Enter) จากนั้นให้สิทธิ์ Execute:

chmod +x /home/git/repos/my-custom-theme.git/hooks/post-receive

ตั้งค่า SSH Key เพื่อความปลอดภัย

แทนที่จะใช้รหัสผ่าน ให้สร้าง SSH Key สำหรับการ Push ปลอดภัยมากขึ้น บนเครื่อง Development ของคุณ รันคำสั่ง:

# บนเครื่อง Development ของคุณ
ssh-keygen -t ed25519 -C "wordpress-deployment" -f ~/.ssh/id_wordpress
# หรือใช้ RSA หากระบบ Ed25519 ไม่รองรับ
ssh-keygen -t rsa -b 4096 -C "wordpress-deployment" -f ~/.ssh/id_wordpress

ถัดไป ใส่ SSH Public Key เข้า VPS:

# คัดลอก Public Key
cat ~/.ssh/id_wordpress.pub | ssh root@your-vps-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

ตั้งค่า SSH Config เพื่อให้ง่ายต่อการเชื่อมต่อ:

cat >> ~/.ssh/config << EOF
Host wordpress-vps
    HostName your-vps-ip
    User root
    IdentityFile ~/.ssh/id_wordpress
    IdentitiesOnly yes
EOF

chmod 600 ~/.ssh/config

Push Code จากเครื่อง Development ไปยัง Production

เมื่อเตรียมพร้อมแล้ว ให้เพิ่ม Remote Repository ไปยัง Local Repository ของคุณ:

# บนเครื่อง Development ของคุณ
cd /path/to/your/local/theme

# เพิ่ม Remote
git remote add production ssh://root@your-vps-ip/home/git/repos/my-custom-theme.git

# ตรวจสอบ Remote
git remote -v

ตอนนี้คุณสามารถ Push Code ไปยัง VPS ได้ด้วยคำสั่ง:

git push production main

ทุกครั้งที่คุณ Push Code ไปยัง Main Branch Git Hook จะทำงานอัตโนมัติและ Deploy Code ไปยัง Theme Directory บน Production VPS ทันที

Branching Strategy สำหรับ WordPress Development

สำหรับโปรเจกต์ที่มีการทำงานเป็นทีม ควรใช้ Branching Strategy ที่ชัดเจน วิธีที่นิยมคือ Git Flow:

  • main branch - สำหรับ Production Code ที่เสถียร เพียงหลัง Code ผ่านการทดสอบแล้ว
  • develop branch - สำหรับ Development และ Staging โค้ดที่กำลังพัฒนา
  • feature branches - สำหรับฟีเจอร์ใหม่ เช่น feature/responsive-menu, feature/dark-mode
  • bugfix branches - สำหรับแก้ไข Bug เช่น bugfix/mobile-navigation-issue
  • hotfix branches - สำหรับแก้ไขฉุกเฉิน Production Issues เช่น hotfix/security-patch

ตัวอย่างการพัฒนาฟีเจอร์ใหม่

ลองดูตัวอย่างการพัฒนาฟีเจอร์ใหม่ด้วย Git Workflow:

# 1. สร้าง Feature Branch จาก Develop
git checkout develop
git pull origin develop
git checkout -b feature/add-custom-widget

# 2. ทำการเปลี่ยนแปลงและ Commit
echo "" >> index.php
git add index.php
git commit -m "Add custom widget support"

# 3. Push Feature Branch ไปยัง Remote
git push production feature/add-custom-widget

# 4. สำหรับ Testing บน Staging ให้สร้าง Hook สำหรับ Develop Branch
# (ไฟล์ post-receive ต้องให้สิทธิ์ Deploy ทั้ง Develop และ Main)

# 5. หลังจากทดสอบเสร็จแล้ว สร้าง Pull Request (ถ้าใช้ GitHub/GitLab)
# หรือ Merge ด้วยคำสั่ง Git
git checkout develop
git pull origin develop
git merge --no-ff feature/add-custom-widget
git push production develop

# 6. เมื่อพร้อม Release ให้ Merge เข้า Main
git checkout main
git pull origin main
git merge --no-ff develop
git tag -a v1.1.0 -m "Release version 1.1.0"
git push production main
git push production --tags

Best Practices สำหรับ Git Workflow

  • Write Clear Commit Messages - ใช้ Imperative Mood เช่น "Add responsive navigation" แทน "Added navigation"
  • Commit Often - ทำ Commit ทีละสิ่ง ไม่ใหญ่เกินไป ให้ History ชัดเจน
  • Create .gitignore File - ใช้เพื่อไม่ให้ Track ไฟล์ที่ไม่จำเป็น เช่น wp-config-local.php, node_modules, .DS_Store
  • Pull Before Push - เสมอ Pull ล่าสุดก่อน Push เพื่อลด Merge Conflicts
  • Use Pull Requests for Code Review - สำหรับทีมงาน ให้ใช้ Pull Request เพื่อ Code Review ก่อน Merge
  • Test Before Push to Production - ทดสอบโค้ดบน Local หรือ Staging ก่อน Push ไปยัง Main
  • Tag Release Versions - ใช้ Tag เพื่อให้ Release Version ชัดเจน เช่น v1.0.0, v1.1.0
  • Keep Commits Small and Focused - แต่ละ Commit ควรแก้ปัญหาเพียงหนึ่งเรื่อง

การแก้ไข Merge Conflicts

บางครั้ง Merge Conflicts อาจเกิดขึ้นเมื่อเปลี่ยนแปลงโค้ดส่วนเดียวกัน ในหลาย Branch ต่อไปนี้คือวิธีการแก้ไข:

# 1. ลอง Merge
git merge feature/some-feature

# 2. ถ้ามี Conflict จะได้ Message แสดง Conflicted Files
# 3. เปิดไฟล์ที่ Conflict และมองหา Conflict Markers:
# <<<<<<< HEAD (Current Branch)
# Your changes
# =======
# Branch changes
# >>>>>>> feature/some-feature

# 4. แก้ไขให้เหลือแค่โค้ดที่ต้องการ
# 5. ลบ Conflict Markers ออก
git add conflicted-file.php
git commit -m "Resolve merge conflict in conflicted-file.php"

# 6. Push เลยไป Production
git push production main

Monitoring และ Logging Deployment

เพื่อให้ Deployment ปลอดภัย ควรติดตามและเก็บ Log ของการ Deploy สามารถติดตามได้ด้วยคำสั่ง:

# ดู Git Log บน VPS
cd /home/git/repos/my-custom-theme.git
git log --oneline -10

# ดู Deployment Log
tail -f /var/log/git-deploy.log

# ดูสถานะ Production Directory
cd /var/www/html/wp-content/themes/my-custom-theme
git log --oneline -10

Deploy WordPress ด้วย Git บน de.co.th Cloud VPS

สำหรับผู้ใช้บริการ Cloud VPS จาก de.co.th คุณสามารถนำ Git-based Deployment มาใช้ได้อย่างเต็มประสิทธิภาพ de.co.th มี SSH Access เต็มรูปแบบ ทำให้คุณสามารถ Customize Git Setup ได้ตามต้องการ นอกจากนี้ de.co.th ยังมี Support ที่พร้อมช่วยเหลือ หากต้องการความช่วยเหลือในการตั้งค่า

สรุป

การใช้ Git สำหรับ Deploy WordPress Theme และ Plugin บน Cloud VPS ของ de.co.th เป็นวิธีที่มืออาชีพและปลอดภัย ด้วยการตั้งค่า Local Repository, Bare Repository บน VPS, Git Hooks สำหรับ Auto-Deploy, SSH Keys สำหรับความปลอดภัย และ Branching Strategy ที่ชัดเจน คุณจะมี Workflow ที่มีประสิทธิภาพ สามารถทำงานเดี่ยว หรือเป็นทีม Deploy Code ได้รวดเร็ว และย้อนกลับได้ทันทีหากมีปัญหา