[READ-ONLY] Mirror of https://github.com/thoda-dev/scripts. custom scripts
0

Configure Feed

Select the types of activity you want to include in your feed.

Create set_new_nginx_app.sh

Thomas (Oct 5, 2025, 2:32 PM +0200) cdebe2b4 bb47b6e4

+150
+150
set_new_nginx_app.sh
··· 1 + #!/bin/bash 2 + 3 + # ------------------------------ 4 + # Script réutilisable Node.js LTS + pnpm + PM2 + Nginx + Certbot Cloudflare 5 + # Arguments : 6 + # $1 = Domaine (ex: exemple.com) 7 + # $2 = Port Node.js (ex: 3000) 8 + # $3 = Email pour SSL (ex: email@example.com) 9 + # ------------------------------ 10 + 11 + if [ "$#" -ne 3 ]; then 12 + echo "Usage: $0 <domain> <app_port> <email>" 13 + exit 1 14 + fi 15 + 16 + DOMAIN="$1" 17 + APP_PORT="$2" 18 + EMAIL="$3" 19 + CLOUDFLARE_CREDENTIALS="$HOME/.secrets/certbot/cloudflare.ini" 20 + 21 + echo "⚙️ Configuration pour :" 22 + echo "Domaine: $DOMAIN" 23 + echo "Port Node.js: $APP_PORT" 24 + echo "Email SSL: $EMAIL" 25 + 26 + # ------------------------------ 27 + # Fonction pour installer un paquet si absent 28 + # ------------------------------ 29 + install_if_missing() { 30 + if ! command -v $1 &> /dev/null; then 31 + echo "Installation de $1..." 32 + sudo apt install -y $2 33 + else 34 + echo "$1 déjà installé, skipping..." 35 + fi 36 + } 37 + 38 + # ------------------------------ 39 + # Mise à jour système 40 + # ------------------------------ 41 + sudo apt update && sudo apt upgrade -y 42 + 43 + # Installer dépendances de base 44 + install_if_missing curl curl 45 + install_if_missing git git 46 + install_if_missing build-essential build-essential 47 + install_if_missing software-properties-common software-properties-common 48 + 49 + # Installer Node.js LTS si absent 50 + if ! command -v node &> /dev/null; then 51 + curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - 52 + sudo apt install -y nodejs 53 + else 54 + echo "Node.js déjà installé, skipping..." 55 + fi 56 + 57 + # Installer pnpm si absent 58 + if ! command -v pnpm &> /dev/null; then 59 + sudo npm install -g pnpm 60 + else 61 + echo "pnpm déjà installé, skipping..." 62 + fi 63 + 64 + # Installer PM2 si absent 65 + if ! command -v pm2 &> /dev/null; then 66 + sudo npm install -g pm2 67 + pm2 startup systemd -u $USER --hp $HOME 68 + pm2 save 69 + else 70 + echo "PM2 déjà installé, skipping..." 71 + fi 72 + 73 + # Installer Nginx si absent 74 + install_if_missing nginx nginx 75 + sudo systemctl enable nginx 76 + sudo systemctl start nginx 77 + 78 + # ------------------------------ 79 + # Configurer Nginx pour l'app (wildcard) 80 + # ------------------------------ 81 + NGINX_CONF="/etc/nginx/sites-available/$DOMAIN" 82 + sudo tee $NGINX_CONF > /dev/null <<EOF 83 + server { 84 + listen 80; 85 + server_name $DOMAIN *.$DOMAIN; 86 + 87 + location / { 88 + proxy_pass http://127.0.0.1:$APP_PORT; 89 + proxy_http_version 1.1; 90 + proxy_set_header Upgrade \$http_upgrade; 91 + proxy_set_header Connection 'upgrade'; 92 + proxy_set_header Host \$host; 93 + proxy_cache_bypass \$http_upgrade; 94 + } 95 + } 96 + EOF 97 + 98 + sudo ln -sf $NGINX_CONF /etc/nginx/sites-enabled/ 99 + sudo nginx -t && sudo systemctl reload nginx 100 + 101 + # ------------------------------ 102 + # Installer Certbot et plugin Cloudflare 103 + # ------------------------------ 104 + install_if_missing certbot certbot 105 + install_if_missing python3-certbot-dns-cloudflare python3-certbot-dns-cloudflare 106 + 107 + # Générer certificat wildcard SSL via Cloudflare si absent 108 + if [ ! -d "/etc/letsencrypt/live/$DOMAIN" ]; then 109 + sudo certbot certonly \ 110 + --dns-cloudflare \ 111 + --dns-cloudflare-credentials $CLOUDFLARE_CREDENTIALS \ 112 + -d $DOMAIN -d *.$DOMAIN \ 113 + -m $EMAIL \ 114 + --agree-tos \ 115 + --non-interactive 116 + else 117 + echo "Certificat SSL déjà existant pour $DOMAIN, skipping..." 118 + fi 119 + 120 + # Configurer Nginx pour SSL 121 + sudo tee /etc/nginx/sites-available/$DOMAIN > /dev/null <<EOF 122 + server { 123 + listen 80; 124 + server_name $DOMAIN *.$DOMAIN; 125 + return 301 https://\$host\$request_uri; 126 + } 127 + 128 + server { 129 + listen 443 ssl; 130 + server_name $DOMAIN *.$DOMAIN; 131 + 132 + ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem; 133 + ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem; 134 + 135 + location / { 136 + proxy_pass http://127.0.0.1:$APP_PORT; 137 + proxy_http_version 1.1; 138 + proxy_set_header Upgrade \$http_upgrade; 139 + proxy_set_header Connection 'upgrade'; 140 + proxy_set_header Host \$host; 141 + proxy_cache_bypass \$http_upgrade; 142 + } 143 + } 144 + EOF 145 + 146 + sudo nginx -t && sudo systemctl reload nginx 147 + 148 + echo "✅ Installation terminée !" 149 + echo "Port Node.js: $APP_PORT" 150 + echo "Nginx configuré avec SSL wildcard pour $DOMAIN et *.$DOMAIN"