使用官方配置的方式公网部署LiveKit,使用自己的ssl证书,不使用caddy自动申请的免费证书。
首先按照 https://pidan.dev/20250721/webrtc-livekit-deploy-with-tls/ 这里的部署方式,使用官方的教程创建好各种启动的配置文件。然后我们修改一下这些配置文件,来使用我们自己申请的证书文件。
你也可以直接复制本文的各种配置文件直接启动livekit,不走他的官方启动流程也是没问题的。
设置域名解析
我们配置两个域名都解析到这台公网服务器,并且我们已经下载了他们的ssl证书文件。
- lk.abc.com 此域名为livekit的域名
- lk-turn.abc.com 此域名为livekit内置的turn的域名
你的公网服务器防火墙需要开启的端口:
- 8443/TCP livekit接口 HTTPS and TURN/TLS
- 3478/UDP TURN/UDP 网络线路条件良好的情况下,此端口不开也能使用,但是推荐开。
- 50000-50100/UDP WebRTC over UDP
实测上边的这几个端口是必须开的.
配置caddy
编辑或创建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 39 40
| logging: logs: default: level: INFO storage: "module": "file_system" "root": "/data" apps: tls: certificates: load_files: - certificate: "/cert/lk.abc.com_bundle.pem" key: "/cert/lk.abc.com.key" - certificate: "/cert/lk-turn.abc.com_bundle.pem" key: "/cert/lk-turn.abc.com.key" layer4: servers: main: listen: [":8443"] routes: - match: - tls: sni: - "lk-turn.abc.com" handle: - handler: tls - handler: proxy upstreams: - dial: ["livekit:5349"] - match: - tls: sni: - "lk.abc.com" handle: - handler: tls connection_policies: - alpn: ["http/1.1"] - handler: proxy upstreams: - dial: ["livekit:7880"]
|
配置livekit配置文件
编辑或创建livekit.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
| port: 7880 bind_addresses: - "" rtc: tcp_port: 7881 port_range_start: 50000 port_range_end: 50100 use_external_ip: true enable_loopback_candidate: false redis: address: lk-redis: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: lk-turn.abc.com tls_port: 5349 udp_port: 3478 external_tls: true keys: APIVg796bZUscSq: p60ixbfgRFssDXAH8tWJfyxg2ftmfZeIw0QCki53fYZI
|
修改redis配置
编辑或创建redis.conf文件
1 2 3 4 5
| bind 0.0.0.0 # 这里放开 protected-mode no # 这里关闭 port 6379 timeout 0 tcp-keepalive 300
|
修改docker compose文件
编辑或创建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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| services: lk-caddy: image: livekit/caddyl4 command: run --config /etc/caddy.yaml --adapter yaml restart: unless-stopped ports: - "8443:8443" volumes: - ./caddy.yaml:/etc/caddy.yaml - ./caddy_data:/data - ./cert:/cert networks: - livekit-network livekit: image: livekit/livekit-server:latest command: --config /etc/livekit.yaml restart: unless-stopped ports: - "7880:7880" - "7881:7881" - "5349:5349" - "3478:3478/udp" - "3478:3478/tcp" - "50000-50100:50000-50100/udp" volumes: - ./livekit.yaml:/etc/livekit.yaml environment: - TZ=Asia/Shanghai ulimits: nproc: 65535 nofile: soft: 65535 hard: 70000 networks: - livekit-network lk-redis: image: redis:7-alpine command: redis-server /etc/redis.conf restart: unless-stopped volumes: - ./redis.conf:/etc/redis.conf networks: - livekit-network networks: livekit-network: driver: bridge
|
放置ssl证书文件
证书我从腾讯云申请的免费ssl证书文件。
cloud.tencent.com_bundle.crt 证书文件
cloud.tencent.com_bundle.pem 证书文件
cloud.tencent.com.key 私钥文件
cloud.tencent.com.csr CSR 文件
创建目录并放到./cert目录下。然后设置权限
1 2 3
| cd ./cert chmod 644 *.crt chmod 600 *.key
|
启动livekit服务
1 2 3 4 5
| docker compose up -d
docker compose down
docker compose logs -f --tail 1000
|
测试连通性
https://livekit.io/connection-test 使用官方的测试页面进行测试
此时我们的livekit服务接口为wss://lk.abc.com:8443
至于token需要调用官方的sdk生成一下,比如下边是我写的一个livekit go sdk的示例:
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
| package main
import ( "fmt" "github.com/livekit/protocol/auth" "time" )
func NewAccessToken(roomName, pID string) (string, error) {
apiKey := "APIgJX" apiSecret := "AXBbhpWBguYelOy6c"
at := auth.NewAccessToken(apiKey, apiSecret) grant := &auth.VideoGrant{ RoomJoin: true, Room: roomName, } at.SetVideoGrant(grant). SetIdentity(pID). SetName(pID). SetValidFor(time.Hour * 24 * 30)
return at.ToJWT() }
func main() { token, _ := NewAccessToken("room001", "user002") fmt.Printf(token) }
|
参考
https://docs.livekit.io/home/self-hosting/vm/
https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/