README.md 2.7 KB

部署与上线指南

本文记录 shudao-chat 项目前后端在生产环境的标准交付流程,包括 Vue 前端打包、产物合入 Go 服务、Linux 下编译后端以及上传、运行服务的步骤。


1. 前端(Vue 3 + Vite)打包

环境准备

  • Node.js ≥ 20.19(建议与 package.json 中的 engines 保持一致)
  • npm(或 pnpm / yarn,以下以 npm 为例)
  • 在国内网络可替换源:npm config set registry https://registry.npmmirror.com

打包流程

cd /path/to/shudao-chat(前端根目录)
npm install
npm run build

执行完成后会在根目录生成 dist/,结构类似:

dist/
  index.html
  assets/
    *.js
    *.css
    ...

2. 将前端产物放入 shudao-chat-go

  1. 备份旧版本(可选):

    cd shudao-chat-go
    mv views/index.html views/index_$(date +%Y%m%d%H%M%S).html
    mv assets assets_$(date +%Y%m%d%H%M%S)
    
  2. 拷贝新的 dist 产物:

    # 回到前端根目录
    cp dist/index.html shudao-chat-go/views/
    rsync -av --delete dist/assets/ shudao-chat-go/assets/
    
    • views/index.html 会被 controllers/frontend.go 用作模版。
    • assets/ 目录被 main.go 中的 beego.SetStaticPath("/assets", "assets") 映射,前端静态资源必须放在这里。
  3. 若有 dist 中的其他静态目录(如 public 下复制出的 favicon 等),放入 shudao-chat-go/static/,Beego 已映射 /static 路径。

3. 在本地编译 Linux 版后端

环境要求

  • Go 1.24+(与 go.mod 中声明一致)
  • 已配置好 GOPROXY(如国内:https://goproxy.cn,direct

编译命令

cd shudao-chat-go
go mod download
GOOS=linux GOARCH=amd64 go build -o bin/shudao-chat-go ./main.go

或者安装bee框架:(go env -w GOPROXY=https://goproxy.cn)//如果下面命令安装不成功先执行这条命令
go install github.com/beego/bee/v2@latest
然后运行:bee pack -be GOOS=linux

输出文件:shudao-chat-go/bin/shudao-chat-go,为可在 Linux x86_64 上运行的二进制。请同时准备以下运行所需文件/目录:

  • conf/app.conf(按环境修改数据库、API、OSS 等配置)
  • assets/static/views/(包含打包后的前端)
  • 其他运行依赖(如 scripts/models/ 中需要的资源)

4. 打包并上传到服务器

上传方式(示例)

压缩包拖入/tmp/tmp

5. 服务器部署与运行

cd /tmp cd tmp tar xvf shudao-chat-go.tar.gz chmod +x shudao-chat-go 找到进程:ps -ef | grep shudao-chat-go 杀死进程:kill -9 1029395 # 将 12345 替换为你找到的实际PID nohup ./shudao-chat-go & tail -f nohup.out #查看实时状态