45 lines
2.0 KiB
PowerShell
45 lines
2.0 KiB
PowerShell
# 前端单独部署脚本 - Nginx
|
|
$serverIp = "64.90.11.73"
|
|
$serverUser = "root"
|
|
$remoteDir = "/root/chat-web"
|
|
|
|
Write-Host "=============================================" -ForegroundColor Cyan
|
|
Write-Host " 开始部署前端 (Nginx) 到 $serverIp" -ForegroundColor Cyan
|
|
Write-Host "=============================================" -ForegroundColor Cyan
|
|
|
|
# 1. 编译前端
|
|
Write-Host "`n[1/3] 开始构建前端应用..." -ForegroundColor Yellow
|
|
Set-Location -Path "chat-web"
|
|
if (!(Test-Path "node_modules")) {
|
|
npm install
|
|
}
|
|
npm run build
|
|
|
|
# 2. 上传文件到服务器
|
|
Write-Host "`n[2/3] 正在创建服务器目录并上传前端产物..." -ForegroundColor Yellow
|
|
Write-Host "可能会提示输入服务器密码:" -ForegroundColor Magenta
|
|
|
|
# 创建远程目录
|
|
ssh ${serverUser}@${serverIp} "mkdir -p $remoteDir/dist"
|
|
|
|
# 上传 Nginx 配置
|
|
scp nginx.conf ${serverUser}@${serverIp}:${remoteDir}/nginx.conf
|
|
|
|
# 上传构建好的前端文件 (将 dist 下的所有文件拷贝到服务器的 dist 目录中)
|
|
scp -r dist/* ${serverUser}@${serverIp}:${remoteDir}/dist/
|
|
|
|
# 3. 部署或更新 Nginx 容器
|
|
Write-Host "`n[3/3] 正在启动/更新 Nginx 容器..." -ForegroundColor Yellow
|
|
Write-Host "可能会再次提示输入服务器密码:" -ForegroundColor Magenta
|
|
|
|
$deployCmd = "docker stop chat-web-nginx; docker rm chat-web-nginx; docker run -d --name chat-web-nginx -p 38080:80 -v ${remoteDir}/dist:/usr/share/nginx/html -v ${remoteDir}/nginx.conf:/etc/nginx/conf.d/default.conf nginx"
|
|
ssh ${serverUser}@${serverIp} $deployCmd
|
|
|
|
Set-Location -Path ".."
|
|
|
|
Write-Host "`n=============================================" -ForegroundColor Green
|
|
Write-Host " 前端 Nginx 部署/更新完成!" -ForegroundColor Green
|
|
Write-Host " 请访问浏览器:http://${serverIp}:38080" -ForegroundColor Green
|
|
Write-Host "日后仅更新前端包:只需运行此脚本即可自动打包上传并重启。" -ForegroundColor Green
|
|
Write-Host "=============================================" -ForegroundColor Green
|