deploy.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/bin/bash
  2. set -e
  3. # ===== 配置部分 =====
  4. APP_NAME="fwh-toolbox-server" # 服务名称
  5. APP_DIR="/var/www/fwh-toolbox/server" # 二进制运行路径
  6. APP_PATH="$APP_DIR/$APP_NAME" # 二进制安装路径
  7. SERVICE_FILE="/etc/systemd/system/$APP_NAME.service"
  8. # ===== 编译程序 =====
  9. echo "🚀 Building Go program..."
  10. GOOS=linux GOARCH=amd64 go build -o $APP_NAME ./ # 按需修改 ./ 为你的项目路径
  11. # ===== 复制程序到目标路径 =====
  12. echo "📦 Copying binary to $APP_PATH..."
  13. sudo mv $APP_NAME $APP_PATH
  14. sudo chmod +x $APP_PATH
  15. # ===== 安装 systemd 服务(如果不存在) =====
  16. if [ ! -f "$SERVICE_FILE" ]; then
  17. echo "📝 Creating systemd service: $SERVICE_FILE"
  18. sudo tee $SERVICE_FILE > /dev/null <<EOF
  19. [Unit]
  20. Description=$APP_NAME service
  21. After=network.target
  22. [Service]
  23. ExecStart=$APP_PATH
  24. Restart=always
  25. User=root
  26. WorkingDirectory=$APP_DIR
  27. CPUQuota=200%
  28. MemoryLimit=2G
  29. [Install]
  30. WantedBy=multi-user.target
  31. EOF
  32. echo "✅ Service file created."
  33. sudo systemctl daemon-reload
  34. sudo systemctl enable $APP_NAME
  35. fi
  36. # ===== 启动或重启服务 =====
  37. if systemctl is-active --quiet $APP_NAME; then
  38. echo "🔄 Restarting $APP_NAME service..."
  39. sudo systemctl restart $APP_NAME
  40. else
  41. echo "▶️ Starting $APP_NAME service..."
  42. sudo systemctl start $APP_NAME
  43. fi
  44. # ===== 状态检查 =====
  45. sudo systemctl status $APP_NAME --no-pager