WebRTC 网络中继 Coturn 服务安装及部署

WebRTC 网络中继 Coturn 服务安装及部署本文主要介绍 Coturn 在 Linux Windows 系统下部署的步骤

大家好,欢迎来到IT知识分享网。

概要

        在 WebRTC 网络传输模块中,当遇到 NAT 之间无法打通的情况下,会使用 TURN 协议通过中转的方式实现端与端之间的通信。Coturn 就是一种开源的 STUN/TURN 服务器,它可以让你轻松地搭建一个能够在 NAT 防火墙和代理服务器背后运行的实时通信系统。Coturn 支持各种协议和技术,包括 STUN(Session Traversal Utilities for NAT)、TURN(Traversal Using Relays around NAT)和 ICE(Interactive Connectivity Establishment)。Coturn 可以用于向 WebRTC 应用程序提供 TURN 服务器,这些应用程序需要在 P2P 通信中转储流量。本文主要介绍 Coturn 在 Linux、Windows 系统下部署的步骤。

Linux 系统下部署(以 CentOS8.0 为例)

  • 下载 libevent2,因为 Coturn 编译的时候会用到 libevent2。
wget https://github.com/libevent/libevent/releases/download/release-2.1.10-stable/libevent-2.1.10-stable.tar.gz
  • 解压 libevent-2.1.10-stable.tar.gz
tar -zxvf libevent-2.1.10-stable.tar.gz
  • 编译并安装  libevent2
cd libevent-2.1.10-stable ./configure && make && make install
  • 通过 wget 命令下载 Coturn 安装包(新版本可能会产生未知的编译问题,所以这里下载的是 4.5.1.1 老版本)。
wget https://github.com/coturn/coturn/archive/4.5.1.1.tar.gz
  • 使用 tar -zxvf 命令解压 4.5.1.1.tar.gz
tar -zxvf 4.5.1.1.tar.gz
  • 编译并安装 Coturn
cd coturn-4.5.1.1 ./configure --prefix=/usr/local/coturn make && make install

配置 Coturn

  • 切换到 coturn 的 etc 目录,拷贝一份 turnserver.conf.default 再进行修改。
cd /usr/local/coturn/etc cp ./turnserver.conf.default ./turnserver.conf vi ./turnserver.conf
  • 修改 turnserver.conf 文件配置内容,realm 需要指定 IP 或域名,否则无法在 WebRTC 本地库中使用,虽然使用 trickle-ice 可以正常访问,但是在 WebRTC 本地库中是没法正常使用的。
// 指定侦听的端口。 listening-port=3478 // 云主机内网 IP 地址。 listening-ip=xxx.xxx.xxx.xxx // 云主机的公网 IP 地址。 external-ip=xxx.xxx.xxx.xxx // 这个很重要,如果没有配置这个就服务使用中转服务。 // 云主机的公网 IP 地址或域名。 realm=xxx.xxx.xxx.xxx // 访问 STUN/TURN 服务的用户名和密码。 user=admin:

 

云主机端口配置

        如果是阿里云、腾讯云的云主机,需要到控制中的安全策略组或者防火墙中允许 3478、49152-65535 端口 UDP/TCP 通行。

启动 Coturn

  • 添加环境变量
vi ~/.bashrc export PATH=$PATH:/usr/local/coturn/bin source ~/.bashrc
  • 通过配置文件启动
turnserver -c /usr/local/coturn/etc/turnserver.conf
  • 测试 STUN/TURN 服务,使用火狐浏览器(其他浏览器有问题)打开以下连接
  • Trickle ICE 出现 relay 说明配置成功。

WebRTC 网络中继 Coturn 服务安装及部署

 

 

将 Coturn 设置为系统服务

把 Coturn 设置成系统服务,便于开机自动启动,就不用手动启动了。

  • 执行以下命令:
touch /usr/lib/systemd/system/coturn.service 
  • 然后编辑 coturn.service 文件
vi coturn.service
  • 设置 coturn.service 文件内容
[Unit] Description=Coturn Server After=network.target [Install] WantedBy=multi-user.target [Service] User=root Group=root ExecStart=/usr/local/coturn/bin/turnserver -c /usr/local/coturn/etc/turnserver.conf LimitNOFILE = 5000
  • 将 coturn 设置开机启动
systemctl enable coturn
  • 启动 coturn 服务
systemctl start coturn

 


 

Windows 系统下部署

        Coturn 原本只能运行在 Linux 系统上的,当我们在开发项目的时候还有 Linux 云主机,又想要使用 TURN 服务,这时候就需借助 Cygwin 工具来安装 Coturn 了。Cygwin 是一个在 Windows 平台上运行的类 UNIX 模拟环境。

  • 下载 Cygwin

 https://cygwin.com/install.html

WebRTC 网络中继 Coturn 服务安装及部署

  • 安装  Cygwin,安装的时候需要把 Devel 设置为安装,因为需要 gcc 编译工具。

WebRTC 网络中继 Coturn 服务安装及部署

 

WebRTC 网络中继 Coturn 服务安装及部署

 

WebRTC 网络中继 Coturn 服务安装及部署

  •  下载 libevent2 和 coturn,并解压到 Cygwin 的 bin 目录中。

https://github.com/libevent/libevent/releases/download/release-2.1.10-stable/libevent-2.1.10-stable.tar.gz

https://github.com/coturn/coturn/archive/4.5.1.1.tar.gz

  • 运行安装命令和配置参考最上面的 “Linux 系统下部署” 的方法。

小结:

        以上就是 coturn 在 Linux 以及 Windows 系统中的安装部署了,如果在安装部署中遇到问题可以通过微信公众号私信我,我将逐一解答。

微信公众号:KeisoftCN

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/127613.html

(0)
上一篇 2025-09-06 14:45
下一篇 2025-09-06 15:10

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信