Git et déploiement sur VPS
Git permet de versionner votre code et de le déployer sur un VPS Linux HolyCloud par git pull, hook post-receive ou script CI. Ce tutoriel installe Git, configure l'authentification SSH vers GitHub/GitLab, prépare un répertoire de production et décrit un flux de déploiement minimal sans sur-ingénierie.
Prérequis
- VPS HolyCloud Ubuntu/Debian, accès sudo
- Dépôt distant (GitHub, GitLab, Gitea) avec accès en lecture (deploy key ou compte machine)
- Utilisateur dédié recommandé (
deploy) — voir « Créer un utilisateur sudo » - Stack web déjà en place (Apache/Nginx) pointant vers le répertoire cible
Conseil : ne clonez jamais en root ; utilisez un utilisateur
deploydont le groupe peut lire/écrire le DocumentRoot avecwww-data.
Étape 1 : installer Git
sudo apt update
sudo apt install -y git
git --version
Configuration identité (commits sur le serveur si besoin) :
git config --global user.name "Deploy HolyCloud"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
Étape 2 : clé SSH dédiée au dépôt (deploy key)
En tant qu'utilisateur 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
Ajoutez la clé publique sur GitHub : Settings → Deploy keys (lecture seule) ou GitLab : Deploy Keys.
Fichier ~/.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]
Étape 3 : premier clone en production
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
Branche de production :
git checkout main
git pull origin main
Étape 4 : script de déploiement
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
# Exemple PHP : composer install --no-dev
# composer install --no-dev --optimize-autoloader
# Exemple Node : 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
Test manuel :
sudo -u deploy /opt/scripts/deploy-monapp.sh
Étape 5 : dépôt bare + hook post-receive (option 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
Depuis votre poste de dev (remote « production ») :
git remote add production deploy@IP_DU_VPS:/home/deploy/repos/monapp.git
git push production main
Étape 6 : variables et secrets
Ne commitez pas .env en production. Créez-le une fois sur le serveur :
sudo -u deploy nano /var/www/exemple.fr/.env
sudo chmod 600 /var/www/exemple.fr/.env
Liste des fichiers ignorés :
cat .gitignore
Étape 7 : cron ou webhook (aperçu)
Pull planifié (simple, moins élégant que le hook) :
*/30 * * * * deploy /opt/scripts/deploy-monapp.sh >> /var/log/deploy-monapp.log 2>&1
Pour GitHub Actions, déclenchez ssh deploy@IP /opt/scripts/deploy-monapp.sh avec une clé dédiée — limitez la commande dans authorized_keys.
Vérification
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/
La version déployée correspond au dernier commit attendu sur main.
Besoin d'aide HolyCloud
- Permission denied (publickey) : mauvaise deploy key ou
IdentitiesOnly - dubious ownership :
git config --global --add safe.directory /var/www/exemple.fr - Conflits au pull : évitez les commits sur le serveur ; utilisez
--ff-only - Support HolyCloud : utilisateur deploy, sortie
git pull, logs hookpost-receive