LiveKit 本地部署全流程指南(含 HTTPS/WSS)

发布于:2025-07-09 ⋅ 阅读:(13) ⋅ 点赞:(0)

1. 环境准备

  • 操作系统:Windows 10/11 或 Linux/Mac
  • 需有本地公网/内网 IP(如 192.168.x.x)
  • 推荐浏览器:Chrome/Edge/Firefox/Safari
  • 端口未被占用,防火墙允许相关端口

2. 目录结构建议

livekit/
  livekit-server.exe
  config.yaml
  turn-cert.pem
  turn-key.pem
  caddy.exe
  Caddyfile
  www/         # 前端静态文件目录
    medical-livekit.html
    ...

3. 生成本地自签名证书(HTTPS/TURN)

3.1 生成证书(以 Windows 为例)

openssl req -x509 -newkey rsa:4096 -sha256 -days 365 -nodes \
  -keyout turn-key.pem -out turn-cert.pem \
  -subj "/CN=localhost" \
  -addext "subjectAltName=DNS:localhost,IP:127.0.0.1,IP:192.168.20.40"
  • 192.168.20.40 替换为你的本地 IP

4. 配置 LiveKit

4.1 config.yaml 示例

port: 7880

rtc:
  port_range_start: 50000
  port_range_end: 60000
  tcp_port: 7881
  use_external_ip: true
  allow_tcp_fallback: true
  stun_servers:
    - "stun.l.google.com:19302"

turn:
  enabled: true
  udp_port: 3478
  tls_port: 5349
  relay_range_start: 50000
  relay_range_end: 60000
  domain: "192.168.20.40"      # 本机内网IP
  cert_file: "turn-cert.pem"
  key_file: "turn-key.pem"

keys:
  devkey: "secret123456789012345678901234567890"

logging:
  level: info
  json: false
  sample: false

room:
  auto_create: true
  empty_timeout: 300
  departure_timeout: 20
  max_participants: 0
  enabled_codecs:
    - mime: audio/opus
    - mime: video/vp8
    - mime: video/h264
  enable_remote_unmute: true

5. 配置 Caddy 反向代理 + 本地 HTTPS

5.1 下载 Caddy

5.2 Caddyfile 示例

https://192.168.20.40:9443 {
    tls turn-cert.pem turn-key.pem
    encode gzip
    root * ./www
    file_server

    # LiveKit signaling (WebSocket)
    reverse_proxy /rtc* 127.0.0.1:7880

    # 可选:API 代理等
    # reverse_proxy /api* 127.0.0.1:xxxx
}
  • 端口 9443 避免与系统 443 冲突
  • root * ./www 指向你的前端静态文件目录

5.3 启动 Caddy

caddy run --config Caddyfile

6. 启动 LiveKit 服务

./livekit-server.exe --config config.yaml

7. 本地防火墙与端口

  • Windows:允许 7880、7881、3478、5349、50000-60000、9443 端口通过
  • Linux:sudo ufw allow 7880,7881,3478,5349,9443/tcpsudo ufw allow 50000:60000/udp

8. 前端 Web 客户端配置

  • signaling 地址:wss://192.168.20.40:9443/rtc
  • 页面地址:https://192.168.20.40:9443/medical-livekit.html
  • 访问时浏览器会提示自签名证书不安全,选择继续访问即可

9. 测试 TURN/ICE

  • 用手机/电脑在同一局域网访问 https 页面
  • F12 控制台查看 ICE 日志,确认有 relay 类型候选
  • 如需外网测试,需做端口转发

10. 常见问题排查

  • 端口未开放:检查防火墙和 Caddy/LiveKit 启动日志
  • 证书不被信任:本地自签名证书需手动信任或忽略警告
  • 没有 relay 候选:只能用 signaling 下发的动态 TURN,不能用静态用户名密码测试
  • WebSocket 失败:确保用 wss 协议,Caddy 代理配置正确

11. 参考命令

Windows 启动顺序

# 1. 启动 LiveKit
.\livekit-server.exe --config config.yaml

# 2. 启动 Caddy
caddy run --config Caddyfile

Linux 启动顺序

./livekit-server --config config.yaml
caddy run --config Caddyfile

12. 访问方式

  • 电脑/手机浏览器访问:https://192.168.20.40:9443/medical-livekit.html
  • signaling 地址自动适配 wss

13. 进阶

  • 可用 hosts 文件将 192.168.20.40 绑定为自定义域名,证书 CN 也写该域名
  • 生产环境建议用公网服务器+正式域名+Let’s Encrypt 证书


网站公告

今日签到

点亮在社区的每一天
去签到