fix_and_deploy.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. SERVER_IP="45.61.60.10"
  3. SERVER_USER="root"
  4. SERVER_PASS="UQb9TgSwC@vwhEM"
  5. /usr/bin/expect <<EOF
  6. set timeout 180
  7. spawn ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP
  8. expect {
  9. "password:" { send "$SERVER_PASS\r" }
  10. "yes/no" { send "yes\r"; exp_continue }
  11. }
  12. expect "#"
  13. # 1. Check Node binaries
  14. send "echo '--- CHECKING NODE BINARIES ---'\r"
  15. send "which node\r"
  16. send "which npm\r"
  17. send "which npx\r"
  18. # 2. Finish backend deployment
  19. send "echo '--- FINISHING BACKEND DEPLOYMENT ---'\r"
  20. send "cd /var/www/ccdw-website/server\r"
  21. send "npm install --omit=dev --registry=https://registry.npmmirror.com\r"
  22. send "npm install -g pm2 --registry=https://registry.npmmirror.com\r"
  23. # Start App
  24. send "echo '--- STARTING APP ---'\r"
  25. send "pm2 delete ccdw-server 2>/dev/null\r"
  26. send "pm2 start index.js --name ccdw-server\r"
  27. send "pm2 save\r"
  28. # 3. Create Nginx Config (HTTP)
  29. send "echo '--- CREATING NGINX CONFIG ---'\r"
  30. send "cat > /etc/nginx/sites-available/ccdw.xyz.conf <<ENDCONF
  31. server {
  32. listen 80;
  33. server_name ccdw.xyz www.ccdw.xyz;
  34. access_log /var/log/nginx/ccdw.xyz.log;
  35. error_log /var/log/nginx/ccdw.xyz.error.log;
  36. location / {
  37. proxy_pass http://127.0.0.1:3001;
  38. proxy_set_header Host \\\$host;
  39. proxy_set_header X-Real-IP \\\$remote_addr;
  40. proxy_set_header X-Forwarded-For \\\$proxy_add_x_forwarded_for;
  41. proxy_set_header X-Forwarded-Proto \\\$scheme;
  42. }
  43. }
  44. ENDCONF\r"
  45. # 4. Reload Nginx
  46. send "echo '--- RELOADING NGINX ---'\r"
  47. send "nginx -t\r"
  48. send "ln -sf /etc/nginx/sites-available/ccdw.xyz.conf /etc/nginx/sites-enabled/ccdw.xyz.conf\r"
  49. send "systemctl reload nginx\r"
  50. # 5. Check URL access
  51. send "echo '--- CHECKING ACCESS ---'\r"
  52. send "curl -I http://127.0.0.1:3001\r"
  53. send "curl -I -H 'Host: ccdw.xyz' http://127.0.0.1\r"
  54. send "exit\r"
  55. expect eof
  56. EOF