Setting Up a Simple Live Streaming Server on Debian 11

Setting up a live stream backend can be a valuable project for understanding media streaming technology. Here's a basic overview and steps to get you started.

1. How a Live Stream Backend Works

A typical live streaming backend captures video, processes it, and then transmits it to viewers. The core components include:

  • Ingest: Receives the live video stream from a source (e.g., a camera or software encoder).
  • Encoding/Transcoding: Converts the video to various formats and bitrates for different devices and network conditions.
  • Packaging: Wraps the encoded video in formats like HLS (HTTP Live Streaming) or DASH (Dynamic Adaptive Streaming over HTTP).
  • Distribution: Delivers the video to users over the internet, often using a Content Delivery Network (CDN) for scalability.
  • Playback: Users receive and play the video on their devices.

2. Setting Up a Simple Live Streaming Server on Debian 11

With your cloud server, you can use FFmpeg for ingest and encoding, NGINX with the RTMP module for streaming, and HLS for adaptive streaming. Here's a streamlined guide:

Step 1: Install FFmpeg and NGINX with RTMP Module
bash 复制代码
sudo apt update
sudo apt install ffmpeg nginx
sudo apt install libnginx-mod-rtmp  # Adds RTMP support to NGINX
Step 2: Configure NGINX for RTMP

Edit your NGINX configuration file (often located at /etc/nginx/nginx.conf) to include RTMP settings:

nginx 复制代码
rtmp {
    server {
        listen 1935;  # RTMP port

        application live {
            live on;
            record off;
            # Configure HLS settings
            hls on;
            hls_path /tmp/hls;
            hls_fragment 3;
        }
    }
}

After configuring, restart NGINX:

bash 复制代码
sudo systemctl restart nginx
Step 3: Start a Stream with FFmpeg

Use FFmpeg to push a live stream to your server. Replace input.mp4 with your video source.

bash 复制代码
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f flv rtmp://<your_server_ip>/live/stream_key
Step 4: Enable HLS Playback

With the HLS setting in place, your stream will be available in .m3u8 format. You can access it at:

复制代码
http://<your_server_ip>/hls/stream_key.m3u8

3. Client Software for Testing

VLC Media Player is excellent for testing. Open VLC, go to Media > Open Network Stream , and enter the URL of your .m3u8 stream:

复制代码
http://<your_server_ip>/hls/stream_key.m3u8

This setup will give you a functional live streaming backend for study purposes, allowing you to test and optimize real-time video delivery!

To install NGINX and the RTMP module (libnginx-mod-rtmp) from source, you will need to compile them manually, as you're aiming to do a source-based installation. Below are the detailed steps to install both components on your Debian 11 server.

Step 1: Install Required Dependencies

First, install all the required dependencies for building NGINX and the RTMP module.

bash 复制代码
sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev libcurl4-openssl-dev

Step 2: Download and Extract NGINX Source Code

  1. Go to the official NGINX website and download the latest stable version.
bash 复制代码
cd /usr/local/src
sudo wget http://nginx.org/download/nginx-1.24.0.tar.gz
  1. Extract the NGINX archive:
bash 复制代码
sudo tar -xzvf nginx-1.24.0.tar.gz
cd nginx-1.24.0

Step 3: Download and Prepare the RTMP Module

  1. The nginx-rtmp-module is a third-party module that adds RTMP support to NGINX. Download it from GitHub:
bash 复制代码
sudo git clone https://github.com/arut/nginx-rtmp-module.git

Step 4: Compile NGINX with RTMP Module

  1. Configure NGINX to include the RTMP module. The following command will configure NGINX with the RTMP module, SSL support, and some basic features (you can modify the options as needed):
bash 复制代码
sudo ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre --with-http_v2_module --add-module=../nginx-rtmp-module
  1. After configuring, compile NGINX:
bash 复制代码
sudo make
  1. Install NGINX:
bash 复制代码
sudo make install

Step 5: Start NGINX

  1. After installation, the NGINX binary will be located in /usr/local/nginx/sbin/nginx. Start NGINX:
bash 复制代码
sudo /usr/local/nginx/sbin/nginx
  1. To check if NGINX is running, visit your server's IP address in a browser (e.g., http://<your_server_ip>). You should see the NGINX welcome page.

Step 6: Configure NGINX for RTMP

  1. Edit the NGINX configuration file to set up the RTMP stream server. Open /usr/local/nginx/conf/nginx.conf in your preferred text editor:
bash 复制代码
sudo nano /usr/local/nginx/conf/nginx.conf
  1. Add the following configuration for RTMP streaming:
nginx 复制代码
rtmp {
    server {
        listen 1935;  # RTMP listen port

        application live {
            live on;
            record off;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 3;
        }
    }
}
  1. Save the configuration and exit the text editor.

Step 7: Restart NGINX to Apply Changes

  1. Restart NGINX to apply the changes:
bash 复制代码
sudo /usr/local/nginx/sbin/nginx -s reload

Step 8: Test the RTMP Stream

You can now test the RTMP server. To stream to it, use FFmpeg (or any other RTMP-compatible software) to send a stream to your NGINX server:

bash 复制代码
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f flv rtmp://<your_server_ip>/live/stream_key

Step 9: Access the HLS Stream

If you've enabled HLS, you should be able to access the live stream in .m3u8 format at:

复制代码
http://<your_server_ip>/hls/stream_key.m3u8

You can use VLC Media Player to test the stream by opening the URL.

Step 10: Manage NGINX as a Service (Optional)

If you want NGINX to start automatically on boot, you'll need to create a systemd service file.

  1. Create the systemd service file:
bash 复制代码
sudo nano /etc/systemd/system/nginx.service
  1. Add the following configuration:
ini 复制代码
[Unit]
Description=The NGINX HTTP and reverse proxy server
Documentation=http://nginx.org/en/docs/
After=network.target

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

[Install]
WantedBy=multi-user.target
  1. Reload systemd and enable the NGINX service:
bash 复制代码
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx

Conclusion

You now have NGINX with the RTMP module installed from source and configured for live streaming. You can test the stream with FFmpeg and playback it with VLC Media Player using HLS or RTMP.

ffmpeg dependency:

sudo apt-get update

sudo apt install nasm

相关推荐
Tim风声(网络工程师)4 小时前
光功率计中的红光(光衰测试设备)的使用
运维·网络
ar01234 小时前
AR电路巡检:让电力运维进入智能可视化时代
运维·人工智能·ar
2301_780789666 小时前
云服务器数据会泄露吗?怎么保护云服务器的数据
运维·服务器·tcp/ip·网络安全
2301_780789666 小时前
云服务器被黑能恢复吗?云服务器被黑的解决办法
运维·服务器·网络·安全·web安全
淘小白_TXB21966 小时前
头条百家采集改写发布软件用户使用手册
运维·服务器·头条采集·头条号采集·文章采集·头条采集软件
渣渣灰95878 小时前
解决VMware上终端窗口太小问题
运维·服务器·经验分享
Tim风声(网络工程师)8 小时前
QoS (服务质量)和TE(流量工程)的区别
运维·网络
OpsEye8 小时前
企业监控避坑:别再把工具当体系!
运维·运维开发
线束线缆组件品替网9 小时前
Amphenol ICC RJE1Y33610162401解析:工业网络线束为何越来越重要?
运维·服务器·网络·数码相机·智能路由器·电脑·智能音箱
NOCSAH10 小时前
统好AI:采购发票与付款管理的自动化协同实践
运维·人工智能·自动化·统好ai