VPS HOSTING

Optimizing Streaming VPS for Live Video

Updated: December 2024
18 min read
9.8K views

X-ZoneServers Streaming VPS is optimized for video streaming applications. This guide shows you how to configure your server for maximum performance and minimal latency.

1 Installing Nginx with RTMP Module

Install Dependencies

sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev

Download and Compile Nginx with RTMP

# Create build directory
cd /tmp

# Download Nginx
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz

# Clone RTMP module
git clone https://github.com/arut/nginx-rtmp-module.git

# Compile Nginx with RTMP
cd nginx-1.24.0
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
make
sudo make install

Configure Nginx for Streaming

sudo nano /usr/local/nginx/conf/nginx.conf

Add this RTMP configuration:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;

            # Enable HLS
            hls on;
            hls_path /var/www/html/hls;
            hls_fragment 3;
            hls_playlist_length 60;

            # Enable DASH
            dash on;
            dash_path /var/www/html/dash;
        }
    }
}

http {
    server {
        listen 8080;

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /var/www/html;
            add_header Cache-Control no-cache;
            add_header Access-Control-Allow-Origin *;
        }

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
    }
}

Create Directories and Start Nginx

# Create HLS directory
sudo mkdir -p /var/www/html/hls
sudo chmod -R 755 /var/www/html/hls

# Start Nginx
sudo /usr/local/nginx/sbin/nginx

# Create systemd service
sudo nano /etc/systemd/system/nginx.service

Add this systemd service configuration:

[Unit]
Description=Nginx RTMP Server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
# Enable and start service
sudo systemctl enable nginx
sudo systemctl start nginx

# Open firewall ports
sudo ufw allow 1935/tcp comment 'RTMP'
sudo ufw allow 8080/tcp comment 'HLS'

2 Configuring OBS Studio

OBS Settings for Streaming

Server:

rtmp://your_server_ip:1935/live

Stream Key:

stream

Recommended Video Settings

  • Video Bitrate: 3000-6000 Kbps (1080p), 1500-3000 Kbps (720p)
  • Encoder: x264 or NVENC (if GPU available)
  • Keyframe Interval: 2 seconds
  • CPU Preset: veryfast or faster
  • Profile: main or high
  • FPS: 30 or 60

Audio Settings

  • Audio Bitrate: 128-160 Kbps
  • Sample Rate: 44.1 kHz or 48 kHz
  • Channels: Stereo

Test Your Stream

View your stream at:

http://your_server_ip:8080/hls/stream.m3u8

Use VLC Player or any HLS-compatible player to test playback.

3 Performance Optimization

System Tuning

# Edit sysctl configuration
sudo nano /etc/sysctl.conf

# Add these lines:
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864
net.core.netdev_max_backlog = 5000

# Apply changes
sudo sysctl -p

Nginx Worker Optimization

# In nginx.conf:
worker_processes auto;
worker_connections 4096;
worker_rlimit_nofile 8192;

Enable BBR Congestion Control

# Add to /etc/sysctl.conf
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr

# Apply
sudo sysctl -p

# Verify
sysctl net.ipv4.tcp_congestion_control

Pro Tip

X-ZoneServers Streaming VPS comes pre-optimized for video streaming with high bandwidth allocation and low latency routing.

4 CDN Integration

For global audiences, integrate a CDN to reduce latency:

Cloudflare Setup

  1. 1. Point your domain to your streaming server IP
  2. 2. Enable Cloudflare proxy (orange cloud)
  3. 3. Set SSL/TLS mode to "Full"
  4. 4. Enable "Auto Minify" for HTML, CSS, JS
  5. 5. Turn on "Brotli" compression

Multi-Bitrate Streaming

Configure adaptive bitrate streaming:

application live {
    live on;
    record off;

    # Create multiple variants
    exec ffmpeg -i rtmp://localhost/live/$name
        -c:v libx264 -b:v 3000k -preset veryfast -g 60 -f flv rtmp://localhost/hls/$name_high
        -c:v libx264 -b:v 1500k -preset veryfast -g 60 -f flv rtmp://localhost/hls/$name_mid
        -c:v libx264 -b:v 750k -preset veryfast -g 60 -f flv rtmp://localhost/hls/$name_low;
}

5 Monitoring Streams

View Stream Statistics

Access real-time stats at:

http://your_server_ip:8080/stat

Monitor Server Resources

# Install monitoring tools
sudo apt install htop iftop nethogs -y

# Monitor CPU/RAM
htop

# Monitor network bandwidth
sudo iftop

# Monitor per-process network usage
sudo nethogs

Set Up Logging

# View Nginx access logs
tail -f /usr/local/nginx/logs/access.log

# View error logs
tail -f /usr/local/nginx/logs/error.log

# Monitor system logs
sudo journalctl -u nginx -f

Streaming Checklist

  • ✓ Nginx RTMP server configured
  • ✓ HLS/DASH enabled for adaptive streaming
  • ✓ System optimized for network performance
  • ✓ CDN integrated for global delivery
  • ✓ Monitoring and logging in place

Was this article helpful?

Need more help?

Contact Support