ตั้งค่า Git บน VPS ให้ Deploy เว็บอัตโนมัติด้วย SSH

การ Deploy เว็บอัตโนมัติด้วย Git และ SSH เป็นวิธีที่ปลอดภัยและมีประสิทธิภาพ ในบทความนี้เราจะสอนการตั้งค่า Git เพื่อให้ Deploy เว็บไซต์อัตโนมัติผ่าน SSH พร้อมการจัดการความปลอดภัยที่ดี

ทำไมต้องใช้ SSH สำหรับ Git Deploy

SSH (Secure Shell) ให้ความปลอดภัยที่ดีกว่า HTTPS เพราะ:

  • ใช้ Public Key Cryptography แทน Username/Password
  • ไม่เสี่ยงต่อ Brute Force Attack
  • สามารถจำกัดสิทธิ์ของแต่ละ Key
  • ไม่ต้องเก็บ Password ไว้บนเซิร์ฟเวอร์

ขั้นตอนการสร้าง SSH Keys

ขั้นแรก ให้สร้าง SSH Key Pair บนเครื่อง Local:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/deploy_key -N ""

คำสั่งนี้จะสร้าง SSH Key ที่ปลอดภัย:

  • -t rsa: ใช้ RSA algorithm
  • -b 4096: ความยาว 4096 bits (ปลอดภัยสูง)
  • -f ~/.ssh/deploy_key: บันทึกลงไฟล์ deploy_key
  • -N “”: ไม่ใช้ passphrase (สำหรับ Automation)

นี่จะสร้างไฟล์สองไฟล์:

  • ~/.ssh/deploy_key (private key – เก็บที่ Local)
  • ~/.ssh/deploy_key.pub (public key – ส่งไปที่ VPS)

ตั้งค่า Public Key บน VPS

ขั้นแรก ให้ Secure Copy (scp) ไฟล์ public key ไปยัง VPS:

scp ~/.ssh/deploy_key.pub root@your-vps-ip:/tmp/deploy_key.pub

จากนั้น SSH เข้า VPS และตั้งค่า authorized_keys:

ssh root@your-vps-ip

ในโฟลเดอร์ Git user บน VPS:

mkdir -p /home/git/.ssh
cat /tmp/deploy_key.pub >> /home/git/.ssh/authorized_keys

ตั้งค่าสิทธิ์อย่างเหมาะสม:

chmod 700 /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys
chown -R git:git /home/git/.ssh

ตั้งค่า SSH Config บน Local

เพื่อให้ง่ายต่อการใช้ สร้างไฟล์ config สำหรับ SSH:

nano ~/.ssh/config

เพิ่มเนื้อหา:

Host vps-deploy
    HostName your-vps-ip
    User git
    IdentityFile ~/.ssh/deploy_key
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null

บันทึกไฟล์แล้วตั้งค่าสิทธิ์:

chmod 600 ~/.ssh/config

ตั้งค่า Git Remote บน Local

cd /path/to/local/project
git remote add deploy git@vps-deploy:/var/git/myproject.git

หรือใช้ SSH URL ตรงๆ:

git remote add deploy git@your-vps-ip:/var/git/myproject.git

Deployment Workflow ด้วย SSH

1. Local Workflow

จากเครื่อง Local ของคุณ:

git add .
git commit -m "New feature"
git push deploy main

2. Automated Checkout บน VPS

ในไฟล์ post-receive hook บน VPS (/var/git/myproject.git/hooks/post-receive):

#!/bin/bash
WORK_TREE=/var/www/myproject
GIT_DIR=/var/git/myproject.git
LOG_FILE=/var/log/deploy.log

while read oldrev newrev ref
do
    if [[ $ref =~ .*/main$ ]]; then
        {
            echo "[$(date)] Starting deployment from SSH push"
            git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f main
            echo "[$(date)] Deployed successfully!"
        } >> $LOG_FILE 2>&1
    fi
done

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ Git Hooks ดู Auto Deploy ด้วย Git Hooks: post-receive บน VPS

เคล็ดลับการรักษาความปลอดภัย

1. ใช้ SSH Key แทนการพิมพ์ Password

  • ไม่ต้องกังวลเรื่อง Password ที่อาจถูกสะกดได้
  • ลดความเสี่ยง Brute Force Attack

2. จำกัดสิทธิ์ของ Deploy User

ในไฟล์ authorized_keys บน VPS ให้ใช้ command restriction:

command="/usr/bin/git-shell -c $SSH_ORIGINAL_COMMAND" ssh-rsa AAAA...

3. ตั้งค่า Firewall เพื่อ Allow SSH จากที่อยู่ IP เฉพาะเท่านั้น

ufw allow from 203.0.113.0/24 to any port 22

4. เปิด SSH ที่ Port ไม่ใช่ 22 เพื่อให้ปลอดภัยมากขึ้น

แก้ไข /etc/ssh/sshd_config:

Port 2222

5. บันทึก Deploy logs เพื่อสำหรับการตรวจสอบ

tail -f /var/log/deploy.log

ตัวอย่างการ Test SSH Connection

ตรวจสอบว่า SSH Connection ทำงานได้:

ssh -i ~/.ssh/deploy_key git@your-vps-ip

หากเชื่อมต่อได้สำเร็จ จะเห็นข้อความยินดีต้อนรับจาก Git Server

ตั้งค่า Bare Repository สำหรับ Deploy

หากยังไม่ได้สร้าง Bare Repository ดู ตั้งค่า Bare Git Repository บน VPS สำหรับ Deploy

การใช้ SSH Key สำหรับ CI/CD Platforms

คุณสามารถใช้ SSH Key แบบเดียวกันในแพลตฟอร์ม CI/CD เช่น GitHub Actions, GitLab CI เพื่อให้สามารถ Deploy ได้อัตโนมัติ

สรุป

การตั้งค่า Git Auto Deploy ด้วย SSH นั้นให้ความมั่นใจและประสิทธิภาพสูง สามารถ Deploy เว็บไซต์ได้อย่างปลอดภัยโดยไม่ต้องเปิดเผย Password ด้วยการใช้ SSH Keys และ Git Hooks จะช่วยให้การ Deploy เป็นไปโดยอัตโนมัติ ลดข้อผิดพลาด และเพิ่มความเร็วในการ Release Features ใหม่บน ผู้ให้บริการโฮสติ้ง Cloud VPS ของคุณ