Deploying web applications directly from GitHub to a Linux Virtual Private Server (VPS) is the industry standard for modern software development. Using Git version control eliminates tedious manual file uploads, safeguards code integrity, and enables fully automated Continuous Integration / Continuous Deployment (CI/CD) pipelines.

Whether you are running Ubuntu, Debian, AlmaLinux, Rocky Linux, RHEL, or CentOS, setting up GitHub deployment can be achieved using two distinct workflows:

  • Method 1: Manual Git Deployment (Pull-Based) – Cloning your repository using secure GitHub Deploy Keys and pulling updates with git pull over SSH.
  • Method 2: Automated CI/CD Deployment via GitHub Actions (Push-Based) – Automatically deploying updates with zero downtime every time you push code to your main branch.

In this comprehensive guide, we will walk you through both manual and automated GitHub deployment workflows across all major Linux distributions.


Prerequisites


Step 1: Install Git and Generate a GitHub Deploy Key

1. Install Git on Your Server:

  • On Ubuntu / Debian: sudo apt update && sudo apt install git -y
  • On AlmaLinux / Rocky Linux / RHEL: sudo dnf install git -y

2. Generate a Dedicated Deploy Key:

ssh-keygen -t ed25519 -C "linux-vps-deploy-key"

Press ENTER to accept the default path (~/.ssh/id_ed25519) and leave the passphrase empty.

3. Add Public Key to GitHub:

  1. Display the public key:
    cat ~/.ssh/id_ed25519.pub
  2. In GitHub, go to your repository > Settings > Deploy Keys > Add deploy key.
  3. Paste the key and click Add key (leave "Allow write access" unchecked for read-only security).
  4. Verify connection from your server terminal:
    ssh -T [email protected]

    Type yes when prompted. You will see: "Hi username/repo! You've successfully authenticated".


Method 1: Manual Deployment via Git Pull

Step 1: Clone Your Repository

sudo mkdir -p /var/www/my-app
sudo chown -R $USER:$USER /var/www/my-app

git clone [email protected]:username/my-web-app.git /var/www/my-app

Step 2: Install Dependencies & Build

  • For Node.js / React / Vue Apps: cd /var/www/my-app && npm install && npm run build
  • For Python (Django/Flask): cd /var/www/my-app && python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt
  • For PHP / Laravel: cd /var/www/my-app && composer install --no-dev --optimize-autoloader

Step 3: Deploying Future Updates

cd /var/www/my-app
git pull origin main
# Rebuild assets or restart process (e.g. pm2 restart all OR sudo systemctl restart gunicorn)

Method 2: Automated CI/CD Deployment via GitHub Actions (Recommended)

Step 1: Generate a CI/CD SSH Key

On your local computer (or server), generate a key pair for GitHub Actions:

ssh-keygen -t ed25519 -f ~/.ssh/github_action_key -C "github-actions-deploy"
cat ~/.ssh/github_action_key.pub | ssh username@YOUR_SERVER_IP "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Step 2: Add Secrets to GitHub Repository

In your GitHub repo, go to Settings > Secrets and variables > Actions > New repository secret and add:

  • HOST: Your Linux VPS IP address (e.g. 107.178.111.179).
  • USERNAME: Your server sudo username (e.g. sammy).
  • SSH_KEY: The private key content of github_action_key.
  • PORT: 22 (or your custom SSH port).

Step 3: Create GitHub Actions Workflow File

In your project repository, create .github/workflows/deploy.yml:

name: Deploy to Linux VPS

on:
  push:
    branches: [ main ]

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

      - name: Deploy to VPS over SSH
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          port: ${{ secrets.PORT }}
          script: |
            cd /var/www/my-app
            git pull origin main
            
            # Build Node.js / React
            npm install
            npm run build
            
            # Reload web server
            sudo systemctl reload nginx

Commit and push: git add .github/workflows/deploy.yml && git commit -m "Add CI/CD" && git push origin main.


Security Best Practices for GitHub Deployments

  • Keep Secrets Out of Git: Always store environment variables in a server-side .env file and add .env to your .gitignore.
  • SELinux on AlmaLinux/Rocky Linux: Run sudo chcon -Rt httpd_sys_content_t /var/www/my-app to ensure Nginx can read the cloned files.
  • Passwordless Sudo for Web Reloads: Add sammy ALL=(ALL) NOPASSWD: /bin/systemctl reload nginx to /etc/sudoers.d/deploy.

Frequently Asked Questions & Troubleshooting

Q: Why does git pull fail with "Host key verification failed"?
A: Run ssh-keyscan -H github.com >> ~/.ssh/known_hosts on your server to add GitHub to your known hosts list.

Q: How do I roll back if a deployment causes errors?
A: Run git log --oneline on the server to find the last stable commit, then run git checkout <commit-hash>.


Need Further Assistance?

If you encounter issues configuring Git deployments or GitHub Actions CI/CD on your Linux VPS, our technical support team is available 24/7. Feel free to submit a support ticket for prompt assistance.

Var dette svaret til hjelp? 0 brukere syntes dette svaret var til hjelp (0 Stemmer)