# 3. 服務安裝紀錄

### 3.1 安裝前置準備

**問題：安裝腳本需要 root 權限**
```bash
# 錯誤方式（process substitution + sudo 不相容）
sudo bash <(curl -sL kejilion.sh) app OpenClaw

# 正確方式
curl -sL kejilion.sh -o /tmp/kejilion.sh && sudo bash /tmp/kejilion.sh app OpenClaw
```

**問題：Webmin apt repo GPG key 失效導致 apt update 失敗**
```bash
sudo rm -f /etc/apt/sources.list.d/webmin.list
sudo apt-get update
# 然後手動裝 Node.js 22
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
```

### 3.2 CLIProxyAPI

安裝來源：`sudo bash /tmp/kejilion.sh app CLIProxyAPI`

安裝後設定：
- WebUI：`http://192.168.31.169:8317/management.html`
- Docker restart policy：`unless-stopped`

### 3.3 OpenClaw

安裝來源：`curl -fsSL https://openclaw.ai/install.sh | bash`

**啟動問題（腳本預期 tmux，後改為 systemd）：**

腳本用 `pgrep -f "openclaw gateway"` 判斷是否運行，原本用 pm2 啟動會比對失敗。最終改用 systemd：

```ini
# /etc/systemd/system/openclaw.service
[Unit]
Description=OpenClaw Gateway
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=root
Environment=NODE_OPTIONS=--dns-result-order=ipv4first
ExecStartPre=/bin/sh -c 'tmux kill-session -t gateway 2>/dev/null; true'
ExecStart=/usr/bin/openclaw gateway
ExecStop=/usr/bin/openclaw gateway stop
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target
```

```bash
sudo systemctl daemon-reload
sudo systemctl enable openclaw.service
sudo systemctl start openclaw.service
```

### 3.4 nginx

安裝來源：`/home/web/docker-compose.yml`（已有 Docker Compose 管理）

**問題：系統 nginx 佔用 port 80**
```bash
sudo systemctl stop nginx
sudo systemctl disable nginx
sudo docker restart nginx
```

### 3.5 設備配對（Browser → OpenClaw）

```bash
# 查看待審核配對請求
sudo openclaw devices list

# 核准配對
sudo openclaw devices approve <request-id>
```
