📚 Series: Belajar Golang dari Nol sampai Deploy
← Part 7
Part 8: Deploy ke VPS 🏁
Persiapan Sebelum Deploy
Pastikan kamu punya:
- VPS Ubuntu 22.04 (DigitalOcean, Vultr, Contabo, atau Biznet Gio)
- Domain yang sudah diarahkan ke IP VPS
- Repository di GitHub
Buat Dockerfile
Go menggunakan multi-stage build — stage pertama untuk kompilasi, stage kedua untuk image produksi yang super kecil.
# Stage 1: Build
FROM golang:1.22-alpine AS builder
WORKDIR /app
# Copy dependency files dulu (memanfaatkan Docker layer cache)
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build binary — CGO_ENABLED=0 untuk binary statis
RUN CGO_ENABLED=0 GOOS=linux go build -o todo-api ./main.go
# Stage 2: Production image (hanya binary, super ringan ~15MB)
FROM alpine:3.19
WORKDIR /app
# Install CA certificates (untuk HTTPS calls)
RUN apk --no-cache add ca-certificates tzdata
# Set timezone
ENV TZ=Asia/Jakarta
# Copy binary dari stage build
COPY --from=builder /app/todo-api .
EXPOSE 8080
CMD ["./todo-api"]
docker-compose.yml
version: '3.9'
services:
app:
build: .
container_name: todo-api
restart: unless-stopped
ports:
- "8080:8080"
environment:
- DB_HOST=db
- DB_PORT=5432
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_NAME=${DB_NAME}
depends_on:
db:
condition: service_healthy
networks:
- todo-network
db:
image: postgres:16-alpine
container_name: todo-db
restart: unless-stopped
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
interval: 10s
timeout: 5s
retries: 5
networks:
- todo-network
volumes:
postgres_data:
networks:
todo-network:
driver: bridge
Setup VPS
# 1. Update sistem
sudo apt update && sudo apt upgrade -y
# 2. Install Docker
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER
newgrp docker
# 3. Install Docker Compose
sudo apt install docker-compose-plugin -y
# 4. Verifikasi
docker --version
docker compose version
Deploy Pertama Kali
# Clone repository
git clone https://github.com/username/todo-api.git
cd todo-api
# Buat file .env
cp .env.example .env
nano .env # isi dengan nilai production
# Build dan jalankan
docker compose up -d --build
# Cek log
docker compose logs -f app
# Cek container berjalan
docker compose ps
Setup Nginx sebagai Reverse Proxy
# Install Nginx
sudo apt install nginx -y
# Buat konfigurasi
sudo nano /etc/nginx/sites-available/todo-api
server {
listen 80;
server_name api.domainmu.com;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
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;
}
}
# Aktifkan site
sudo ln -s /etc/nginx/sites-available/todo-api /etc/nginx/sites-enabled/
sudo nginx -t # test konfigurasi
sudo systemctl reload nginx
SSL dengan Certbot (HTTPS Gratis!)
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d api.domainmu.com
# Auto-renewal
sudo systemctl status certbot.timer
Update & Redeploy (Workflow Harian)
# Di lokal — push perubahan
git add .
git commit -m "feat: tambah fitur X"
git push origin main
# Di VPS — pull dan rebuild
cd ~/todo-api
git pull origin main
docker compose up -d --build
# Atau dengan script deploy.sh
cat > deploy.sh << 'EOF'
#!/bin/bash
set -e
git pull origin main
docker compose up -d --build
docker image prune -f
echo "✅ Deploy berhasil!"
EOF
chmod +x deploy.sh
Monitoring Dasar
# Lihat resource usage
docker stats
# Lihat log real-time
docker compose logs -f --tail=100
# Masuk ke dalam container untuk debug
docker compose exec app sh
# Backup database
docker compose exec db pg_dump -U postgres todo_db > backup.sql
# Restore database
docker compose exec -T db psql -U postgres todo_db < backup.sql
Selamat! Kamu Sudah Deploy Go API 🎉
Kamu telah berhasil menyelesaikan seluruh series Belajar Golang dari Nol sampai Deploy. Berikut recap perjalanan kita:
- ✅ Install Go dan setup project dengan Go Modules
- ✅ Sintaks dasar: variabel, tipe data, if/for/switch
- ✅ Fungsi, slice, map, dan struct dengan method
- ✅ Interface dan error handling yang idiomatic
- ✅ Concurrency dengan goroutine dan channel
- ✅ REST API lengkap dengan Gin framework
- ✅ Database PostgreSQL dengan GORM ORM
- ✅ Deploy ke VPS dengan Docker dan SSL
Langkah Selanjutnya
- 🔐 Tambahkan autentikasi JWT
- 📝 Setup Swagger/OpenAPI documentation
- 🧪 Tulis unit test dan integration test
- 🔄 Setup CI/CD dengan GitHub Actions
- 📊 Tambahkan observability (Prometheus + Grafana)
Checklist Progress Final
🎊 Selamat telah menyelesaikan series Golang!
Kamu sekarang sudah bisa membangun dan deploy REST API production-ready dengan Go.
Komentar 0