准備工作
准備一台雲服務器,沒有的小伙伴可以去擼谷歌雲300刀
我這里是Ubuntu18.04
clone coturn代碼
git clone https://github.com/coturn/coturn.git
1
不懂git的請自行百度
編譯coturn
cd coturn
./configure --prefix=/usr/local/coturn
1
2
編譯后的文件目錄為/usr/local/coturn
錯誤1
install is /usr/bin/install
pkill is /usr/bin/pkill
sqlite3 is /usr/bin/sqlite3
Use TMP dir /var/tmp
Compiler: cc
Library option -lsocket cannot be used
Library option -lrt cannot be used
Library option -lwldap32 cannot be used
Library option -lwldap64 cannot be used
Library option -lintl cannot be used
Library option -lnsl cannot be used
Sockets code is fine: sin_len field present
pthread barriers not found
Ignore IP_RECVERR
Library option -lcrypto cannot be used
ERROR: OpenSSL Crypto development libraries are not installed properly in required location.
Abort.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
需要安裝相關的依賴
Ubuntu執行
apt install libssl-dev
1
Centos執行
yum install openssl-devel
1
然后再次運行
./configure --prefix=/usr/local/coturn
1
官方回答:https://github.com/coturn/coturn/issues/122
錯誤2
Libevent2 development is not installed properly
ERROR: Libevent2 development libraries are not installed properly in required location.
ERROR: may be you have just too old libevent tool - then you have to upgrade it.
See the INSTALL file.
Abort.
1
2
3
4
5
執行以下代碼
apt-get install libevent-dev
1
然后再次運行
./configure --prefix=/usr/local/coturn
1
查看是否生成Makefile文件,如果有代表成功
ll Makefile
1
執行make命令 -j代表是用多少線程執行,一般雙核就寫4 4核就寫8
make -j 16
sudo make install
安裝好后會看到如下圖

配置coturn

進入剛剛的目錄/usr/local/coturn ,編輯文件
cd /usr/local/coturn
vim etc/turnserver.conf.default
按照上面的截圖依次填入信息,除了雲主機的IP外,其他的都可以隨便設置改完后保存
如果出現以下錯誤
ERROR: Empty cli-password, and so telnet cli interface is disabled! Please set a non empty cli-password!錯誤
需要在vim etc/turnserver.conf.default 添加
cli-password=youpasswd
對應的github 官方issue
https://github.com/coturn/coturn/issues/361
配置證書
openssl req -x509 -newkey rsa:2048 -keyout /etc/turn_server_pkey.pem -out /etc/turn_server_cert.pem -days 99999 -nodes
vim etc/turnserver.conf.default
把以下的注釋取消掉,注意證書的路徑根據自己生成的路徑填寫
cert=/etc/turn_server_cert.pem
pkey=/etc/turn_server_pkey.pem
最小端口和最大端口
這個是限制端口范圍的,最好是設置下,並且阿里雲安全組和防火牆也要添加對應規則
vim etc/turnserver.conf.default
min-port=49152
max-port=65535
啟動turnserver
這里怕有小白所以寫的全部路徑,其實相對路徑就可以了,不懂的直接復制這句話
/usr/local/coturn/bin/turnserver -o -a -f -c /usr/local/coturn/etc/turnserver.conf.defaul -r Chengdu
注意!
如果是雲主機的話需要打開對應的端口,如阿里雲需要在安全組添加規則3478,UDP和TCP,需要同時開啟並且如果雲服務開啟了防火牆需要添加防火牆規則,我這里是Ubuntu,執行以下命令
sudo ufw allow 3478
最好telnet一下看下端口是否是通的。
telnet 雲主機ip 3478
如果是通的打開以下地址檢測
https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/




WebRTC使用trun服務
var configuration = { iceServers: [
{urls: "stun:youip:5060"},
{
urls: "turn:youip:5060",
username: 'you user',
credential: 'you passwd',
}];
};
var pc = new RTCPeerConnection(configuration);
WebRTC參考文章:https://developer.mozilla.org/zh-CN/docs/Web/API/RTCConfiguration
