golang-dy-back/deploy/README_DEPLOY.md
2025-10-28 16:59:41 +08:00

62 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# go-dy 部署说明
本文档介绍如何在 Linux 与 Windows 服务器上部署。
## 1. 准备
- 数据库:确保 `DB_DSN` 可用(例如:`user:pass@tcp(host:3306)/db?parseTime=true&charset=utf8mb4`)。
- 端口:默认 `PORT=8080`,可改为需要的端口。
- JWT 密钥:设置 `JWT_SECRET` 为强随机字符串。
- 验证码:
- 登录验证码由 `LOGIN_REQUIRE_CAPTCHA` 控制(默认 true
- 注册验证码由 `REGISTER_REQUIRE_CAPTCHA` 控制(默认 true
## 2. 文件构成
- 二进制:`go-dy-linux-amd64` 或 `go-dy-linux-arm64``go-dy-windows-amd64.exe`
- 配置:`.env`(可参考 `.env.example`
## 3. Linuxsystemd
1. 新建工作目录(例如 `/opt/go-dy`),上传二进制与 `.env`
2. 赋予执行权限:`chmod +x /opt/go-dy/go-dy-linux-amd64`
3. 创建 systemd 单元:`/etc/systemd/system/go-dy.service`
参考模板见 `deploy/systemd/go-dy.service`,根据架构与路径调整:
- `ExecStart` 指向二进制
- `WorkingDirectory` 指向 `.env` 所在目录
4. 加载与启动:
- `sudo systemctl daemon-reload`
- `sudo systemctl enable go-dy`
- `sudo systemctl start go-dy`
- `sudo systemctl status go-dy`
5. 查看日志:`journalctl -u go-dy -f`
## 4. Windows服务/常驻)
- 方式 A使用 NSSM 注册为服务(推荐)。
1. 安装 NSSM。
2. `nssm install go-dy C:\path\to\go-dy-windows-amd64.exe`
3. 设置 `Startup directory` 指向 `.env` 所在目录。
4. 启动服务:`nssm start go-dy`
- 方式 B任务计划程序或 PowerShell 常驻:
- PowerShell`Start-Process -NoNewWindow -WorkingDirectory C:\path\to -FilePath C:\path\to\go-dy-windows-amd64.exe`
## 5. 环境变量
将以下键放入 `.env` 或服务器环境中:
```
PORT=18080
JWT_SECRET=请替换为强随机密钥
DB_DSN=user:pass@tcp(host:3306)/db?parseTime=true&charset=utf8mb4
ALLOW_REGISTRATION=true
LOGIN_REQUIRE_CAPTCHA=false
REGISTER_REQUIRE_CAPTCHA=false
```
## 6. 健康与验证
- 启动后日志应包含:数据库连接成功与监听端口。
- 登录:`POST /api/login`(表单 `name`, `password`)。
- 注册:`POST /api/register`(字段随验证码开关而异)。
## 7. 迁移与数据
- 用户表需包含 `password_hash`bcrypt
- 可使用 `tools/update_password.go` 脚本按需写入 bcrypt 密码。