71 lines
2.7 KiB
Markdown
71 lines
2.7 KiB
Markdown
# scripts
|
||
|
||
| 文件 | 用途 |
|
||
|---|---|
|
||
| `dev.sh` | 开发启动:建 venv(首次)→ `alembic upgrade head` → `uvicorn --reload` |
|
||
| `start.sh` | 生产启动:只启动 uvicorn(`--workers`),供 systemd 调用 |
|
||
| `deploy.sh` | 服务器部署:依赖 → 迁移 → 构建 Web → 重启服务 → 健康检查 |
|
||
| `docker-deploy.sh` | Docker 部署:构建镜像 → 启动 Compose → 可选初始化码表 |
|
||
| `nex-buddy.service` | systemd 模板,首次部署时改路径/运行用户 |
|
||
| `nginx-nex-buddy.conf` | Nginx 同域反代模板(静态文件 + `/api`、`/device`) |
|
||
|
||
## 开发
|
||
|
||
```bash
|
||
./scripts/dev.sh # 默认读 .env 的 BACKEND_PORT
|
||
./scripts/dev.sh 8101 # 临时指定端口
|
||
```
|
||
|
||
## 服务器部署
|
||
|
||
```bash
|
||
cp .env.example .env
|
||
# APP_ENV=prod;DATABASE_URL / REDIS_URL 改成服务器地址
|
||
|
||
./scripts/deploy.sh --init --no-restart # --init 仅在首次部署时需要
|
||
cd server && ../.venv/bin/python scripts/create_admin.py \
|
||
--account admin@nexbuddy.com --password '换成强口令'
|
||
cd ..
|
||
sudo cp scripts/nex-buddy.service /etc/systemd/system/
|
||
sudo vim /etc/systemd/system/nex-buddy.service # 改 User / WorkingDirectory
|
||
sudo chown -R www-data:www-data data # 与 service 的 User 保持一致
|
||
sudo systemctl daemon-reload && sudo systemctl enable --now nex-buddy
|
||
|
||
sudo cp scripts/nginx-nex-buddy.conf /etc/nginx/sites-available/nex-buddy
|
||
sudo vim /etc/nginx/sites-available/nex-buddy # 改 server_name / root
|
||
sudo ln -s /etc/nginx/sites-available/nex-buddy /etc/nginx/sites-enabled/
|
||
sudo nginx -t && sudo systemctl reload nginx
|
||
```
|
||
|
||
之后更新代码只需重新执行 `./scripts/deploy.sh`(检测到 systemd 服务会自动重启并检查 `/healthz`)。
|
||
|
||
## Docker
|
||
|
||
```bash
|
||
./scripts/docker-deploy.sh # 使用 .env 里的外部 PG/Redis
|
||
./scripts/docker-deploy.sh --full --init # 连 PostgreSQL/Redis 一起起,并初始化码表
|
||
```
|
||
|
||
镜像构建默认使用国内 pip/npm/apt 源;`PIP_INDEX_URL`、`NPM_REGISTRY`、`APT_MIRROR`
|
||
可在 `.env` 或命令行覆盖。详细的镜像拉取加速与挂载说明见 `docker/README.md`。
|
||
|
||
## 资源目录
|
||
|
||
媒体、固件等运行时文件统一放在项目根 `data/`,只有 `regions.json` 入库,其余内容被 `.gitignore` 忽略。
|
||
服务器或容器部署时把该目录挂到宿主机磁盘即可;也可以用 `DATA_DIR` 指向别的位置:
|
||
|
||
```bash
|
||
DATA_DIR=/srv/nex_buddy/data
|
||
```
|
||
|
||
## CORS
|
||
|
||
同域部署(Nginx 把 Web 和 API 放在同一个域名下)不需要 CORS,`.env` 里不用写 `CORS_ORIGINS`。
|
||
只有 Web 与 API 确实不同域时才配置:
|
||
|
||
```bash
|
||
CORS_ORIGINS=https://app.example.com
|
||
```
|
||
|
||
如果已经配置了 `WEB_BASE_URL`,它的源会自动加入允许列表,通常不必再写 `CORS_ORIGINS`。
|