Setting Up an RTMP Server for Media Streaming

A comprehensive walkthrough for creating a robust audio streaming solution with Ubuntu, Nginx, and FFmpeg

Introduction

RTMP (Real-Time Messaging Protocol) remains one of the most reliable protocols for low-latency audio and video streaming, despite being initially developed for Flash applications. This guide will walk you through setting up a production-ready RTMP server using Ubuntu 24.04 LTS, Nginx with the RTMP module, and FFmpeg.

Note: While this guide focuses on audio streaming, the same configuration can be adapted for video streaming with minimal adjustments.

RTMP Audio Streamer

Prerequisites

Before beginning the installation, ensure you have:

Hardware Requirements

  • Processor: 2+ vCPUs (4+ recommended for production)
  • Memory: 2+ GB RAM (4GB recommended for concurrent streams)
  • Storage: 40+ GB disk space (SSD recommended for better performance)
  • Network: Stable connection with sufficient bandwidth for your expected traffic

Software Requirements

  • Ubuntu 24.04 LTS (recommended) or other Linux distribution
  • Root or sudo access to the server
  • Basic familiarity with Linux command line

Understanding RTMP

RTMP (Real-Time Messaging Protocol) was initially a proprietary protocol developed by Macromedia (later Adobe) for streaming audio, video, and data between a Flash player and server. Despite Flash's deprecation, RTMP remains popular due to its:

Key Advantages

  • Low latency (1-3 seconds typically)
  • Persistent TCP connection
  • Support for multiplexed streams
  • Wide compatibility with encoders

Common Use Cases

  • Live audio streaming
  • Video conferencing
  • Interactive live streams
  • Ingest protocol for restreaming

RTMP Workflow

The typical RTMP workflow involves:

  1. Handshake: Client and server establish connection
  2. Connect: Client connects to the application
  3. Create Stream: Client creates a stream channel
  4. Publish/Play: Client either publishes or plays the stream

Server Setup

1. Initial Server Configuration

First, update your system and install essential packages:

# Update package lists and upgrade existing packages
sudo apt update && sudo apt upgrade -y

# Install essential utilities
sudo apt install -y software-properties-common curl wget fail2ban net-tools

Important: If you have Apache installed, you'll need to remove or stop it as it conflicts with Nginx on port 80:

sudo systemctl stop apache2
sudo apt remove apache2 -y

2. Install Development Tools

Install compilation tools and libraries needed for building modules:

sudo apt install -y gcc make build-essential autoconf automake \
libssl-dev libpcre3-dev zlib1g-dev libcurl4-openssl-dev \
libxml2-dev libyajl-dev libgd-dev ffmpeg

Nginx with RTMP Module Installation

1. Add Nginx Repository

Ubuntu 24.04 includes the RTMP module in its repositories, but we'll use the official Nginx repository for the latest version:

# Import Nginx signing key
curl https://nginx.org/keys/nginx-signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# Add Nginx repository
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# Set repository priority
echo -e "Package: *\nPin: origin nginx.org\nPin-Priority: 900\n" \
    | sudo tee /etc/apt/preferences.d/99nginx

2. Install Nginx with RTMP Module

sudo apt update
sudo apt install -y nginx libnginx-mod-rtmp librtmp-dev \
libnginx-mod-http* libnginx-mod-stream

3. Configure Nginx User

sudo useradd -r -m -s /usr/sbin/nologin nginx
sudo usermod -a -G www-data nginx

# Start and enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx

RTMP Configuration

1. Configure Nginx for RTMP

Edit the Nginx configuration file (/etc/nginx/nginx.conf) and add the RTMP block:

rtmp {
    server {
        listen 1935;  # Standard RTMP port
        listen 9035;  # Additional port for your application
        
        chunk_size 4096;
        
        # IP restrictions for publishing
        allow publish 127.0.0.1;
        allow publish 41.0.0.0/8;  # Example network
        allow publish 151.0.0.0/8; # Example network
        deny publish all;
        
        # Live streaming application
        application liveradio {
            live on;
            
            # Recording configuration
            record all;
            record_suffix -%d-%b-%y-%H%M%S.flv;
            record_path /var/www/html/radio/public/audios/recordings;
            record_max_size 50M;
            
            # HLS configuration
            hls on;
            hls_path /var/www/html/radio/public/audios/hls;
            hls_fragment 5s;
            hls_playlist_length 1h;
            
            # DASH configuration
            dash on;
            dash_path /var/www/html/radio/public/audios/live/dash;
            dash_fragment 5s;
            
            # Event handlers
            exec_publish /usr/local/bin/pause-playback.bash;
            exec_publish_done /usr/local/bin/switch-to-playback.bash;
        }
        
        # Playback application
        application radio {
            live on;
            record off;
            
            hls on;
            hls_path /var/www/html/radio/public/audios/hls;
            hls_fragment 5s;
            
            dash on;
            dash_path /var/www/html/radio/public/audios/dash;
        }
    }
}

Configuration Notes:

  • Adjust IP ranges in allow publish to match your network
  • Modify paths according to your directory structure
  • Consider adjusting HLS/DASH fragment sizes based on your latency requirements

2. Create Required Directories

sudo mkdir -p /var/www/html/radio/public/audios/{recordings,hls,live/dash,dash}
sudo chown -R nginx:www-data /var/www/html/radio
sudo chmod -R 775 /var/www/html/radio

3. Test and Reload Configuration

# Test configuration
sudo nginx -t

# Reload Nginx
sudo systemctl reload nginx

Network Configuration

1. Firewall Setup

Allow traffic on RTMP ports through your firewall:

sudo ufw allow 1935/tcp
sudo ufw allow 9035/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

2. Verify Port Accessibility

Check that Nginx is listening on the correct ports:

sudo ss -tulnp | grep -E '1935|9035'

Test external connectivity (replace with your domain/IP):

nmap -sS -p 80,443,1935,9035 stream.example.com

Testing Your RTMP Server

1. Publishing a Test Stream

Use FFmpeg to publish a test stream:

ffmpeg -re -i test.mp3 -c:a aac -b:a 128k -f flv rtmp://localhost/liveradio/stream

2. Playing the Stream

Use FFplay or VLC to test playback:

ffplay rtmp://localhost/liveradio/stream

Or access the HLS stream in a compatible player:

ffplay http://localhost/radio/public/audios/hls/stream.m3u8

Next Steps

1. Production Considerations

  • Set up SSL/TLS for secure streaming
  • Configure monitoring for your RTMP server
  • Implement authentication for publishing streams
  • Set up proper logging and log rotation

2. Example Stream URLs

Publish URL:

rtmp://yourdomain.com:9035/liveradio/streamkey

Playback URLs:

rtmp://yourdomain.com:1935/radio/streamkey http://yourdomain.com/radio/public/audios/hls/stream.m3u8

Download our Audio Streaming App

To start streaming from your mobile device, download our Android app from the Google Play Store:

Troubleshooting

Common Issues and Solutions

1. Connection refused on RTMP port

  • Check Nginx is running: sudo systemctl status nginx
  • Verify RTMP configuration is loaded: sudo nginx -T | grep rtmp
  • Check firewall rules: sudo ufw status

2. Stream not appearing

  • Verify publisher IP is in allowed range
  • Check application name matches exactly
  • Inspect Nginx error logs: sudo tail -f /var/log/nginx/error.log

Conclusion

You've now set up a fully functional RTMP server capable of handling live audio streaming with recording capabilities and HLS/DASH fallback options. This configuration provides a solid foundation that can be extended with additional features like:

  • Transcoding for multiple quality levels
  • Authentication for stream publishers
  • CDN integration for large-scale distribution
  • Monitoring and analytics

For production environments, consider implementing proper monitoring and scaling strategies based on your expected audience size.