How to Set Up a Live Streaming Server with Nginx and RTMP Module

Live Streaming Server With Nginx And Rtmp Module

In this tutorial, we will guide you through setting up your own live streaming server using Nginx and the RTMP (Real-Time Messaging Protocol) module. This allows you to stream live content from your computer, using a tool like OBS Studio, to your own server, giving you full control over the content delivery.

By the end of this tutorial, you’ll be able to stream your content through your own infrastructure and even broadcast to platforms like YouTube or Twitch.


Step 1: Install Nginx with the RTMP Module

1.1 Install Dependencies

Before installing Nginx with RTMP support, make sure your system has the required dependencies. These include libraries for building software and other tools. For Ubuntu or Debian-based systems, run:

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

1.2 Download Nginx and the RTMP Module

Next, download the Nginx source code and the RTMP module. You can do this by cloning both from their respective repositories.

# Download Nginx source
wget http://nginx.org/download/nginx-1.21.1.tar.gz
tar -zxvf nginx-1.21.1.tar.gz
cd nginx-1.21.1

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

1.3 Compile and Install Nginx with RTMP Module

Now, compile Nginx from the source and include the RTMP module in the build. To do this, navigate to the Nginx source directory and run the configuration, make, and installation commands:

./configure --add-module=../nginx-rtmp-module --with-http_ssl_module
make
sudo make install

After this, Nginx will be installed with RTMP module support.

1.4 Verify Nginx Installation

Once installed, verify that Nginx is installed and running properly by starting the Nginx service:

sudo /usr/local/nginx/sbin/nginx

You can check if it is running by visiting http://your-server-ip or http://localhost in your browser.


Step 2: Configure Nginx for RTMP Streaming

2.1 Edit Nginx Configuration

To enable RTMP streaming, modify the Nginx configuration file. Open the configuration file located at /usr/local/nginx/conf/nginx.conf for editing:

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

Add the following configuration at the end of the file to set up the RTMP streaming:

rtmp {
server {
listen 1935; # RTMP port

application live {
live on;
record off; # Disable recording of streams
}
}
}

This configuration sets up an RTMP server that listens on port 1935, the default RTMP port, and enables live streaming without recording the streams.

2.2 Restart Nginx

After editing the configuration file, restart the Nginx server to apply the changes:

sudo /usr/local/nginx/sbin/nginx -s reload

Now, your Nginx server is ready to accept RTMP streams.


Step 3: Stream Content Using OBS Studio

Now that the Nginx RTMP server is ready, we need a tool to stream content. OBS Studio is a popular open-source streaming tool that you can use to broadcast your live video to your Nginx server.

3.1 Download and Install OBS Studio

If you don’t already have OBS Studio installed, download it from the official website and install it on your computer.

3.2 Configure OBS Studio for Streaming

  1. Open OBS Studio and go to the Settings menu.
  2. In Settings, go to the Stream section.
  3. Set the Service to Custom.
  4. In the Server field, enter the following URL:
rtmp://your-server-ip/live

Replace your-server-ip with your server’s IP address or domain name.

  1. In the Stream Key field, enter any identifier you like, such as mystream. This will be used in the URL to access your stream.
  2. Click OK to save the settings.

Step 4: Start Streaming

4.1 Start Broadcasting with OBS Studio

  1. Prepare your scene in OBS Studio (you can add video sources like webcams, screen captures, etc.).
  2. When you’re ready to go live, click Start Streaming in OBS Studio.

OBS will start streaming your content to the Nginx RTMP server.


Step 5: View the Live Stream

Now that your stream is running on your Nginx RTMP server, you can view the live stream using an RTMP player like VLC or by embedding the stream on a web page.

5.1 Watch the Stream via VLC

  1. Open VLC Media Player.
  2. Go to Media > Open Network Stream.
  3. In the URL field, enter the following:
rtmp://your-server-ip/live/mystream

Replace your-server-ip with your server’s IP address or domain name and mystream with the stream key you used in OBS Studio.

  1. Click Play. You should now see your live stream in VLC.

Step 6: (Optional) Re-stream to Platforms Like YouTube or Twitch

If you want to stream to multiple platforms (like YouTube or Twitch) through your Nginx server, you can modify the Nginx configuration to push the stream to external RTMP servers.

6.1 Edit Nginx Configuration to Re-stream

Open the Nginx configuration file again:

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

Modify the RTMP section to push the stream to an external service like YouTube:

rtmp {
server {
listen 1935;

application live {
live on;
record off;

# Push stream to YouTube
push rtmp://a.rtmp.youtube.com/live2/YOUR-YOUTUBE-STREAM-KEY;
}
}
}

Replace YOUR-YOUTUBE-STREAM-KEY with the stream key provided by YouTube (or Twitch, Facebook, etc.).

Restart Nginx again to apply the changes:

sudo /usr/local/nginx/sbin/nginx -s reload

Now, when you stream to your Nginx server, it will also forward the stream to YouTube or any other platform you specify.


Conclusion

Setting up your own live streaming server using Nginx with the RTMP module gives you complete control over your streams. You can host your streams privately or push them to external platforms like YouTube or Twitch. With OBS Studio, you can easily broadcast content to your Nginx server, giving you a professional streaming setup without the need for third-party platforms.

By following this tutorial, you now have the foundation for a fully functional live streaming solution that can be customized for various use cases, from private broadcasts to multi-platform streams.