Setting Up a Basic Live Streaming Server with Nginx and RTMP
To establish a live streaming server, we'll leverage Nginx along with the nginx-rtmp-module. This guide outlines the steps from obtaining the necessary components to configuring the server and streaming content. First, obtain the nginx-rtmp-module:
cd /data/nginx
yum install git
git clone https://github.com/arut/nginx-rtmp-module.git
Next, install Nginx with the RTMP module. Ensure you have development tools and libraries installed: ```
yum install gcc make pcre-devel openssl-devel wget http://nginx.org/download/nginx-1.15.0.tar.gz tar xf nginx-1.15.0.tar.gz cd nginx-1.15.0 ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module make && make install
Modify the Nginx configuration file (`nginx.conf`) to include RTMP server settings. The following configuration enables RTMP listening on port 1935 and sets up an HLS (HTTP Live Streaming) application: ```
rtmp {
server {
listen 1935; # Port for RTMP connections
chunk_size 4000; # Adjust chunk size; lower values increase CPU, minimum 128
application hls { # Application name for RTMP stream
live on; # Enable live streaming
hls on; # Enable HLS output
hls_path /usr/share/nginx/html/hls; # Directory to store HLS segments; ensure write permissions
hls_fragment 5s; # Duration of each HLS segment in seconds
}
}
}
Once Nginx is configured, you can push streams to it. **Using FFmpeg for Streaming:** The following command pushes an MP4 file as a live stream: ```
ffmpeg -re -i /path/to/your/video.mp4 -vcodec copy -acodec copy -f flv rtmp://YOUR_SERVER_IP:1935/hls
Replace `YOUR\_SERVER\_IP` with you're server's IP address. \*\*Using OBS Studio for Streaimng:\*\* 1. Download and install OBS Studio. 2. Go to Settings -> Stream. 3. Set the Server URL to `rtmp://YOUR\_SERVER\_IP:1935/hls`. 4. Use a Stream Key (e.g., a room number) to identify your stream. You can also use VLC or other sources to stream content. After starting the stream, check the directory specified in `hls\_path` (e.g., `/usr/share/nginx/html/hls`) for generated `.m3u8` and `.ts` files. To view the live stream, you can use various clients. \*\*Using an HTML5 Browser:\*\* Create an HTML file (e.g., `play.html`) with the following content: ```
<video controls width="640" height="360">
<source src="http://YOUR_SERVER_IP/hls/stream_key.m3u8" type="application/x-mpegURL"/>
Your browser does not support HTML5 video.
</video>
Replace YOUR\_SERVER\_IP and stream\_key accordingly. Access this HTML file through you're web server. **Using VLC Media Player:** Open VLC and go to Media -> Open Network Stream. Enter the following URL: ```
http://YOUR_SERVER_IP/hls/stream_key.m3u8
Replace `YOUR\_SERVER\_IP` and `stream\_key` as needed.