fix_and_deploy.sh 1.9 KB

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