cPanel Git Version Control คืออะไร?
cPanel Git Version Control เป็น Feature ที่ช่วยให้คุณ Clone และ Pull Repository จาก Git Remote เช่น GitHub หรือ GitLab มายัง Hosting ได้โดยตรง โดยไม่ต้องใช้ FTP หรืออัปโหลดไฟล์ด้วยตนเองทุกครั้ง
Workflow ที่ได้ประโยชน์สูงสุดคือ: แก้ Code ในเครื่องตัวเอง → Commit และ Push ไป GitHub → Trigger Deploy บน Hosting อัตโนมัติผ่าน Webhook ทำให้กระบวนการ Deploy ราบรื่นและลด Error จากการ Upload ผิดไฟล์
Feature นี้มาพร้อมกับ cPanel เวอร์ชัน 82 ขึ้นไป และรองรับ Repository จาก GitHub, GitLab, Bitbucket หรือ Self-hosted Git Server
สร้าง Repository ใน cPanel
- ล็อกอิน cPanel แล้วไปที่หมวด Files
- คลิก Git Version Control
- คลิกปุ่ม Create
- เลือก Clone a Repository ถ้าต้องการ Clone จาก Remote
- กรอก Clone URL: HTTPS URL ของ Repository เช่น
https://github.com/username/myproject.git - กำหนด Repository Path: Directory บน Hosting เช่น
/home/username/repos/myproject - คลิก Create
Clone จาก GitHub / GitLab
สำหรับ Private Repository ต้องใช้ Authentication:
วิธีที่ 1: HTTPS + Personal Access Token
สร้าง Personal Access Token จาก GitHub Settings > Developer settings > Personal access tokens แล้วใส่ใน Clone URL:
# รูปแบบ URL สำหรับ Private Repo
https://[email protected]/username/private-repo.git
# ตัวอย่าง
https://[email protected]/myorg/myproject.git
วิธีที่ 2: SSH Key
สร้าง SSH Key ใน cPanel > SSH Access แล้วเพิ่ม Public Key ไปยัง GitHub Settings > SSH and GPG keys จากนั้นใช้ SSH URL:
[email protected]:username/myproject.git
ตั้งค่า Deploy Path
cPanel Git Version Control ใช้ไฟล์ .cpanel.yml ในโปรเจกต์เพื่อกำหนดว่าไฟล์ไหนควร Deploy ไปที่ไหน สร้างไฟล์นี้ใน Root ของ Repository:
---
deployment:
tasks:
- export DEPLOYPATH=/home/username/public_html/
- /bin/cp -R . $DEPLOYPATH
หรือสำหรับโปรเจกต์ที่ซับซ้อนกว่า:
---
deployment:
tasks:
- export DEPLOYPATH=/home/username/public_html/myapp/
- /bin/cp -R src/ $DEPLOYPATH
- /bin/cp composer.json $DEPLOYPATH
- /usr/local/bin/php $DEPLOYPATH/../bin/composer install --no-dev
Pull อัปเดตใหม่
หลังจาก Push Code ใหม่ไปยัง Repository แล้ว มีสองวิธี Pull:
Manual Pull ผ่าน cPanel
- เข้า cPanel > Git Version Control
- คลิก Manage ข้าง Repository ที่ต้องการ
- คลิก Update from Remote หรือ Deploy HEAD Commit
Pull ผ่าน SSH
cd ~/repos/myproject
git pull origin main
Auto-deploy ด้วย Webhook
ตั้งค่า Webhook เพื่อให้ cPanel Deploy อัตโนมัติทุกครั้งที่ Push:
- ในหน้า Manage ของ Repository ใน cPanel ดู Deploy URL (Webhook URL)
- ไปที่ GitHub Repository > Settings > Webhooks > Add webhook
- ใส่ Payload URL เป็น Deploy URL จาก cPanel
- Content type:
application/json - เลือก Event: Just the push event
- คลิก Add webhook
ทดสอบโดยการ Push Commit ใหม่ไปยัง GitHub แล้วตรวจดู Deploy Log ใน cPanel
ตรวจสอบว่า Deploy สำเร็จ
# ตรวจสอบ Commit ล่าสุดที่ Deploy บน Server
cd ~/repos/myproject
git log --oneline -5
# ตรวจสอบว่าไฟล์อัปเดตแล้ว
ls -lt ~/public_html/ | head -10
# ดู Deploy Log (ถ้ามีไฟล์ Log)
cat ~/repos/myproject/.cpanel_deploy.log
# ตรวจสอบ Hash ของ Commit ล่าสุด
git rev-parse HEAD
# เทียบกับ Remote
git log HEAD..origin/main --oneline
.cpanel.yml เพื่อตรวจสอบเวลา Deploy ล่าสุดได้ง่าย: echo "Deployed at $(date)" > $DEPLOYPATH/deploy.txtแก้ปัญหาที่พบบ่อย
Authentication Failed เมื่อ Clone หรือ Pull
ตรวจสอบ Personal Access Token ว่ายังไม่หมดอายุและมีสิทธิ์ repo สำหรับ GitHub ถ้าใช้ SSH Key ตรวจสอบว่า Public Key ถูก Add ที่ GitHub แล้ว และ Private Key อยู่ที่ ~/.ssh/id_rsa บน Hosting
Deploy ไม่อัปเดตไฟล์หลัง Pull
ตรวจสอบว่า .cpanel.yml อยู่ใน Root ของ Repository และ Syntax ถูกต้อง YAML มีความละเอียดเรื่อง Indentation ต้องใช้ Space ไม่ใช่ Tab ตรวจสอบด้วย YAML Validator ออนไลน์ก่อน
Webhook ไม่ทำงาน — Deploy ไม่รันอัตโนมัติ
ตรวจสอบใน GitHub Webhook Settings ว่า Delivery ล่าสุดสำเร็จหรือ Error ถ้า Response เป็น 401 แสดงว่า Token หมดอายุหรือ URL ผิด ถ้าเป็น Timeout อาจเกิดจาก Server Deploy ใช้เวลานานเกินไป
Conflict เมื่อ Pull หลังแก้ไขไฟล์โดยตรงบน Server
ห้ามแก้ไขไฟล์โดยตรงบน Server ถ้าใช้ Git Deployment เพราะจะทำให้เกิด Conflict เมื่อ Pull ใหม่ ถ้าจำเป็นต้องแก้ ให้ทำใน Local Repository แล้ว Commit และ Push แทน
คำถามที่พบบ่อย
cPanel Git Version Control ต่างจากการรัน git pull ผ่าน SSH อย่างไร?
cPanel Git Version Control เพิ่ม UI สำหรับจัดการ Repository และรองรับ Webhook สำหรับ Auto-deploy ได้สะดวกกว่า แต่ในทางเทคนิคมันใช้ git เหมือนกัน ข้อดีของ cPanel UI คือทำได้โดยไม่ต้องใช้ SSH เหมาะสำหรับคนที่ไม่ถนัด Command Line
สามารถ Deploy หลาย Branch แยกกันได้ไหม?
ได้ สร้าง Repository หลายตัวใน cPanel ชี้ไปยัง Branch ต่างกัน เช่น main branch ชี้ไป public_html และ staging branch ชี้ไป staging.yourdomain.com แยกกัน
ถ้าไม่มี cPanel Git Version Control จะ Deploy ด้วย Git ยังไง?
ใช้ SSH เข้า Server แล้วรัน git pull โดยตรง หรือสร้าง PHP Script รับ Webhook แล้วรัน git pull ผ่าน shell_exec หรือใช้ บริการ CI/CD ภายนอกเช่น GitHub Actions ที่ Deploy ผ่าน FTP หรือ SFTP ให้อัตโนมัติ