Install TURN server on Ubuntu for WebRTC

0
344

Install TURN server on Ubuntu for WebRTC. Basically, TURN is an extension from STUN, so when this extension is not used, we still connect via STUN. During server declaration is used, if there is both server address STUN and TURN added.

Typically, STUN is prioritized to be used first in order to speed up the connection to get user candidate information behind NAT.

If the media does not directly connect and work together, then TURN is required to exchange intermediaries by the Proxy server.

In this article, I would like to guide people to create their own TURN server, hopefully it is useful for people who do not want to depend on Google STUN.

Extra: WebRTC need SSL. You can see more Add Let’s Encrypt SSL certificate for Nginx website

Here I am using Ubuntu 18.

Install TURN server on Ubuntu for WebRTC

First, update the libraries.

apt-get -y update

Then install coturn

apt-get -y install coturn

In Ubuntu, turnserver has been packaged into coturn packages. Download and use: D.

Open /etc/default/coturn and edit like this

#
# Uncomment it if you want to have the turnserver running as 
# an automatic system service daemon
#
TURNSERVER_ENABLED=1

Save and continue to edit the file /etc/turnserver.conf

Open /etc/turnserver.conf and find the line

#user=username1:key1

Add below following structure.

user=<your_custom_username>:<your_custom_password>

For example:

user=khanh:123456

Save and start coturn.

service coturn start

Apply config:

turnserver -a -o -v -n  --no-dtls --no-tls -u khanh:123456 -r "khanh_realm"

Now you have your TURN server running.

To connect, you use port 3478. This is default port of turn server.

iceServers: [
    {
        'urls': 'stun:stun.l.google.com:19302'
    },
    {
        'urls': 'turn:<your_ip_server>:3478',
        'credential': <your_custom_username>,
        'username': <your_custom_password>
    }
]

Demo:

iceServers: [
    {
        'urls': 'stun:stun.l.google.com:19302'
    },
    {
        'urls': 'turn:12.34.56.78:3478',
        'credential': 'khanh',
        'username': '123456'
    }
]

Thank you for reading. Any problem with the article please leave a comment.

Leave a Reply

avatar