You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
7.4 KiB
Bash
272 lines
7.4 KiB
Bash
|
2 months ago
|
#!/bin/bash
|
||
|
|
|
||
|
|
# Script d'installation automatique de BricoLoc sur VPS
|
||
|
|
# Usage: bash install-vps.sh
|
||
|
|
|
||
|
|
set -e
|
||
|
|
|
||
|
|
echo "======================================"
|
||
|
|
echo "Installation de BricoLoc sur VPS"
|
||
|
|
echo "======================================"
|
||
|
|
echo ""
|
||
|
|
|
||
|
|
# Couleurs pour les messages
|
||
|
|
GREEN='\033[0;32m'
|
||
|
|
YELLOW='\033[1;33m'
|
||
|
|
RED='\033[0;31m'
|
||
|
|
NC='\033[0m' # No Color
|
||
|
|
|
||
|
|
# Fonction pour afficher les messages
|
||
|
|
print_success() {
|
||
|
|
echo -e "${GREEN}✓ $1${NC}"
|
||
|
|
}
|
||
|
|
|
||
|
|
print_warning() {
|
||
|
|
echo -e "${YELLOW}⚠ $1${NC}"
|
||
|
|
}
|
||
|
|
|
||
|
|
print_error() {
|
||
|
|
echo -e "${RED}✗ $1${NC}"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Vérifier si on est root
|
||
|
|
if [ "$EUID" -eq 0 ]; then
|
||
|
|
print_error "Ne pas exécuter ce script en tant que root"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 1. Mise à jour du système
|
||
|
|
echo "1. Mise à jour du système..."
|
||
|
|
sudo apt update && sudo apt upgrade -y
|
||
|
|
print_success "Système mis à jour"
|
||
|
|
|
||
|
|
# 2. Installation de Node.js
|
||
|
|
echo ""
|
||
|
|
echo "2. Installation de Node.js 20.x..."
|
||
|
|
if ! command -v node &> /dev/null; then
|
||
|
|
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
|
||
|
|
sudo apt install -y nodejs
|
||
|
|
print_success "Node.js installé: $(node --version)"
|
||
|
|
else
|
||
|
|
print_success "Node.js déjà installé: $(node --version)"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 3. Installation de Git
|
||
|
|
echo ""
|
||
|
|
echo "3. Installation de Git..."
|
||
|
|
if ! command -v git &> /dev/null; then
|
||
|
|
sudo apt install -y git
|
||
|
|
print_success "Git installé: $(git --version)"
|
||
|
|
else
|
||
|
|
print_success "Git déjà installé: $(git --version)"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 4. Installation de PM2
|
||
|
|
echo ""
|
||
|
|
echo "4. Installation de PM2..."
|
||
|
|
if ! command -v pm2 &> /dev/null; then
|
||
|
|
sudo npm install -g pm2
|
||
|
|
print_success "PM2 installé: $(pm2 --version)"
|
||
|
|
else
|
||
|
|
print_success "PM2 déjà installé: $(pm2 --version)"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 5. Installation de Nginx
|
||
|
|
echo ""
|
||
|
|
echo "5. Installation de Nginx..."
|
||
|
|
if ! command -v nginx &> /dev/null; then
|
||
|
|
sudo apt install -y nginx
|
||
|
|
print_success "Nginx installé: $(nginx -v 2>&1)"
|
||
|
|
else
|
||
|
|
print_success "Nginx déjà installé: $(nginx -v 2>&1)"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 6. Configuration du pare-feu
|
||
|
|
echo ""
|
||
|
|
echo "6. Configuration du pare-feu..."
|
||
|
|
sudo ufw allow 22/tcp
|
||
|
|
sudo ufw allow 80/tcp
|
||
|
|
sudo ufw allow 443/tcp
|
||
|
|
sudo ufw --force enable
|
||
|
|
print_success "Pare-feu configuré"
|
||
|
|
|
||
|
|
# 7. Clonage du projet
|
||
|
|
echo ""
|
||
|
|
echo "7. Clonage du projet..."
|
||
|
|
mkdir -p ~/apps
|
||
|
|
cd ~/apps
|
||
|
|
|
||
|
|
if [ -d "bricolociaac" ]; then
|
||
|
|
print_warning "Le dossier bricolociaac existe déjà"
|
||
|
|
read -p "Voulez-vous le supprimer et le re-cloner ? (y/n) " -n 1 -r
|
||
|
|
echo
|
||
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||
|
|
rm -rf bricolociaac
|
||
|
|
git clone https://github.com/morepudding/bricolociaac.git
|
||
|
|
print_success "Projet cloné depuis GitHub"
|
||
|
|
else
|
||
|
|
cd bricolociaac
|
||
|
|
git pull origin main
|
||
|
|
print_success "Projet mis à jour"
|
||
|
|
fi
|
||
|
|
else
|
||
|
|
git clone https://github.com/morepudding/bricolociaac.git
|
||
|
|
print_success "Projet cloné depuis GitHub"
|
||
|
|
fi
|
||
|
|
read -p "Voulez-vous le supprimer et réinstaller ? (y/N) " -n 1 -r
|
||
|
|
echo
|
||
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||
|
|
rm -rf bricolociaac
|
||
|
|
git clone https://github.com/morepudding/bricolociaac.git
|
||
|
|
print_success "Projet cloné"
|
||
|
|
else
|
||
|
|
print_warning "Installation annulée"
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
else
|
||
|
|
git clone https://github.com/morepudding/bricolociaac.git
|
||
|
|
print_success "Projet cloné"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 8. Installation des dépendances
|
||
|
|
echo ""
|
||
|
|
echo "8. Installation des dépendances..."
|
||
|
|
cd bricolociaac/apps/legacy-app
|
||
|
|
npm install
|
||
|
|
print_success "Dépendances installées"
|
||
|
|
|
||
|
|
# 9. Initialisation de la base de données
|
||
|
|
echo ""
|
||
|
|
echo "9. Initialisation de la base de données..."
|
||
|
|
npm run db:reset
|
||
|
|
print_success "Base de données initialisée"
|
||
|
|
|
||
|
|
# 10. Création des dossiers nécessaires
|
||
|
|
echo ""
|
||
|
|
echo "10. Création des dossiers nécessaires..."
|
||
|
|
mkdir -p src/uploads
|
||
|
|
mkdir -p logs
|
||
|
|
mkdir -p ~/backups/bricoloc
|
||
|
|
print_success "Dossiers créés"
|
||
|
|
|
||
|
|
# 11. Configuration du fichier .env
|
||
|
|
echo ""
|
||
|
|
echo "11. Configuration des variables d'environnement..."
|
||
|
|
if [ ! -f .env ]; then
|
||
|
|
# Générer un secret aléatoire
|
||
|
|
SECRET=$(openssl rand -base64 32)
|
||
|
|
cat > .env << EOF
|
||
|
|
NODE_ENV=production
|
||
|
|
PORT=3000
|
||
|
|
SESSION_SECRET=${SECRET}
|
||
|
|
EOF
|
||
|
|
print_success "Fichier .env créé"
|
||
|
|
else
|
||
|
|
print_warning "Fichier .env existe déjà"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 12. Démarrage avec PM2
|
||
|
|
echo ""
|
||
|
|
echo "12. Démarrage de l'application..."
|
||
|
|
cd ~/apps/bricolociaac
|
||
|
|
pm2 start ecosystem.config.js
|
||
|
|
pm2 save
|
||
|
|
print_success "Application démarrée avec PM2"
|
||
|
|
|
||
|
|
# 13. Configuration du démarrage automatique
|
||
|
|
echo ""
|
||
|
|
echo "13. Configuration du démarrage automatique..."
|
||
|
|
pm2 startup | grep -v PM2 | bash
|
||
|
|
print_success "Démarrage automatique configuré"
|
||
|
|
|
||
|
|
# 14. Configuration de Nginx
|
||
|
|
echo ""
|
||
|
|
echo "14. Configuration de Nginx..."
|
||
|
|
USER=$(whoami)
|
||
|
|
|
||
|
|
# Demander le nom de domaine
|
||
|
|
read -p "Entrez votre nom de domaine (ex: defder.fr) ou appuyez sur Entrée pour utiliser l'IP: " DOMAIN
|
||
|
|
if [ -z "$DOMAIN" ]; then
|
||
|
|
DOMAIN="_"
|
||
|
|
print_warning "Utilisation de l'IP du serveur"
|
||
|
|
else
|
||
|
|
print_success "Configuration pour le domaine: $DOMAIN"
|
||
|
|
fi
|
||
|
|
|
||
|
|
sudo tee /etc/nginx/sites-available/bricoloc > /dev/null << EOF
|
||
|
|
server {
|
||
|
|
listen 80;
|
||
|
|
server_name ${DOMAIN} www.${DOMAIN};
|
||
|
|
|
||
|
|
access_log /var/log/nginx/bricoloc-access.log;
|
||
|
|
error_log /var/log/nginx/bricoloc-error.log;
|
||
|
|
|
||
|
|
client_max_body_size 10M;
|
||
|
|
|
||
|
|
location / {
|
||
|
|
proxy_pass http://localhost:3000;
|
||
|
|
proxy_http_version 1.1;
|
||
|
|
proxy_set_header Upgrade \$http_upgrade;
|
||
|
|
proxy_set_header Connection 'upgrade';
|
||
|
|
proxy_set_header Host \$host;
|
||
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
||
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
||
|
|
proxy_cache_bypass \$http_upgrade;
|
||
|
|
}
|
||
|
|
|
||
|
|
location /css/ {
|
||
|
|
alias /home/${USER}/apps/bricolociaac/apps/legacy-app/src/public/css/;
|
||
|
|
}
|
||
|
|
|
||
|
|
location /js/ {
|
||
|
|
alias /home/${USER}/apps/bricolociaac/apps/legacy-app/src/public/js/;
|
||
|
|
}
|
||
|
|
|
||
|
|
location /uploads/ {
|
||
|
|
alias /home/${USER}/apps/bricolociaac/apps/legacy-app/src/uploads/;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
EOF
|
||
|
|
|
||
|
|
# Activer la configuration
|
||
|
|
sudo ln -sf /etc/nginx/sites-available/bricoloc /etc/nginx/sites-enabled/
|
||
|
|
sudo rm -f /etc/nginx/sites-enabled/default
|
||
|
|
|
||
|
|
# Tester et redémarrer Nginx
|
||
|
|
sudo nginx -t
|
||
|
|
sudo systemctl restart nginx
|
||
|
|
print_success "Nginx configuré et redémarré"
|
||
|
|
|
||
|
|
# 15. Affichage des informations finales
|
||
|
|
echo ""
|
||
|
|
echo "======================================"
|
||
|
|
echo "Installation terminée avec succès !"
|
||
|
|
echo "======================================"
|
||
|
|
echo ""
|
||
|
|
print_success "Application accessible sur:"
|
||
|
|
if [ "$DOMAIN" != "_" ]; then
|
||
|
|
echo " → http://${DOMAIN}"
|
||
|
|
echo " → http://www.${DOMAIN}"
|
||
|
|
echo ""
|
||
|
|
print_warning "Pour activer HTTPS (recommandé):"
|
||
|
|
echo " sudo apt install -y certbot python3-certbot-nginx"
|
||
|
|
echo " sudo certbot --nginx -d ${DOMAIN} -d www.${DOMAIN}"
|
||
|
|
else
|
||
|
|
echo " → http://$(curl -s ifconfig.me)"
|
||
|
|
echo " → http://localhost (depuis le VPS)"
|
||
|
|
fi
|
||
|
|
echo ""
|
||
|
|
print_success "Commandes utiles:"
|
||
|
|
echo " → pm2 status # Voir le statut de l'application"
|
||
|
|
echo " → pm2 logs bricoloc-legacy # Voir les logs"
|
||
|
|
echo " → pm2 restart bricoloc-legacy # Redémarrer l'application"
|
||
|
|
echo " → pm2 monit # Monitorer les ressources"
|
||
|
|
echo " → cd ~/apps/bricolociaac && git pull origin main # Mettre à jour"
|
||
|
|
echo ""
|
||
|
|
print_warning "IMPORTANT: Cette application contient des vulnérabilités intentionnelles"
|
||
|
|
print_warning "Ne l'exposez pas sur Internet sans corriger les bugs de sécurité"
|
||
|
|
echo ""
|
||
|
|
print_success "Documentation complète: ~/apps/bricolociaac/DEPLOYMENT_VPS.md"
|
||
|
|
echo ""
|