Solo DevOps Toolkit

Guide to deploying apps via Git on a VPS

Guide to deploying apps via Git on a VPS

Git-Based VPS Deployment

Comprehensive Guide to Deploying Apps via Git on a VPS: Updated for 2024

Deploying applications efficiently, securely, and with minimal fuss remains a cornerstone of modern web development. Using Git for deployment on a Virtual Private Server (VPS) continues to be a popular, streamlined approach, especially for small teams, personal projects, or situations where simplicity and control are paramount. Recent developments and best practices have further refined this method, making it more robust, secure, and easier to implement.

This article synthesizes the latest insights—building on foundational steps and incorporating new operational strategies—to provide an up-to-date, comprehensive guide to deploying apps via Git on a VPS.


1. Preparing Your VPS Environment: The Foundation

A solid deployment process starts with a well-prepared server. When provisioning a new Linux VPS, efficiency is key. Here are essential commands and steps to streamline setup:

# Update all packages to ensure the server is current
sudo apt update && sudo apt upgrade -y

# Install necessary tools (git, ufw for firewall, nginx or apache for web server)
sudo apt install git nginx ufw -y

# Create a dedicated deployment user
sudo adduser deploy --disabled-password --gecos ""
sudo usermod -aG sudo deploy

# Set up SSH key-based authentication
sudo -u deploy ssh-keygen -t ed25519 -C "deploy@yourdomain.com"
# Copy the generated public key to your local machine and add it to ~/.ssh/authorized_keys

Security tips:

  • Disable root SSH login by editing /etc/ssh/sshd_config:
    PermitRootLogin no
    
  • Enable UFW firewall with rules:
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    sudo ufw allow ssh
    sudo ufw allow 'Nginx Full'  # if using nginx
    sudo ufw enable
    

These steps ensure your server is current, secure, and ready for deployment tasks.


2. Setting Up a Bare Git Repository with Hooks for Deployment

Creating a bare repository is the first step:

sudo -u deploy git init --bare /home/deploy/repo.git

Configuring deployment hooks:

  • The post-receive hook automates the deployment process, checking out the latest code into your web directory:
sudo -u deploy nano /home/deploy/repo.git/hooks/post-receive

Insert:

#!/bin/bash
GIT_WORK_TREE=/var/www/myapp git checkout -f
# Optional: restart web or app services
# systemctl restart myapp.service

Make the hook executable:

sudo -u deploy chmod +x /home/deploy/repo.git/hooks/post-receive

This setup ensures every push updates your application automatically.


3. Securing the Deployment Process: Best Practices

Security remains paramount. Recent developments emphasize fine-grained control and server hardening:

  • Use separate SSH keys for deployment, restrict commands with command= in authorized_keys.
  • Disable root login and limit SSH access to specific IPs if possible.
  • Implement Fail2Ban or similar tools to prevent brute-force attacks.
  • Regularly update system packages (sudo apt update && sudo apt upgrade -y) and monitor logs.

By restricting SSH keys and access, and by hardening services, you significantly reduce potential attack vectors.


4. Automating Deployment: From Local to Server

From your local machine, add a remote pointing to your server:

git remote add production deploy@your-vps:/home/deploy/repo.git

Push your code:

git push production main

The post-receive hook then triggers deployment automatically.

Deploy scripts can further streamline this:

# Example deploy.sh
git push production main
# Optional: run further deployment commands, e.g., database migrations

This process minimizes manual intervention and ensures consistent deployment procedures.


5. Permissions and Directory Security

Proper ownership and permissions prevent accidental or malicious modifications:

sudo chown -R www-data:www-data /var/www/myapp
sudo chmod -R 755 /var/www/myapp

Important:

  • Keep web directories read-only for the web server user where possible.
  • Use chmod and chown carefully to avoid exposing sensitive files.

Regular permission audits help maintain security and operational stability.


6. Managing Releases and Rollbacks

An advanced strategy involves maintaining timestamped releases, facilitating quick rollbacks:

  • Create timestamped release directories in /var/www/releases/.
  • Update the post-receive hook to checkout code into a new timestamped folder rather than overwriting the current release.
  • To rollback, point your web server to the previous release directory.

Using Git tags or branches can also help target specific versions:

# Tag a release
git tag -a v1.0 -m "Stable release v1.0"
# Push tags
git push --tags

Rollback example:

  • Checkout previous commit locally
  • Push to the server
  • Update the deployment to point back to the prior release

This approach ensures safe, reversible deployments, crucial for minimizing downtime.


7. Operational Considerations and Latest Developments

Recent updates emphasize monitoring, minimal external dependencies, and automation:

  • Monitoring tools like Prometheus, Grafana, or simple log watchers help track application health.
  • Automated updates: Regularly update server packages and dependencies.
  • Minimal external tooling: Rely on simple Git hooks, SSH, and scripting rather than complex CI/CD pipelines, aligning with the philosophy of self-hosted, controlled deployment.

A recent article encapsulates this philosophy:

"Setting up a fresh Linux system and getting it ready for your work can sometimes be a tedious time sink. It's why I have a set of commands I run on every new install to speed up provisioning."

This highlights the importance of standardized setup scripts to accelerate initial server preparation.


Current Status and Implications

Deploying via Git remains a robust, flexible, and secure method—especially when combined with best practices in server hardening, permission management, and rollback strategies. As of 2024, the approach benefits from:

  • Improved automation techniques
  • Enhanced security measures
  • Clearer workflows for managing multiple releases

By following these updated guidelines, you can confidently deploy, maintain, and roll back applications with minimal downtime and maximum control, all while keeping your infrastructure secure and resilient.


In summary, deploying apps via Git on a VPS is an ongoing process of refinement. The latest best practices focus on automation, security, and operational robustness—ensuring your deployment pipeline is both reliable and scalable for future growth.

Sources (2)
Updated Mar 2, 2026
Guide to deploying apps via Git on a VPS - Solo DevOps Toolkit | NBot | nbot.ai