A comprehensive walkthrough for creating a robust audio streaming solution with Ubuntu, Nginx, and FFmpeg
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.
Before beginning the installation, ensure you have:
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:
The typical RTMP workflow involves:
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
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
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
sudo apt update
sudo apt install -y nginx libnginx-mod-rtmp librtmp-dev \
libnginx-mod-http* libnginx-mod-stream
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
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:
allow publish
to match your networksudo 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
# Test configuration
sudo nginx -t
# Reload Nginx
sudo systemctl reload nginx
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
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
Use FFmpeg to publish a test stream:
ffmpeg -re -i test.mp3 -c:a aac -b:a 128k -f flv rtmp://localhost/liveradio/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
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
To start streaming from your mobile device, download our Android app from the Google Play Store:
1. Connection refused on RTMP port
sudo systemctl status nginx
sudo nginx -T | grep rtmp
sudo ufw status
2. Stream not appearing
sudo tail -f /var/log/nginx/error.log
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:
For production environments, consider implementing proper monitoring and scaling strategies based on your expected audience size.