- client 模式抓取水星 MW305R DHCP 列表,记录上下线,本地看板 - server 模式收上报,按站点分组持久化,多站点看板,7天滚动 - ESP8266 低功耗固件抓原始 HTML 上报 /api/raw,服务器端解析 - 部署脚本+systemd 单元+文档,交叉编译 Linux 二进制
114 lines
3.2 KiB
Markdown
114 lines
3.2 KiB
Markdown
# VPS 部署 dhcp-mon server(收集 + 看板)
|
|
|
|
server 模式:公网监听 HTTP 端口,接收采集端(树莓派 client / ESP8266 raw)上报,按站点分组存储,提供多站点看板。
|
|
|
|
## 一、本机交叉编译(已产出)
|
|
|
|
```bash
|
|
# 在项目根目录
|
|
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o dhcp-mon-linux-amd64 .
|
|
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o dhcp-mon-linux-arm64 .
|
|
```
|
|
|
|
产物:
|
|
- `dhcp-mon-linux-amd64` —— x86_64 静态编译,绝大多数 VPS 用这个
|
|
- `dhcp-mon-linux-arm64` —— ARM64 静态编译(如 AWS Graviton / 树莓派 4)
|
|
|
|
> 静态编译(`CGO_ENABLED=0`),无需服务器装 Go,直接拷上去就能跑。
|
|
|
|
## 二、拷贝到 VPS
|
|
|
|
```bash
|
|
scp dhcp-mon-linux-amd64 deploy/dhcp-mon-server.service deploy/install-server.sh \
|
|
user@<VPS_IP>:/tmp/
|
|
```
|
|
|
|
## 三、服务器上执行部署
|
|
|
|
```bash
|
|
sudo bash /tmp/install-server.sh /tmp/dhcp-mon-linux-amd64 你的令牌 8080
|
|
```
|
|
|
|
脚本会:
|
|
1. 把二进制装到 `/opt/dhcp-mon/dhcp-mon`
|
|
2. 创建专用用户 `dhcpmon` + 数据目录 `/srv/dhcp-data`
|
|
3. 写配置 `/etc/dhcp-mon-server.env`(仅 root 可读,含令牌)
|
|
4. 安装并启动 systemd 服务,开机自启
|
|
|
|
部署完成会打印:
|
|
- **看板地址**:`http://<服务器IP>:8080/?token=你的令牌`
|
|
- **令牌**(上报与看板共用)
|
|
- 查看状态/日志的命令
|
|
|
|
### systemd 单元(/etc/systemd/system/dhcp-mon-server.service)
|
|
|
|
```ini
|
|
[Service]
|
|
User=dhcpmon
|
|
EnvironmentFile=/etc/dhcp-mon-server.env
|
|
ExecStart=/opt/dhcp-mon/dhcp-mon -mode server -web $WEB_ADDR -data $DATA_DIR -stoken $STOKEN -retain $RETAIN_DAYS
|
|
Restart=always
|
|
RestartSec=5
|
|
```
|
|
|
|
`/etc/dhcp-mon-server.env`:
|
|
```
|
|
WEB_ADDR=:8080
|
|
DATA_DIR=/srv/dhcp-data
|
|
STOKEN=你的令牌
|
|
RETAIN_DAYS=7
|
|
```
|
|
|
|
## 四、验证
|
|
|
|
```bash
|
|
systemctl status dhcp-mon-server # 应显示 active (running)
|
|
curl -s 'http://127.0.0.1:8080/?token=你的令牌' | head # 看到看板 HTML
|
|
curl -s 'http://127.0.0.1:8080/api/sites?token=你的令牌' # 站点列表(初始 {sites:[],...})
|
|
```
|
|
|
|
## 五、放行端口
|
|
|
|
```bash
|
|
# firewalld
|
|
sudo firewall-cmd --add-port=8080/tcp --permanent && sudo firewall-cmd --reload
|
|
# ufw
|
|
sudo ufw allow 8080/tcp
|
|
```
|
|
|
|
若走公网/有域名,建议在 Nginx/Caddy 反代并上 HTTPS(见下文)。
|
|
|
|
## 六、采集端配置(ESP8266 secrets.h)
|
|
|
|
`firmware/esp8266/src/secrets.h`:
|
|
```
|
|
SERVER_URL = "http://<服务器IP>:8080/api/raw"
|
|
X_TOKEN = 与 STOKEN 一致
|
|
X_SITE = 站点名(如 home)
|
|
```
|
|
|
|
树莓派 client 模式上报对应 `-report http://<IP>:8080/api/report -token STOKEN -site home`。
|
|
|
|
## 七、(可选)Nginx 反代 + HTTPS
|
|
|
|
```nginx
|
|
server {
|
|
listen 80;
|
|
server_name dhcp.example.com;
|
|
return 301 https://$host$request_uri;
|
|
}
|
|
server {
|
|
listen 443 ssl;
|
|
server_name dhcp.example.com;
|
|
ssl_certificate /etc/letsencrypt/live/dhcp.example.com/fullchain.pem;
|
|
ssl_certificate_key /etc/letsencrypt/live/dhcp.example.com/privkey.pem;
|
|
|
|
location / {
|
|
proxy_pass http://127.0.0.1:8080;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
}
|
|
}
|
|
```
|
|
|
|
> 看板与上报用同一 token 保护;反代仅加密传输,不改变令牌机制。ESP8266 的 `SERVER_URL` 改用 `https://...` 即可(需固件支持 TLS,当前固件为 HTTP 直连,如走 HTTPS 需评估内存)。 |