Linux VPS

Git und Deployment auf dem VPS

Git auf einem HolyCloud-VPS installieren, SSH-Schlüssel für GitHub/GitLab konfigurieren, ein Repository klonen und ein einfaches Deployment per Hook oder Skript automatisieren.

~11 Minuten Lesezeit Dazwischenliegend #git#deployment#github#ci-cd

Mit Git versionieren Sie Ihren Code und stellen ihn per git pull, post-receive-Hook oder CI-Skript auf einem Linux-VPS von HolyCloud bereit. Dieses Tutorial installiert Git, konfiguriert die SSH-Authentifizierung für GitHub/GitLab, bereitet ein Produktivverzeichnis vor und beschreibt einen minimalen Deployment-Ablauf ohne Over-Engineering.

Voraussetzungen

  • HolyCloud-VPS mit Ubuntu/Debian, sudo-Zugang
  • Remote-Repository (GitHub, GitLab, Gitea) mit Lesezugriff (Deploy-Key oder Maschinenkonto)
  • Eigener Benutzer empfohlen (deploy) — siehe „Einen sudo-Benutzer anlegen“
  • Vorhandener Web-Stack (Apache/Nginx), der auf das Zielverzeichnis zeigt

Tipp: Klonen Sie niemals als root; verwenden Sie einen Benutzer deploy, dessen Gruppe gemeinsam mit www-data im DocumentRoot lesen und schreiben darf.

Schritt 1: Git installieren

sudo apt update
sudo apt install -y git
git --version

Identität für Commits auf dem Server, falls erforderlich:

git config --global user.name "Deploy HolyCloud"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main

Schritt 2: Eigener SSH-Schlüssel für das Repository (Deploy-Key)

Als Benutzer deploy:

sudo -u deploy -i
ssh-keygen -t ed25519 -C "deploy@vps-holycloud" -f ~/.ssh/id_ed25519_git -N ""
cat ~/.ssh/id_ed25519_git.pub

Hinterlegen Sie den öffentlichen Schlüssel bei GitHub: Settings → Deploy keys (nur Lesezugriff) oder bei GitLab: Deploy Keys.

Datei ~/.ssh/config:

nano ~/.ssh/config
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_git
    IdentitiesOnly yes
chmod 600 ~/.ssh/config ~/.ssh/id_ed25519_git
ssh -T [email protected]

Schritt 3: Erster Klon für den Produktivbetrieb

sudo mkdir -p /var/www/exemple.fr
sudo chown deploy:www-data /var/www/exemple.fr
sudo chmod 775 /var/www/exemple.fr
cd /var/www/exemple.fr
git clone [email protected]:organisation/monapp.git .
git status
git branch -a

Produktiv-Branch:

git checkout main
git pull origin main

Schritt 4: Deployment-Skript

sudo nano /opt/scripts/deploy-monapp.sh
#!/bin/bash
set -euo pipefail
APP_DIR=/var/www/exemple.fr
cd "$APP_DIR"
git fetch origin
git checkout main
git pull --ff-only origin main
# PHP example: composer install --no-dev
# composer install --no-dev --optimize-autoloader
# Node example: npm ci && npm run build
sudo systemctl reload apache2
echo "Deploy OK $(date -Is)" >> /var/log/deploy-monapp.log
sudo chmod +x /opt/scripts/deploy-monapp.sh
sudo chown deploy:deploy /opt/scripts/deploy-monapp.sh

Manueller Test:

sudo -u deploy /opt/scripts/deploy-monapp.sh

Schritt 5: Bare-Repository + post-receive-Hook (optionales Push-to-Deploy)

sudo -u deploy git init --bare /home/deploy/repos/monapp.git
nano /home/deploy/repos/monapp.git/hooks/post-receive
#!/bin/bash
TARGET=/var/www/exemple.fr
GIT_DIR=/home/deploy/repos/monapp.git
git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f main
/opt/scripts/deploy-monapp.sh
chmod +x /home/deploy/repos/monapp.git/hooks/post-receive

Von Ihrem Entwicklungsrechner aus (Remote „production“):

git remote add production deploy@IP_DU_VPS:/home/deploy/repos/monapp.git
git push production main

Schritt 6: Variablen und Secrets

Committen Sie die .env der Produktivumgebung nicht. Legen Sie sie einmalig auf dem Server an:

sudo -u deploy nano /var/www/exemple.fr/.env
sudo chmod 600 /var/www/exemple.fr/.env

Liste der ignorierten Dateien:

cat .gitignore

Schritt 7: Cron oder Webhook (Überblick)

Geplanter Pull (einfach, aber weniger elegant als ein Hook):

*/30 * * * * deploy /opt/scripts/deploy-monapp.sh >> /var/log/deploy-monapp.log 2>&1

Lösen Sie bei GitHub Actions ssh deploy@IP /opt/scripts/deploy-monapp.sh mit einem eigenen Schlüssel aus — beschränken Sie den erlaubten Befehl in authorized_keys.

Überprüfung

cd /var/www/exemple.fr && git log -1 --oneline
git remote -v
ls -la
tail -5 /var/log/deploy-monapp.log
curl -I http://127.0.0.1/

Die bereitgestellte Version entspricht dem letzten erwarteten Commit auf main.

HolyCloud-Support

  • Permission denied (publickey): falscher Deploy-Key oder IdentitiesOnly
  • dubious ownership: git config --global --add safe.directory /var/www/exemple.fr
  • Konflikte beim Pull: Vermeiden Sie Commits auf dem Server; verwenden Sie --ff-only
  • HolyCloud-Support: Deploy-Benutzer, Ausgabe von git pull, Logs des post-receive-Hooks