deploy.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. [Install]
  28. WantedBy=multi-user.target
  29. EOF
  30. echo "✅ Service file created."
  31. sudo systemctl daemon-reload
  32. sudo systemctl enable $APP_NAME
  33. fi
  34. # ===== 启动或重启服务 =====
  35. if systemctl is-active --quiet $APP_NAME; then
  36. echo "🔄 Restarting $APP_NAME service..."
  37. sudo systemctl restart $APP_NAME
  38. else
  39. echo "▶️ Starting $APP_NAME service..."
  40. sudo systemctl start $APP_NAME
  41. fi
  42. # ===== 状态检查 =====
  43. sudo systemctl status $APP_NAME --no-pager