Configure VPS as WebRTC SFU Media Server with LiveKit: Infrastructure from Host to Optimize for Startups
This guide provides a detailed walkthrough on configuring a VPS to run LiveKit, an open-source SFU (Selective Forwarding Unit) media server for WebRTC applications. With a...
This guide provides a detailed walkthrough on configuring a VPS to run LiveKit, an open-source SFU (Selective Forwarding Unit) media server for WebRTC applications. With a host-to-optimized architecture, this guide enables startups to deploy efficient, cost-effective, and scalable real-time video, audio, and data streaming infrastructure. Key steps include installing Docker, setting up LiveKit services such as LiveKit Server and LiveKit WebRTC, and integrating Nginx as a reverse proxy. The guide also covers network configuration, security best practices, and basic verification commands to ensure stable system operation.
Requirements and Preparation
To deploy LiveKit on a VPS, you need a virtual private server with at least 2 CPU cores, 4GB RAM, and 50GB SSD NVMe storage. The VPS should run a Linux operating system, preferably Ubuntu 22.04 LTS or Debian 11. You will also need a domain name (or subdomain) to access the service and root access to install required tools. Essential tools include Docker, Docker Compose, and Git. If you don't have a suitable VPS, consider the NVMe Cloud VPS for Docker, Nginx & Databases for a high-performance setup.
Install and Configure LiveKit
LiveKit can be deployed easily using Docker Compose. First, create a working directory and download the sample configuration file:
mkdir livekit && cd livekit
curl -o docker-compose.yml https://raw.githubusercontent.com/livekit/livekit-server/master/docker-compose.yml
Modify the docker-compose.yml file to configure services such as LiveKit Server, Redis, and Kafka. For example, you can set a default room name and API interface configuration:
services:
livekit-server:
image: livekit/livekit-server:latest
ports:
- "7880:7880"
- "7881:7881"
environment:
- LIVEKIT_SERVER_ADDRESS=0.0.0.0:7880
- LIVEKIT_API_KEY=your_api_key
- LIVEKIT_API_SECRET=your_api_secret
- LIVEKIT_DEFAULT_ROOM_NAME=my_default_room
Start LiveKit with the following command:
docker-compose up -d
Integrate Nginx as Reverse Proxy and SSL
To secure the service and enable HTTPS access, configure Nginx as a reverse proxy. Install Nginx and create a configuration file:
sudo apt update
sudo apt install nginx
Create a configuration file at /etc/nginx/sites-available/livekit:
server {
listen 80;
server_name your-domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
location / {
proxy_pass http://localhost:7880;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Activate the configuration and reload Nginx:
sudo ln -s /etc/nginx/sites-available/livekit /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Perform Testing and Optimization
After installation, verify the service status using the following command:
docker-compose ps
Ensure all containers (livekit-server, redis, kafka) are running. Test connectivity by accessing https://your-domain.com in a browser. If errors occur, check logs with:
docker-compose logs livekit-server
To optimize performance, adjust parameters in the docker-compose.yml file, such as increasing the number of workers for LiveKit or configuring Redis to handle multiple concurrent connections. Additionally, use tools like the Guide to Setting Up Global Server Load Balancing GSLB with PowerDNS and GeoIP to distribute load across geographic regions if scaling is required.
Troubleshooting
If the service fails to start, verify that ports 7880, 7881, and 443 are not blocked by the firewall. On the VPS, open the required ports with:
sudo ufw allow 7880
sudo ufw allow 7881
sudo ufw allow 443
If HTTPS access fails, ensure the Let's Encrypt certificate has been issued and is correctly placed in the specified path. If API key errors occur, double-check the environment variables in docker-compose.yml and confirm they match the keys generated in the LiveKit dashboard.
References
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
