【WebRTC全流程】按照官方推荐的方式部署LiveKit(配置turn和ssl/tls证书)

下边是几种部署livekit的方式

部署模式 ssl/tls证书 turn/stun 域名 备注 文档
纯内网 不需要 不需要 不需要 https://pidan.dev/20250715/webrtc-livekit-deploy-with-no-tls/
公网 需要 all in one自带turn 需要 不支持自签证书 官方模式 https://pidan.dev/20250721/webrtc-livekit-deploy-with-tls/
公网 需要 使用第三方turn 需要 不支持自签证书 https://pidan.dev/20250722/webrtc-livekit-deploy-config-turn-server/

本文按照官方文档部署一个安全的livekit会议室服务器,自动配置ssl、turn、stun等组件。

官方流程会自动配置启动一个Caddy并自动设置&自动续签免费域名SSL证书,启动一个all in one的livekit服务器,内置一个turn服务器,这样你就不需要自己去找半天turn服务配置上发现都不能用。

本文完全参考了官方文档 https://docs.livekit.io/home/self-hosting/vm/ 只是自己操作一遍

使用官方配置工具

准备:

  • livekit域名,比如:livekit.abc.com
  • turn域名,比如:turn-lk.abc.com

自行解析域名到机器上。

执行docker容器,开始进入配置流程。

1
2
docker pull livekit/generate
docker run --rm -it -v$PWD:/output livekit/generate

官方配置过程

一共几个步骤,基本都选默认,配置上你的livekit和turn域名,完成后,生成了一个给你测试用的token,后边进入会议室能用到。当前目录会生成一个用你的域名命名的文件夹,进入就是所有的配置文件。

启动容器们

1
docker compose up -d

连接到会议室

这时候就完成了,你的程序接口是wss://livekit.abc.com,在网页版 https://meet.livekit.io/里输入刚才打印给你的token和你的服务地址。

livekit网页会议室入口

测试Turn

使用ICE测试页面,测试你的Turn服务

https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

测试turn连通性

这时候一个官方流程配置出来的所有配置文件如下

https://github.com/livekit/livekit/blob/master/config-sample.yaml 官方的配置文件示例也给你贴出来

livekit.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
port: 7880
bind_addresses:
- ""
rtc:
tcp_port: 7881
port_range_start: 50000
port_range_end: 60000
use_external_ip: true
enable_loopback_candidate: false
redis:
address: localhost:6379
username: ""
password: ""
db: 0
use_tls: false
sentinel_master_name: ""
sentinel_username: ""
sentinel_password: ""
sentinel_addresses: []
cluster_addresses: []
max_redirects: null
turn:
enabled: true
domain: turn-lk.abc.com
tls_port: 5349
udp_port: 3478
external_tls: true
keys:
APIeE: RkBUNQrEe3m

caddy.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
logging:
logs:
default:
level: INFO
storage:
"module": "file_system"
"root": "/data"
apps:
tls:
certificates:
automate:
- livekit.abc.com
- turn-lk.abc.com
layer4:
servers:
main:
listen: [":443"]
routes:
- match:
- tls:
sni:
- "turn-lk.abc.com"
handle:
- handler: tls
- handler: proxy
upstreams:
- dial: ["localhost:5349"]
- match:
- tls:
sni:
- "livekit.abc.com"
handle:
- handler: tls
connection_policies:
- alpn: ["http/1.1"]
- handler: proxy
upstreams:
- dial: ["localhost:7880"]

redis.conf

1
2
3
4
5
bind 127.0.0.1 ::1
protected-mode yes
port 6379
timeout 0
tcp-keepalive 300

docker-compose.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# This docker-compose requires host networking, which is only available on Linux
# This compose will not function correctly on Mac or Windows
services:
caddy:
image: livekit/caddyl4
command: run --config /etc/caddy.yaml --adapter yaml
restart: unless-stopped
network_mode: "host"
volumes:
- ./caddy.yaml:/etc/caddy.yaml
- ./caddy_data:/data
livekit:
image: livekit/livekit-server:latest
command: --config /etc/livekit.yaml
restart: unless-stopped
network_mode: "host"
volumes:
- ./livekit.yaml:/etc/livekit.yaml
redis:
image: redis:7-alpine
command: redis-server /etc/redis.conf
restart: unless-stopped
network_mode: "host"
volumes:
- ./redis.conf:/etc/redis.conf