zhibo/Log/服务搭建/视频通话-TURN服务.md
xiao12feng8 7376df609d 主题:添加聊天室
功能:聊天室功能添加
2026-01-11 14:07:50 +08:00

3.1 KiB
Raw Blame History

TURN服务器部署文档 (Coturn)

服务信息

项目
服务器IP 1.15.149.240
监听端口 3478 (UDP/TCP)
用户名 turnuser
密码 TurnPass123456
TURN地址 turn:1.15.149.240:3478

一、安装Coturn

CentOS/OpenCloudOS

yum install -y coturn

Ubuntu/Debian

apt install -y coturn

二、配置文件

位置:/etc/coturn/turnserver.conf

# 监听端口
listening-port=3478

# 监听所有网卡
listening-ip=0.0.0.0

# 你的服务器公网IP需要修改
external-ip=你的公网IP

# realm
realm=你的公网IP

# 认证方式
lt-cred-mech

# 用户名:密码
user=turnuser:TurnPass123456

# 日志
log-file=/var/log/coturn/turnserver.log
verbose
simple-log

# 不使用TLS
no-tls
no-dtls

三、启动服务

# 启动
systemctl start coturn

# 设置开机自启
systemctl enable coturn

# 查看状态
systemctl status coturn

# 查看日志
tail -f /var/log/coturn/turnserver.log

四、防火墙配置

# 开放端口
firewall-cmd --permanent --add-port=3478/tcp
firewall-cmd --permanent --add-port=3478/udp
firewall-cmd --permanent --add-port=49152-65535/udp
firewall-cmd --reload

云服务器安全组

协议 端口 用途
TCP 3478 TURN控制
UDP 3478 TURN控制
UDP 49152-65535 媒体中继

五、验证服务

1. 检查端口

netstat -tlnup | grep 3478

2. 在线测试

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

填入:

  • STUN or TURN URI: turn:你的IP:3478
  • TURN username: turnuser
  • TURN password: TurnPass123456

点击 "Gather candidates",如果看到 relay 类型的候选者说明TURN服务正常。


六、常用命令

# 启动
systemctl start coturn

# 停止
systemctl stop coturn

# 重启
systemctl restart coturn

# 查看状态
systemctl status coturn

# 查看日志
tail -100 /var/log/coturn/turnserver.log

七、Android客户端配置

local.properties 中配置:

turn.server_host=你的服务器IP
turn.server_port=3478

WebRTCConfig.java 中使用:

public static final String TURN_SERVER_URL = "turn:" + BuildConfig.TURN_SERVER_HOST + ":" + BuildConfig.TURN_SERVER_PORT;
public static final String TURN_USERNAME = "turnuser";
public static final String TURN_PASSWORD = "TurnPass123456";

八、在新服务器部署(完整步骤)

# 1. 安装coturn
yum install -y coturn   # CentOS
# 或
apt install -y coturn   # Ubuntu

# 2. 创建配置文件
cat > /etc/coturn/turnserver.conf << 'EOF'
listening-port=3478
listening-ip=0.0.0.0
external-ip=你的公网IP
realm=你的公网IP
lt-cred-mech
user=turnuser:TurnPass123456
log-file=/var/log/coturn/turnserver.log
verbose
simple-log
no-tls
no-dtls
EOF

# 3. 创建日志目录
mkdir -p /var/log/coturn

# 4. 启动服务
systemctl enable coturn
systemctl start coturn

# 5. 验证
systemctl status coturn
netstat -tlnup | grep 3478