How to Set Up a VPS as a Development Server: Git, Docker, and CI/CD Pipeline Setup

How to Set Up a VPS as a Development Server: Git, Docker, and CI/CD Pipeline Setup

A VPS is more than just a web host — it’s an excellent platform for a personal development server where you can code, test, and deploy applications. This step-by-step tutorial walks you through setting up a complete development environment on a fresh Ubuntu 24.04 VPS, including Git server configuration, Docker containerization, and a CI/CD pipeline using GitHub Actions.

Prerequisites

  • A VPS running Ubuntu 24.04 LTS (provision one from Virtual Servers VPS — any plan with 2 GB RAM or more)
  • SSH access with a sudo-enabled user
  • A domain name pointing to your VPS IP (optional but recommended)
  • GitHub account for CI/CD integration

Step 1: Initial Server Hardening

Before installing anything, secure your server:

# Update system
sudo apt update && sudo apt upgrade -y

# Install essential tools
sudo apt install -y curl wget git ufw fail2ban

# Configure firewall
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable

# Enable fail2ban for SSH protection
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Step 2: Install Docker and Docker Compose

Docker lets you run applications in isolated containers, making dependency management trivial.

# Install Docker using the official convenience script
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# Add your user to the docker group (no sudo needed for docker commands)
sudo usermod -aG docker $USER

# Log out and back in, or run:
newgrp docker

# Install Docker Compose plugin
sudo apt install -y docker-compose-plugin

# Verify installation
docker --version
docker compose version

Step 3: Set Up Git Server with Gitea

Gitea is a lightweight, self-hosted Git service that runs perfectly on a VPS. It’s a great alternative to GitHub for private repositories.

# Create a directory for Gitea
mkdir -p ~/gitea
cd ~/gitea

# Create docker-compose.yml
cat << 'EOF' > docker-compose.yml
version: "3"
services:
  gitea:
    image: gitea/gitea:latest
    container_name: gitea
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - GITEA__server__DOMAIN=git.yourdomain.com
      - GITEA__server__ROOT_URL=https://git.yourdomain.com
    volumes:
      - ./data:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3000:3000"
      - "2222:22"
    restart: unless-stopped
EOF

# Start Gitea
docker compose up -d

Access Gitea at http://your-vps-ip:3000. Configure a reverse proxy with Nginx and Let’s Encrypt for HTTPS. See the VPS guides section for Nginx reverse proxy setup instructions.

Step 4: Set Up a CI/CD Pipeline with GitHub Actions

Connect your VPS to GitHub Actions for automated deployments. First, set up a deployment user on your VPS:

# Create a deploy user
sudo adduser --disabled-password deploy
sudo usermod -aG docker deploy

# Generate SSH key for the deploy user
sudo -u deploy ssh-keygen -t ed25519 -f /home/deploy/.ssh/id_ed25519 -N ""

# Add the public key to authorized_keys
sudo -u deploy sh -c 'cat /home/deploy/.ssh/id_ed25519.pub >> /home/deploy/.ssh/authorized_keys'

# Display the private key (add to GitHub Secrets)
sudo cat /home/deploy/.ssh/id_ed25519

Now create a GitHub Actions workflow in your repository (.github/workflows/deploy.yml):

name: Deploy to VPS
on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build Docker image
        run: docker build -t my-app:latest .

      - name: Deploy to VPS
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.VPS_HOST }}
          username: deploy
          key: ${{ secrets.VPS_SSH_KEY }}
          script: |
            cd /opt/my-app
            git pull origin main
            docker compose pull
            docker compose up -d --build

Add your VPS IP and SSH private key as secrets (VPS_HOST and VPS_SSH_KEY) in your GitHub repository settings.

Step 5: Set Up Docker Registry for Private Images

For a complete development workflow, run a private Docker registry on your VPS:

# Create registry directory
mkdir -p ~/docker-registry
cd ~/docker-registry

# Create docker-compose.yml for registry
cat << 'EOF' > docker-compose.yml
version: "3"
services:
  registry:
    image: registry:2
    container_name: docker-registry
    ports:
      - "5000:5000"
    volumes:
      - ./data:/var/lib/registry
    environment:
      - REGISTRY_STORAGE_DELETE_ENABLED=true
    restart: unless-stopped
EOF

# Start the registry
docker compose up -d

# Tag and push an image
docker tag my-app:latest your-vps-ip:5000/my-app:latest
docker push your-vps-ip:5000/my-app:latest

Step 6: Monitor Your Development Server

Keep an eye on resource usage with these tools:

# Install Netdata for real-time monitoring
bash <(curl -Ss https://my-netdata.io/kickstart.sh)

# Check Docker container stats
docker stats

# View logs for all containers
docker compose logs -f

# Check system resource usage
htop

With this setup, your VPS becomes a complete development platform: version control with Gitea, containerized applications with Docker, continuous deployment via GitHub Actions, and monitoring with Netdata. For more VPS configuration tutorials, visit Virtual Servers VPS.

Leave a Reply