← 返回首页

OpenCode Web 版部署指南

AVX-only CPU + qemu 模拟 + Bun 运行时 · 赛博风格技术文档

qemu-x86_64 Bun Runtime AVX-only CPU Port 8090

🎯 适用场景

当服务器 CPU 不支持 AVX2(如 Intel Xeon E5-2650v2),无法直接运行 Bun 二进制时,需通过 qemu-x86_64-static 模拟运行 Bun,部署 OpenCode Web 服务。

❌ AVX2 E5-2650v2 等
✅ AVX qemu 可模拟

📥 第一步:克隆源码

# 浅克隆,避免仓库过大超时
git clone --depth 1 https://用户名:私人令牌@gitee.com/xusuai/opencode-cn.git /opt/opencode-cn
⚠️ 私人令牌格式:必须使用 用户名:令牌 格式,直接用令牌会 403。
⚠️ 浅克隆:仓库较大,必须 --depth 1,否则 clone 会超时。

📦 第二步:安装依赖

cd /opt/opencode-cn
qemu-x86_64-static /opt/bun/bun-linux-x64/bun install --ignore-scripts
⚠️ 必须加 --ignore-scripts
Bun install 会运行 tree-sitter-*/protobufjs 的 native postinstall 脚本,这些脚本在 qemu 下触发 SIGILL(非法指令)崩溃。
对策:跳过 postinstall,--ignore-scripts 是唯一解。

🚀 第三步:创建 Launch Wrapper

文件:/usr/local/bin/opencode-cn-launch.sh

#!/bin/bash
export HOME=/root
export OPENCODE_DISABLE_AUTOUPDATE=true
export OPENCODE_DISABLE_LSP_DOWNLOAD=true
export PATH="/opt/bun/bun-linux-x64:$PATH"
cd /opt/opencode-cn/packages/opencode
exec /usr/bin/qemu-x86_64-static /opt/bun/bun-linux-x64/bun run ./src/index.ts serve --port 8090 --hostname 0.0.0.0
chmod +x /usr/local/bin/opencode-cn-launch.sh

⚙️ 第四步:创建 systemd 服务

文件:/etc/systemd/system/opencode-cn.service

[Unit]
Description=OpenCode-CN Source Dev Mode (qemu + bun serve on port 8090)
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/opencode-cn-launch.sh
Restart=on-failure
RestartSec=5
Environment=HOME=/root
Environment=OPENCODE_DISABLE_AUTOUPDATE=true
Environment=OPENCODE_DISABLE_LSP_DOWNLOAD=true
WorkingDirectory=/opt/opencode-cn/packages/opencode
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/opt/opencode-cn /root /tmp
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload && systemctl enable --now opencode-cn.service

第五步:等待启动

# qemu 模拟下启动极慢,需循环等待
for i in $(seq 1 12); do
  ss -tlnp | grep -q ':8090 ' && echo "READY after ${i}0s" && break
  sleep 10
done
# 验证服务(curl 必须 -m 30 延长超时)
curl -m 30 -s -o /dev/null -w "%{http_code}" http://127.0.0.1:8090/
⚠️ 启动耗时 60-120 秒:qemu 模拟下初始化非常慢,curl 默认 10s 超时不够,必须用 -m 30

🔄 第六步:创建健康检查 Cron

使用 Hermes Cron Job 工具,每分钟检查:

  1. systemctl is-active opencode-cn.service — 服务是否运行
  2. curl -m 30 返回 200 — 端口是否响应
  3. 失败则自动 systemctl restart opencode-cn.service

第七步:验证

systemctl is-active opencode-cn.service   # 期望: active
systemctl is-enabled opencode-cn.service  # 期望: enabled
ss -tlnp | grep 8090                      # 期望: LISTEN
curl -m 30 -s http://127.0.0.1:8090/      # 期望: HTML 内容

🪤 六大陷阱(必须避开)

1
git clone 超时 — 仓库大 → --depth 1 浅克隆
2
私人令牌 403 — 格式错误 → 必须 用户名:令牌
3
bun install SIGILL — native postinstall 崩溃 → --ignore-scripts
4
启动 60-120s — qemu 模拟慢 → 循环等待 + Cron 兜底
5
curl 超时 10s — Web UI 初始化慢 → -m 30 延长
6
服务掉线无感知 — qemu 不稳定 → Cron 每分钟检查

🔧 环境变量速查

OPENCODE_DISABLE_AUTOUPDATE
true
禁用自动更新
OPENCODE_DISABLE_LSP_DOWNLOAD
true
HOME
/root
Bun 需要 HOME

🔌 端口规划

CmdCode (ttyd)
8080
OpenCode-CN (serve)
8090

🌐 网络优化策略

Linux/Darwin (~35-50MB) ~5MB/s 前台 terminal, timeout=180
Windows (~50MB) 100KB/s~5MB/s 后台 terminal, timeout=600
小文件 (<5MB) 前台即可