# 简单部署脚本 - MQTT Vue Dashboard到树莓派服务器 Write-Host "开始部署MQTT项目到服务器 192.168.1.17" -ForegroundColor Cyan # 1. 检查服务器连接 Write-Host "1. 检查服务器连接..." -ForegroundColor Yellow ping 192.168.1.17 -n 2 | Out-Null if ($LASTEXITCODE -eq 0) { Write-Host " 服务器连接正常" -ForegroundColor Green } else { Write-Host " 服务器连接失败" -ForegroundColor Red exit 1 } # 2. 在服务器上创建目录结构 Write-Host "2. 创建服务器目录结构..." -ForegroundColor Yellow try { $result = ssh yangfei@192.168.1.17 "mkdir -p /home/yangfei/mqtt-vue-dashboard/{backend,frontend,logs}" Write-Host " 目录创建完成" -ForegroundColor Green } catch { Write-Host " 目录创建失败: $_" -ForegroundColor Red exit 1 } # 3. 构建前端项目 Write-Host "3. 构建前端项目..." -ForegroundColor Yellow Set-Location "mqtt-vue-dashboard" if (Test-Path "node_modules") { Write-Host " 前端依赖已存在,跳过安装" -ForegroundColor Green } else { Write-Host " 安装前端依赖..." -ForegroundColor Yellow npm install if ($LASTEXITCODE -ne 0) { Write-Host " 前端依赖安装失败" -ForegroundColor Red exit 1 } } Write-Host " 构建前端..." -ForegroundColor Yellow npm run build if ($LASTEXITCODE -ne 0) { Write-Host " 前端构建失败" -ForegroundColor Red exit 1 } # 4. 上传前端文件到服务器 Write-Host "4. 上传前端文件到服务器..." -ForegroundColor Yellow if (Test-Path "dist") { scp -r dist yangfei@192.168.1.17:/home/yangfei/mqtt-vue-dashboard/frontend/ if ($LASTEXITCODE -eq 0) { Write-Host " 前端文件上传成功" -ForegroundColor Green } else { Write-Host " 前端文件上传失败" -ForegroundColor Red exit 1 } } else { Write-Host " 前端构建目录不存在" -ForegroundColor Red exit 1 } # 5. 构建后端项目 Write-Host "5. 构建后端项目..." -ForegroundColor Yellow Set-Location "server" if (Test-Path "node_modules") { Write-Host " 后端依赖已存在,跳过安装" -ForegroundColor Green } else { Write-Host " 安装后端依赖..." -ForegroundColor Yellow npm install if ($LASTEXITCODE -ne 0) { Write-Host " 后端依赖安装失败" -ForegroundColor Red exit 1 } } Write-Host " 构建后端..." -ForegroundColor Yellow npm run build if ($LASTEXITCODE -ne 0) { Write-Host " 后端构建失败" -ForegroundColor Red exit 1 } # 6. 上传后端文件到服务器 Write-Host "6. 上传后端文件到服务器..." -ForegroundColor Yellow $backendFiles = @("dist", "package.json", "package-lock.json", ".env", ".env.production") foreach ($file in $backendFiles) { if (Test-Path $file) { scp -r $file yangfei@192.168.1.17:/home/yangfei/mqtt-vue-dashboard/backend/ if ($LASTEXITCODE -eq 0) { Write-Host " 文件 $file 上传成功" -ForegroundColor Green } else { Write-Host " 文件 $file 上传失败" -ForegroundColor Red } } } # 7. 在服务器上设置项目 Write-Host "7. 在服务器上设置项目..." -ForegroundColor Yellow $setupCommands = @( "cd /home/yangfei/mqtt-vue-dashboard/backend", "npm install --production", "cd /home/yangfei/mqtt-vue-dashboard", "echo '#!/bin/bash' > start.sh", "echo 'cd /home/yangfei/mqtt-vue-dashboard/backend' >> start.sh", "echo 'npm start' >> start.sh", "chmod +x start.sh" ) foreach ($cmd in $setupCommands) { try { ssh yangfei@192.168.1.17 $cmd Write-Host " 命令执行成功: $cmd" -ForegroundColor Green } catch { Write-Host " 命令执行失败: $cmd" -ForegroundColor Red } } # 8. 启动服务 Write-Host "8. 启动后端服务..." -ForegroundColor Yellow try { # 先停止可能正在运行的服务 ssh yangfei@192.168.1.17 "pkill -f 'node.*backend' || true" # 启动新服务 ssh yangfei@192.168.1.17 "cd /home/yangfei/mqtt-vue-dashboard/backend && nohup npm start > ../logs/backend.log 2>&1 &" # 等待服务启动 Start-Sleep -Seconds 5 # 检查服务状态 $healthCheck = ssh yangfei@192.168.1.17 "curl -s http://localhost:3002/api/health || echo 'not running'" if ($healthCheck -ne "not running") { Write-Host " 后端服务启动成功" -ForegroundColor Green } else { Write-Host " 后端服务启动失败" -ForegroundColor Red } } catch { Write-Host " 服务启动失败: $_" -ForegroundColor Red } # 9. 验证部署 Write-Host "9. 验证部署结果..." -ForegroundColor Yellow try { # 检查前端文件 $frontendCheck = ssh yangfei@192.168.1.17 "ls -la /home/yangfei/mqtt-vue-dashboard/frontend/dist/" Write-Host " 前端文件验证成功" -ForegroundColor Green # 检查后端服务 $backendCheck = ssh yangfei@192.168.1.17 "ps aux | grep 'node.*backend' | grep -v grep" if ($backendCheck) { Write-Host " 后端服务运行正常" -ForegroundColor Green } else { Write-Host " 后端服务未运行" -ForegroundColor Red } } catch { Write-Host " 验证失败: $_" -ForegroundColor Red } Write-Host "" -ForegroundColor Cyan Write-Host "部署完成!" -ForegroundColor Green Write-Host "" -ForegroundColor Cyan Write-Host "访问信息:" -ForegroundColor Yellow Write-Host " 后端API: http://192.168.1.17:3002" -ForegroundColor White Write-Host " 前端应用: http://192.168.1.17" -ForegroundColor White Write-Host "" -ForegroundColor Cyan Write-Host "查看日志:" -ForegroundColor Yellow Write-Host " ssh yangfei@192.168.1.17 'tail -f /home/yangfei/mqtt-vue-dashboard/logs/backend.log'" -ForegroundColor White Write-Host "" -ForegroundColor Cyan