56618a6655
Document Nginx SSE proxy settings for long-lived live streams. Co-authored-by: Cursor <cursoragent@cursor.com>
215 lines
4.7 KiB
Markdown
215 lines
4.7 KiB
Markdown
# crypto_okx 部署文档(Ubuntu)
|
|
|
|
独立仓库:[https://git.bz121.com/dekun/crypto_okx.git](https://git.bz121.com/dekun/crypto_okx.git)
|
|
|
|
**功能与页面操作**见同目录 **[使用说明.md](./使用说明.md)**.脚本细节见 **[deploy/README.md](./deploy/README.md)**.
|
|
|
|
---
|
|
|
|
## 一键部署(推荐)
|
|
|
|
新服务器**无需先 clone**:
|
|
|
|
```bash
|
|
curl -fsSL https://git.bz121.com/dekun/crypto_okx/raw/branch/main/deploy/manage.sh | bash
|
|
```
|
|
|
|
菜单:
|
|
|
|
1. **一键部署** — 环境识别、系统依赖、Node/PM2、venv、密钥、启动 `crypto_okx`
|
|
2. **一键卸载** — 备份 `.env` 后移走 `/opt/crypto_okx`
|
|
3. **更新** — `git pull` + 重启 / 依赖更新
|
|
4. **仅检测** — 识别环境与依赖,不安装
|
|
0. 退出
|
|
|
|
已安装后再次进入:
|
|
|
|
```bash
|
|
bash /opt/crypto_okx/deploy/manage.sh
|
|
```
|
|
|
|
默认安装路径:`/opt/crypto_okx`
|
|
默认端口:`5004`(`.env` 中 `APP_PORT`)
|
|
访问:`http://<服务器IP>:5004` → 默认进入期权页 `/options`
|
|
|
|
---
|
|
|
|
## 本地部署 + SSH SOCKS 转发 + PM2(可选)
|
|
|
|
本机直连 OKX 若被 TLS/SNI reset,可用本机 `ssh -D` 把 SOCKS 出口放到可达 OKX 的 VPS,并配置:
|
|
|
|
```env
|
|
OKX_SOCKS_PROXY=socks5h://127.0.0.1:1080
|
|
```
|
|
|
|
> 安全提醒:不要把 `.env`、私钥、OKX API Key 提交到 Git.
|
|
|
|
### 0. 准备
|
|
|
|
- Ubuntu 本机或云主机
|
|
- 能访问 OKX 的 VPS(SSH)
|
|
- Python 3.10+、`python3-venv`、`git`、`curl`、`node`/`npm`(装 PM2)
|
|
|
|
### 1. 克隆本仓库
|
|
|
|
```bash
|
|
cd /opt
|
|
git clone https://git.bz121.com/dekun/crypto_okx.git crypto_okx
|
|
cd /opt/crypto_okx
|
|
cp -n .env.example .env
|
|
```
|
|
|
|
或用一键脚本自动克隆到 `/opt/crypto_okx`.
|
|
|
|
### 2. SSH SOCKS(可选)
|
|
|
|
`~/.ssh/config` 示例:
|
|
|
|
```sshconfig
|
|
Host okx-vps
|
|
HostName YOUR_VPS_IP
|
|
User root
|
|
IdentityFile ~/.ssh/vps1.pem
|
|
IdentitiesOnly yes
|
|
ServerAliveInterval 30
|
|
ServerAliveCountMax 3
|
|
ExitOnForwardFailure yes
|
|
BatchMode yes
|
|
```
|
|
|
|
开隧道并验证:
|
|
|
|
```bash
|
|
ssh -N -D 127.0.0.1:1080 okx-vps
|
|
curl -4 -Iv --max-time 15 --proxy socks5h://127.0.0.1:1080 https://www.okx.com/api/v5/public/time
|
|
```
|
|
|
|
### 3. 环境与依赖
|
|
|
|
推荐:
|
|
|
|
```bash
|
|
cd /opt/crypto_okx
|
|
bash deploy/setup_env.sh --install-system-deps
|
|
```
|
|
|
|
手动等价:
|
|
|
|
```bash
|
|
cd /opt/crypto_okx
|
|
python3 -m venv .venv
|
|
source .venv/bin/activate
|
|
pip install -U pip
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
走 SOCKS 须已安装 **PySocks**(含在 `requirements.txt`).
|
|
|
|
### 4. 配置 `.env`
|
|
|
|
| 文件 | 是否进 Git | 说明 |
|
|
|------|------------|------|
|
|
| `.env.example` | 是 | 模板 |
|
|
| `.env` | 否 | 真实配置;`app.py` 只读此文件 |
|
|
|
|
首次:
|
|
|
|
```bash
|
|
cp -n .env.example .env
|
|
nano .env
|
|
```
|
|
|
|
至少确认:
|
|
|
|
```env
|
|
APP_HOST=0.0.0.0
|
|
APP_PORT=5004
|
|
APP_USERNAME=admin
|
|
APP_PASSWORD=你的强密码
|
|
FLASK_SECRET_KEY=长随机串
|
|
|
|
LIVE_TRADING_ENABLED=false
|
|
OKX_API_KEY=...
|
|
OKX_API_SECRET=...
|
|
OKX_API_PASSPHRASE=...
|
|
OKX_SOCKS_PROXY=socks5h://127.0.0.1:1080
|
|
```
|
|
|
|
`git pull` **不会**覆盖 `.env`.升级前可备份:`cp .env .env.backup.$(date +%Y%m%d)`.
|
|
|
|
### 5. 手工启动验证
|
|
|
|
```bash
|
|
cd /opt/crypto_okx
|
|
source .venv/bin/activate
|
|
python app.py
|
|
```
|
|
|
|
浏览器:`http://127.0.0.1:5004`
|
|
|
|
### 6. PM2 常驻
|
|
|
|
```bash
|
|
cd /opt/crypto_okx
|
|
pm2 start ecosystem.config.cjs
|
|
pm2 save
|
|
pm2 startup
|
|
```
|
|
|
|
进程名:`crypto_okx`.日志:`pm2 logs crypto_okx --lines 100`
|
|
|
|
SOCKS 隧道建议用 `tmux`/`autossh`/`ssh -D` 常驻,**不要**交给本项目的 PM2 配置(见 `ecosystem.config.cjs` 注释).
|
|
|
|
### 7. 更新 / 卸载
|
|
|
|
```bash
|
|
# 更新
|
|
bash /opt/crypto_okx/deploy/manage.sh # 选 3
|
|
# 或
|
|
bash /opt/crypto_okx/deploy/pull_and_restart.sh
|
|
|
|
# 卸载
|
|
bash /opt/crypto_okx/deploy/manage.sh # 选 2
|
|
```
|
|
|
|
### 8. 常见问题
|
|
|
|
- **页面打不开**:`pm2 status` / `pm2 logs crypto_okx --lines 50`
|
|
- **OKX 失败**:先确认 SOCKS `ss -lntp | grep 1080`,再 `curl --proxy socks5h://...`
|
|
- **脚本 `pipefail` 报错**(Windows 编辑过):`sed -i 's/\r$//' deploy/*.sh deploy/lib/*.sh`
|
|
- **几分钟后资金/行情不自动刷新**:embed 壳靠 SSE(`/api/instance/live/stream`).若前面有 Nginx,需关闭缓冲并拉长超时,例如:
|
|
|
|
```nginx
|
|
location / {
|
|
proxy_pass http://127.0.0.1:5004;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Connection "";
|
|
proxy_buffering off;
|
|
proxy_read_timeout 3600s;
|
|
proxy_send_timeout 3600s;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
```
|
|
|
|
改完后 `nginx -t && systemctl reload nginx`,浏览器强刷.前端另有断开重连与轮询兜底.
|
|
|
|
---
|
|
|
|
## 环境变量覆盖(可选)
|
|
|
|
```bash
|
|
INSTALL_ROOT=/opt/crypto_okx
|
|
GIT_URL=https://git.bz121.com/dekun/crypto_okx.git
|
|
GIT_BRANCH=main
|
|
BACKUP_ROOT=/root/backups
|
|
```
|
|
|
|
---
|
|
|
|
## 免责声明
|
|
|
|
请遵守当地法律法规与交易所条款.实盘风险自负.
|