From cdebe2b4cefeffc312e0e51f71b68a36bc069fea Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 5 Oct 2025 14:32:08 +0200 Subject: [PATCH] Create set_new_nginx_app.sh --- set_new_nginx_app.sh | 150 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 set_new_nginx_app.sh diff --git a/set_new_nginx_app.sh b/set_new_nginx_app.sh new file mode 100644 index 0000000..c52cc2d --- /dev/null +++ b/set_new_nginx_app.sh @@ -0,0 +1,150 @@ +#!/bin/bash + +# ------------------------------ +# Script réutilisable Node.js LTS + pnpm + PM2 + Nginx + Certbot Cloudflare +# Arguments : +# $1 = Domaine (ex: exemple.com) +# $2 = Port Node.js (ex: 3000) +# $3 = Email pour SSL (ex: email@example.com) +# ------------------------------ + +if [ "$#" -ne 3 ]; then + echo "Usage: $0 " + exit 1 +fi + +DOMAIN="$1" +APP_PORT="$2" +EMAIL="$3" +CLOUDFLARE_CREDENTIALS="$HOME/.secrets/certbot/cloudflare.ini" + +echo "⚙️ Configuration pour :" +echo "Domaine: $DOMAIN" +echo "Port Node.js: $APP_PORT" +echo "Email SSL: $EMAIL" + +# ------------------------------ +# Fonction pour installer un paquet si absent +# ------------------------------ +install_if_missing() { + if ! command -v $1 &> /dev/null; then + echo "Installation de $1..." + sudo apt install -y $2 + else + echo "$1 déjà installé, skipping..." + fi +} + +# ------------------------------ +# Mise à jour système +# ------------------------------ +sudo apt update && sudo apt upgrade -y + +# Installer dépendances de base +install_if_missing curl curl +install_if_missing git git +install_if_missing build-essential build-essential +install_if_missing software-properties-common software-properties-common + +# Installer Node.js LTS si absent +if ! command -v node &> /dev/null; then + curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - + sudo apt install -y nodejs +else + echo "Node.js déjà installé, skipping..." +fi + +# Installer pnpm si absent +if ! command -v pnpm &> /dev/null; then + sudo npm install -g pnpm +else + echo "pnpm déjà installé, skipping..." +fi + +# Installer PM2 si absent +if ! command -v pm2 &> /dev/null; then + sudo npm install -g pm2 + pm2 startup systemd -u $USER --hp $HOME + pm2 save +else + echo "PM2 déjà installé, skipping..." +fi + +# Installer Nginx si absent +install_if_missing nginx nginx +sudo systemctl enable nginx +sudo systemctl start nginx + +# ------------------------------ +# Configurer Nginx pour l'app (wildcard) +# ------------------------------ +NGINX_CONF="/etc/nginx/sites-available/$DOMAIN" +sudo tee $NGINX_CONF > /dev/null < /dev/null <