GitLab CE ติดตั้ง Self-hosted Git Server บน Ubuntu 2026
GitLab CE เป็น DevOps platform ที่ครบถ้วนที่สุดในโลก open source รวม Git repository management CI/CD pipeline issue tracking container registry และ wiki ไว้ในที่เดียว ไม่ต้องพึ่ง GitHub หรือ Bitbucket อีกต่อไป คุณสามารถ host GitLab บน server ของตัวเองได้ ควบคุม data ทั้งหมด ไม่มีค่าใช้จ่ายรายเดือน ไม่มี user limit
บทความนี้ผมจะพาคุณติดตั้ง GitLab CE ตั้งแต่ศูนย์จนถึง production-ready พร้อม HTTPS SSL CI/CD Runner Container Registry Backup อัตโนมัติ และ performance tuning สำหรับทีมขนาด 10 ถึง 100 คน ทุกคำสั่งทดสอบบน Ubuntu 24.04 LTS ใช้ได้จริงครับ
สารบัญ
- 1. ทำไมต้อง Self-hosted GitLab
- 2. System Requirements
- 3. ติดตั้ง GitLab CE บน Ubuntu
- 4. ตั้งค่า HTTPS ด้วย Let's Encrypt
- 5. สร้าง User และ Group แรก
- 6. ติดตั้ง GitLab Runner สำหรับ CI/CD
- 7. เขียน .gitlab-ci.yml แรก
- 8. เปิด Container Registry
- 9. Backup และ Restore
- 10. Performance Tuning
- 11. Security Hardening
- 12. Troubleshooting
- 13. FAQ
1. ทำไมต้อง Self-hosted GitLab
หลายองค์กรเลือก self-hosted GitLab แทน GitHub หรือ GitLab.com เพราะหลายเหตุผล เหตุผลแรกคือ data sovereignty ข้อมูล source code ทั้งหมดอยู่บน server ของคุณเอง ไม่ส่งออกไป third party เหมาะกับองค์กรที่มี compliance requirements เช่น PDPA หรือ ISO 27001 ที่ต้องควบคุมว่าข้อมูลเก็บอยู่ที่ไหน
เหตุผลที่สองคือ cost savings GitHub Team เริ่มที่ 4 USD ต่อ user ต่อเดือน ถ้ามี developer 50 คน ต้องจ่ายเดือนละ 200 USD หรือปีละ 2400 USD GitLab CE ฟรีตลอดชีพ จ่ายแค่ค่า server ซึ่งถ้าใช้ Proxmox VE หรือ on-premise server ก็แทบไม่มีค่าใช้จ่ายเพิ่มเลย เหตุผลที่สามคือ customization สามารถ integrate กับ internal tools LDAP Active Directory หรือ SSO ขององค์กรได้ตามต้องการ
2. System Requirements
| ขนาดทีม | CPU | RAM | Disk |
|---|---|---|---|
| 1-10 คน | 2 cores | 4 GB | 30 GB SSD |
| 10-50 คน | 4 cores | 8 GB | 100 GB SSD |
| 50-100 คน | 8 cores | 16 GB | 250 GB SSD |
| 100+ คน | 16 cores | 32 GB | 500 GB+ SSD |
GitLab กิน memory ค่อนข้างมากเพราะรัน Ruby on Rails Puma PostgreSQL Redis Sidekiq และ Gitaly พร้อมกัน RAM 4 GB เป็นขั้นต่ำสุด แต่จะช้ามาก แนะนำ 8 GB ขึ้นไปสำหรับ production
3. ติดตั้ง GitLab CE บน Ubuntu
# อัพเดท system
sudo apt update && sudo apt upgrade -y
# ติดตั้ง dependencies
sudo apt install -y curl openssh-server ca-certificates tzdata perl
# เพิ่ม GitLab repository
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
# ติดตั้ง GitLab CE (เปลี่ยน URL เป็น domain ของคุณ)
sudo EXTERNAL_URL="https://gitlab.example.com" apt install -y gitlab-ce
# รอสักครู่... GitLab จะ configure ทุกอย่างให้อัตโนมัติ
# ใช้เวลาประมาณ 3-5 นาทีขึ้นอยู่กับ server
# ดู initial root password
sudo cat /etc/gitlab/initial_root_password
# Password: xxxxxxxxxxxxxxxx
# NOTE: This file will be cleaned up in the first reconfigure run after 24 hours
# เข้า browser: https://gitlab.example.com
# Login: root / password จากด้านบน
หลัง login ให้เปลี่ยน root password ทันที เข้า Admin Area จาก menu ด้านบน ไปที่ Settings แล้วปรับ Sign-up restrictions ถ้าไม่ต้องการให้คนทั่วไปสมัครใช้งานได้
4. ตั้งค่า HTTPS ด้วย Let's Encrypt
# แก้ไข /etc/gitlab/gitlab.rb
sudo nano /etc/gitlab/gitlab.rb
# เปลี่ยน external_url เป็น https
external_url 'https://gitlab.example.com'
# เปิด Let's Encrypt auto SSL
letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['admin@example.com']
letsencrypt['auto_renew'] = true
letsencrypt['auto_renew_hour'] = 3
letsencrypt['auto_renew_day_of_month'] = "*/7"
# Apply config
sudo gitlab-ctl reconfigure
# ตรวจสอบ SSL
curl -I https://gitlab.example.com
# HTTP/2 200
5. สร้าง User และ Group แรก
# สร้าง user ผ่าน command line
sudo gitlab-rails console -e production
# ใน console:
user = User.new(username: 'developer1', email: 'dev1@example.com'
name: 'Developer 1', password: 'SecureP@ss123'
password_confirmation: 'SecureP@ss123')
user.skip_confirmation!
user.save!
exit
# หรือสร้างผ่าน Web UI:
# Admin Area → Users → New User
แนะนำสร้าง Group สำหรับแต่ละทีมหรือ project แล้วเพิ่ม user เข้า Group พร้อมกำหนด role เช่น Guest Reporter Developer Maintainer หรือ Owner เพื่อควบคุม permission ได้อย่างละเอียด
6. ติดตั้ง GitLab Runner สำหรับ CI/CD
# ติดตั้ง GitLab Runner
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt install -y gitlab-runner
# Register Runner กับ GitLab
# ไปที่ GitLab Web → Admin Area → CI/CD → Runners → New Instance Runner
# copy Registration Token
sudo gitlab-runner register \
--non-interactive \
--url "https://gitlab.example.com/" \
--token "YOUR_REGISTRATION_TOKEN" \
--executor "docker" \
--docker-image "ubuntu:24.04" \
--description "docker-runner-01"
# ตรวจสอบ
sudo gitlab-runner list
# docker-runner-01 Executor=docker Token=xxx URL=https://gitlab.example.com/
# ดู status
sudo gitlab-runner status
sudo systemctl status gitlab-runner
Docker executor เป็นตัวเลือกที่ดีที่สุดสำหรับ CI/CD เพราะทุก job รันใน container ที่แยกจากกัน สะอาด ปลอดภัย ไม่กระทบกัน ถ้าต้องการ build Docker images ใน CI pipeline ต้องเปิด Docker-in-Docker (DinD) หรือใช้ Kaniko แทน สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ CI/CD อ่าน Jenkins CI/CD Pipeline เพื่อเปรียบเทียบครับ
7. เขียน .gitlab-ci.yml แรก
# .gitlab-ci.yml — ตัวอย่าง Node.js CI/CD
stages:
- test
- build
- deploy
variables:
NODE_VERSION: "20"
test:
stage: test
image: node:${NODE_VERSION}
script:
- npm ci
- npm run lint
- npm test
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
artifacts:
reports:
junit: junit.xml
build:
stage: build
image: node:${NODE_VERSION}
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 week
deploy:
stage: deploy
image: alpine:latest
script:
- apk add --no-cache openssh-client rsync
- rsync -avz --delete dist/ user@server:/var/www/app/
only:
- main
environment:
name: production
url: https://myapp.example.com
GitLab CI/CD ทำงานอัตโนมัติทุกครั้งที่ push code ไป repository ตั้ง stage เป็น test build deploy ตามลำดับ ถ้า test ไม่ผ่าน build และ deploy จะไม่รัน ป้องกันไม่ให้ code ที่มี bug ไป production สำหรับ automation ที่ซับซ้อนกว่านี้แนะนำ Ansible Automation ครับ
8. เปิด Container Registry
# แก้ /etc/gitlab/gitlab.rb
registry_external_url 'https://registry.example.com'
gitlab_rails['registry_enabled'] = true
# Apply
sudo gitlab-ctl reconfigure
# ทดสอบ push image
docker login registry.example.com
# Username: root
# Password: (personal access token)
docker build -t registry.example.com/mygroup/myapp:latest .
docker push registry.example.com/mygroup/myapp:latest
Container Registry ทำให้ทีมเก็บ Docker images ไว้ที่เดียวกับ source code ใช้ใน CI/CD pipeline ได้สะดวก ไม่ต้องพึ่ง Docker Hub หรือ registry ภายนอก สำหรับการจัดการ container แบบ multi-service ดู Docker Compose ครับ
9. Backup และ Restore
# สร้าง backup
sudo gitlab-backup create
# Backup จะเก็บไว้ที่ /var/opt/gitlab/backups/
ls -la /var/opt/gitlab/backups/
# 1708646400_2026_02_22_17.0.0_gitlab_backup.tar
# backup config files แยก (สำคัญมาก!)
sudo cp /etc/gitlab/gitlab.rb /backup/gitlab.rb
sudo cp /etc/gitlab/gitlab-secrets.json /backup/gitlab-secrets.json
# ตั้ง cron backup ทุกวันตี 2
sudo crontab -e
# เพิ่ม:
0 2 * * * /opt/gitlab/bin/gitlab-backup create CRON=1
# Restore
sudo gitlab-ctl stop puma
sudo gitlab-ctl stop sidekiq
sudo gitlab-backup restore BACKUP=1708646400_2026_02_22_17.0.0
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
สิ่งสำคัญที่สุดคือต้อง backup ไฟล์ gitlab-secrets.json แยกจาก backup หลัก เพราะไฟล์นี้เก็บ encryption keys สำหรับ CI/CD variables 2FA secrets และ encrypted database columns ถ้าไม่มีไฟล์นี้จะ restore ไม่ได้แม้มี backup tar file สำหรับ backup strategy โดยละเอียดอ่านได้ที่ Rsync Backup Linux Guide ครับ
10. Performance Tuning
# /etc/gitlab/gitlab.rb — ปรับสำหรับ server RAM 8 GB
# ลด Puma workers (default = CPU cores)
puma['worker_processes'] = 2
puma['min_threads'] = 1
puma['max_threads'] = 4
# ลด Sidekiq concurrency
sidekiq['concurrency'] = 10
# PostgreSQL shared buffers
postgresql['shared_buffers'] = "512MB"
# Redis maxmemory
redis['maxmemory'] = "256mb"
redis['maxmemory_policy'] = "allkeys-lru"
# Gitaly cgroup limits
gitaly['cgroups_memory_limit'] = 2147483648 # 2 GB
# Apply
sudo gitlab-ctl reconfigure
สำหรับ monitoring GitLab มี Prometheus และ Grafana ในตัว เข้าถึงได้ที่ /-/grafana ดู dashboard สำหรับ Gitaly Sidekiq PostgreSQL และ overall system health สำหรับ monitoring ที่ครอบคลุมกว่านี้ใช้ Prometheus Monitoring ร่วมด้วยครับ
11. Security Hardening
- ปิด Sign-up — Admin Area จาก Settings จาก General จาก Sign-up restrictions จาก ปิด Sign-up enabled
- บังคับ 2FA — Admin Area จาก Settings จาก General จาก Sign-in restrictions จาก Two-factor authentication required
- SSH Key — บังคับให้ user ใช้ SSH key สำหรับ git push ปิด password authentication
- Rate Limiting — ตั้ง rate limit สำหรับ API และ login ป้องกัน brute force
- Firewall — เปิดเฉพาะ port 22 (SSH) 80 (HTTP) 443 (HTTPS) ปิดทุกอย่างอื่น
- อัพเดทเสมอ — sudo apt update && sudo apt install -y gitlab-ce อย่างน้อยเดือนละครั้ง
สำหรับ SSH security ของ server ที่รัน GitLab แนะนำอ่าน SSH Security Hardening ด้วยครับ เรื่อง VPN สำหรับเข้าถึง GitLab จากภายนอกอ่าน WireGuard VPN
12. Troubleshooting
GitLab ช้ามาก หรือ 502 Error
# ตรวจ memory usage
free -h
# ถ้า RAM เต็มต้องเพิ่ม RAM หรือลด worker
# ตรวจ service status
sudo gitlab-ctl status
# ถ้า puma หรือ sidekiq ล่ม ให้ restart
sudo gitlab-ctl restart
# ดู log
sudo gitlab-ctl tail
SSL Certificate ไม่ renew
# renew manual
sudo gitlab-ctl renew-le-certs
# ตรวจ firewall ว่าเปิด port 80 สำหรับ ACME challenge
Runner ไม่ทำงาน
# ตรวจ runner status
sudo gitlab-runner verify
sudo gitlab-runner list
# ตรวจว่า Docker daemon รันอยู่
sudo systemctl status docker
GitLab กับ DevOps Workflow จริงในองค์กร
จากประสบการณ์ที่ deploy GitLab ให้หลายองค์กร ผมพบว่า workflow ที่ได้ผลดีที่สุดคือ GitLab Flow ที่รวมจุดแข็งของ Git Flow และ GitHub Flow เข้าด้วยกัน โดยใช้ main branch เป็น production-ready code มี feature branches สำหรับ development และใช้ environment branches เช่น staging และ production สำหรับ deployment
สิ่งที่ทำให้ GitLab โดดเด่นกว่า GitHub สำหรับองค์กรคือ built-in CI/CD ที่ไม่ต้องพึ่ง third party service เช่น GitHub Actions ต้องใช้ minutes ที่มีจำกัดใน free tier แต่ GitLab Runner รันบน server ของคุณเอง ไม่มีจำกัด minutes เลย นอกจากนี้ GitLab ยังมี built-in container registry package registry และ infrastructure monitoring ในที่เดียว
สำหรับผู้ที่สนใจการลงทุนและ trading automation แนะนำ iCafeForex.com ที่มีบทเรียน Forex ครบวงจร รวมถึง XMSignal.com/th สำหรับสัญญาณเทรดฟรี รีวิวอุปกรณ์ IT และ server hardware ที่ SiamLancard.com และ Siam2R.com ครับ
13. FAQ
GitLab CE กับ GitLab EE ต่างกันอย่างไร
GitLab CE เป็นเวอร์ชันฟรีและ open source มี feature ครบสำหรับทีมขนาดเล็กถึงกลาง รวมถึง Git repository CI/CD issue tracking และ container registry ส่วน GitLab EE มี feature เพิ่มเช่น advanced security scanning compliance management และ premium support เหมาะกับองค์กรขนาดใหญ่
GitLab CE ต้องใช้ server spec ขั้นต่ำเท่าไหร่
แนะนำ CPU 4 cores ขึ้นไป RAM 8 GB ขั้นต่ำ แนะนำ 16 GB สำหรับทีม 50 คนขึ้นไป Disk 50 GB SSD ขึ้นไป ถ้ามี user น้อยกว่า 10 คน อาจใช้ RAM 4 GB ได้แต่จะช้า
GitLab CE รองรับ CI/CD ในตัวหรือไม่
รองรับครบถ้วน แค่สร้างไฟล์ .gitlab-ci.yml ใน repository และติดตั้ง GitLab Runner ก็ใช้ CI/CD ได้เลย รองรับ Docker executor Shell executor และ Kubernetes executor
ย้ายจาก GitHub มา GitLab ยากไหม
ง่ายมาก GitLab มี import tool ในตัว เข้า New Project เลือก Import Project จาก GitHub จะ import ทั้ง repository issues pull requests wiki และ labels ให้อัตโนมัติ
GitLab CE มี Container Registry ในตัวไหม
มีครับ เปิดใช้งานแค่เพิ่ม registry_external_url ใน gitlab.rb แล้ว reconfigure จากนั้น developer สามารถ docker push image ไปเก็บใน GitLab ได้เลย
สรุป
GitLab CE เป็นตัวเลือกที่ดีที่สุดสำหรับองค์กรที่ต้องการ DevOps platform ครบวงจรโดยไม่ต้องจ่ายค่า subscription รายเดือน ติดตั้งครั้งเดียวใช้ได้ตลอด ควบคุม data เอง ปรับแต่งได้ตามต้องการ ลงทุนเวลาเรียนรู้ GitLab CI/CD สักสัปดาห์ แล้วทีมของคุณจะได้ DevOps workflow ที่ทันสมัยเทียบเท่าบริษัทเทคโนโลยีชั้นนำของโลกครับ
GitLab CE เป็น DevOps platform ที่ดีที่สุดสำหรับองค์กรที่ต้องการ self-hosted Git server ฟรี feature ครบ ตั้งแต่ Git repository CI/CD Container Registry ไปจนถึง issue tracking และ wiki ทั้งหมดในที่เดียว ลงทุนเวลาติดตั้งสัก 30 นาที แล้วคุณจะได้ DevOps platform ที่ใช้ได้ตลอดโดยไม่ต้องจ่ายค่า subscription ให้ GitHub อีกเลยครับ
บทความแนะนำ
สนใจ Automation ด้านการเงิน? iCafeForex.com สอน Forex ครบวงจร พร้อม EA Trading อัตโนมัติ ที่ทำงานเหมือน GitLab CI/CD — ตั้งครั้งเดียวแล้วทำงานให้คุณตลอด