---
title: "How to Setup a NGINX RTMP Server for Streaming"
description: "Learn how to set up an NGINX RTMP server and keep your live stream from crashing in this step-by-step tutorial!"
canonical: "https://adamtheautomator.com/nginx-rtmp/"
---

# How to Setup a NGINX RTMP Server for Streaming

> Learn how to set up an NGINX RTMP server and keep your live stream from crashing in this step-by-step tutorial!

Source: https://adamtheautomator.com/nginx-rtmp/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Setup a NGINX RTMP Server for Streaming](https://adamtheautomator.com/wp-content/uploads/2022/01/How-to-Setup-a-NGINX-RTMP-Server-for-Streaming.jpg)

# How to Setup a NGINX RTMP Server for Streaming

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)1 February 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Command Line](/tag/command-line/)[Linux](/tag/linux/)[NGINX](/tag/nginx/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Configuring an RTMP Server](#configuring-an-rtmp-server)
*   [Securing Network by Setting up a Firewall](#securing-network-by-setting-up-a-firewall)
*   [Streaming Video using FFmpeg](#streaming-video-using-ffmpeg)
*   [Conclusion](#conclusion)

Is your live stream crashing every time? If so, you’ll need to find a reliable way to host your stream. But if you can’t afford expensive options, no worries! [NGINX RTMP](https://docs.nginx.com/nginx/admin-guide/dynamic-modules/rtmp/) will save the day. NGINX RTMP is a reliable open-source module, so you know what that means — it’s FREE!

In this tutorial, you’ll learn how to set up an NGINX RTMP server so you can set up a reliable live stream with your website.

Ready? Read on and start setting up a live stream!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

*   A Linux server – This demo uses Ubuntu 20.04 LTS, but you can use any other Linux distribution with NGINX.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   [NGINX](https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/) installed on the Linux server.
    
*   A non-root user with [sudo](https://www.liquidweb.com/kb/how-to-set-up-and-manage-sudo-permissions/) privileges.
    
*   Minimum resources required to run a live stream server – 2G RAM, 2 CPU Cores, and 20 GB HDD.
    

> _These values are suggestions only. The exact values depend on your actual traffic needs. The more traffic you have, the more powerful servers you will need to host and stream your live video. In a development environment, an NGINX RTMP server with 2 CPUs and 2 GB RAM is sufficient to test._

## Configuring an RTMP Server

Now that the prerequisites are out of the way, you can now move on to installing and configuring your RTMP server. You’ll first install an NGINX module to convert your NGINX server into an RTMP server, then configure the NGINX configuration file to enable live streams for users.

1\. Open your terminal and run the `apt update` command to update all your packages. This command synchronizes your package database with the latest database from Ubuntu.

```bash
sudo apt update -y
```

![Updating all your packages](https://adamtheautomator.com/wp-content/uploads/2022/01/image-383.png)

Updating all your packages

2\. Next, run the following command to install an RTMP server on your Linux server.

The `libnginx-mod-rtmp` module is an NGINX module that converts your NGINX server into an RTMP server. This module enables NGINX to communicate with Adobe Flash player so you can stream your live video or audio, and is officially supported by its development team.

```bash
sudo apt install libnginx-mod-rtmp -y
```

![ installing an RTMP server](https://adamtheautomator.com/wp-content/uploads/2022/01/image-384.png)

installing an RTMP server

3\. Open the _/etc/nginx/nginx.conf_ file in your favorite editor to configure your RTMP live program. The _nginx.conf_ file is an NGINX configuration file, which contains several directives or blocks to define the NGINX service’s behavior.

4\. Finally, populate the _nginx.conf_ file with the following code, save the changes and exit the editor.

The below code tells NGINX to act as an RTMP server and enables live streams for your users.

```bash
# This block of code tells NGINX to act as an RTMP server. 
rtmp {
		server {
		# Sets the port the server will listen on.
    listen 1935;
				# The size of each chunk of data that will be sent.
				# Flash Player will freeze if the chunk is too small, 
				# which can cause a buffer-underflow condition.
        chunk_size 4096;
				# The IP addresses are allowed to publish content to the server.
				# Allow only the localhost to publish content on NGINX RTMP.
				allow publish 127.0.0.1;
				# Deny publishing for everyone else.
				deny publish all;
				# Defines a named application called "live"
        application live {
				# Tells NGINX to enable live streams for your users.
						live on;
				# This line tells NGINX to disable recording. If this line is not included, 
				# NGINX will keep recording and save your stream forever to your disk.
						record off;
				}
		}
}
```

## Securing Network by Setting up a Firewall

You’ve already configured your NGINX RTMP server, but you’ll need to secure your network and control the traffic flow. How? By setting up a firewall, Uncomplicated Firewall (UFW), which is the default firewall in Ubuntu.

Related:[How To Set Up the UFW Firewall on Linux](https://adamtheautomator.com/ufw-firewall/)

If you put your NGINX RTMP server behind a firewall, you’ll have to allow the NGINX RTMP server port to be open. Since you’ve configured your NGINX RTMP server to listen on port `1935`, you’ll allow this port through your firewall.

1\. Run the following command to add a firewall rule that opens port `1935` on your firewall. This firewall rule allows your NGINX RTMP server port `1935` to be accessible for all incoming and outgoing requests.

```bash
sudo ufw allow 1935/tcp
```

![Opening Port 1935 on your firewall](https://adamtheautomator.com/wp-content/uploads/2022/01/image-385.png)

Opening Port 1935 on your firewall

2\. Next, run the following command to disable and re-enable your firewall. This command basically restarts the firewall for any changes on the firewall to take effect.

```bash
sudo ufw disable && sudo ufw enable
```

3\. Finally, run the command below to check the status of your firewall and verify if port `1935` is accessible from all IP addresses (**Anywhere**).

```bash
sudo ufw status
```

![Checking if Port 1935 is Accessible from Anywhere](https://adamtheautomator.com/wp-content/uploads/2022/01/image-386.png)

Checking if Port 1935 is Accessible from Anywhere

## Streaming Video using FFmpeg

Your NFINX RTMP server is now up, so it’s time to use [FFmpeg](https://ffmpeg.org/documentation.html) to create an example video and stream it through your NGINX RTMP server. [FFmpeg](https://ffmpeg.org/documentation.html) is a complete solution to record, convert and stream audio and video in many different applications like VLC, OMXPlayer, VobSub, and so on.

You’ll be testing video streaming on your server, and you can use any video that is small in size for streaming.

1\. Run the `apt install` command below to install the required dependencies for FFmpeg to run properly.

The command below installs the following:

*   `libpcre3` – A regex library that is used by the ffmpeg toolkit to help parse and compile regular expressions.
*   `libpcre3-dev` – This package contains the development headers of the libpcre3 package.
*   `libssl-dev` – The OpenSSL cryptographic library used to create SSL/TLS connections in your RTMP server.
*   `zlib1g-dev`: The zlib library is a compression and decompression library that speeds up audio and video file transfer over the Internet.

```bash
sudo apt install -y libpcre3 libpcre3-dev libssl-dev zlib1g-dev
```

![Installing Required Dependencies](https://adamtheautomator.com/wp-content/uploads/2022/01/image-387.png)

Installing Required Dependencies

2\. Next, run the following command to install `ffmpeg` on your server.

```bash
 sudo apt install ffmpeg -y 
```

![Installing FFmpeg](https://adamtheautomator.com/wp-content/uploads/2022/01/image-388.png)

Installing FFmpeg

3\. Lastly, run the [`ffmpeg`](https://ffmpeg.org/ffmpeg.html) command to perform the following and create a new live stream:

*   Read input (`re`) from the source file (`i`) at the original framerate. Be sure to replace `video.mp4` with your video’s path and filename.
*   Copy the video format verbatim (`c:v copy`), and encode the audio n AAC codec (`c:a aac`).
*   Set the audio sample rate to 44100 Hz (`ar 44100`). If your video’s sample rate is less than this value (44100 Hz), FFmpeg performs [interpolation](https://en.wikipedia.org/wiki/Interpolation#In_digital_signal_processing) to increase the sample rate.
*   Set audio channels to one channel (mono) (`ac 1`), and the video format that FFmpeg will stream (`-f flv`).
*   Set your RTMP live program (`rtmp://localhost/live/streamname.flv`) that you configured in the “Configuring an RTMP Server” section (step four). You can change `streamname.flv` to any name you prefer.

```bash
ffmpeg -re -i "video.mp4" -c:v copy -c:a aac -ar 44100 -ac 1 -f flv rtmp://139.180.203.6:1935/live/streamname.flv 
```

As you see below, the `ffmpeg` command prints out the **frame** per second it detects, the **bitrate**, and the **fps** of the newly created RTMP live stream.

![Viewing the RTMP Live Stream Data](https://adamtheautomator.com/wp-content/uploads/2022/01/image.gif)

Viewing the RTMP Live Stream Data

## Conclusion

In this tutorial, you’ve learned how to set up your NGINX RTMP server and enable live streams on your server by modifying the NGINX configuration file. You’ve also touched on setting up a video live stream using the FFmpeg tool through your NGINX RTMP server.

You’ve just realized that setting up a live stream doesn’t have to cost you every penny. At this point, why not [integrate your stream into your web page using the HTML5](https://www.dacast.com/blog/optimizing-html5-video-streaming/) <video> element. Or [set up an OBS Studio for your RTMP stream](https://www.theverge.com/2020/4/15/21213548/live-stream-obs-how-to-streamlabsobs-windows-mac-hardware)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fnginx-rtmp%2F&text=How%20to%20Setup%20a%20NGINX%20RTMP%20Server%20for%20Streaming)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fnginx-rtmp%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fnginx-rtmp%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Useful-Linux-Dig-Examples-for-the-Network-Admin.jpg)

### [Linux Dig for Network Admins: Practical Guide with Examples](/linux-dig/)

Become a network troubleshooting expert! This comprehensive Linux Dig guide empowers you with essential DNS query commands for immediate results.

![](https://adamtheautomator.com/wp-content/uploads/2021/12/install-phpmyadmin.jpg)

### [Step-by-Step Tutorial to Install phpMyAdmin Securely on Linux](/install-phpmyadmin/)

Discover how to securely install phpMyAdmin on Linux with our concise guide. Learn essential steps for a safe and efficient setup, tailored for Linux users.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
