Cara Membuat Server Live Streaming RTMP UBUNTU 20.04

How To Install Nginx on Ubuntu 20.04
Step 1 – Installing Nginx
Because Nginx is available in Ubuntu’s default repositories, it is possible to install it from these repositories using the apt
packaging system.
Since this is our first interaction with the apt
packaging system in this session, we will update our local package index so that we have access to the most recent package listings. Afterwards, we can install nginx
:
sudo apt update
sudo apt install nginx
Copy
After accepting the procedure, apt
will install Nginx and any required dependencies to your server.
Step 2 – Adjusting the Firewall
Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw
upon installation, making it straightforward to allow Nginx access.
List the application configurations that ufw
knows how to work with by typing:
sudo ufw app list
Copy
You should get a listing of the application profiles:
OutputAvailable applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
As demonstrated by the output, there are three profiles available for Nginx:
- Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
- Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
- Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Right now, we will only need to allow traffic on port 80.
You can enable this by typing:
sudo ufw allow 'Nginx HTTP'
Copy
You can verify the change by typing:
sudo ufw status
Copy
The output will indicated which HTTP traffic is allowed:
OutputStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
Step 3 – Checking your Web Server
At the end of the installation process, Ubuntu 20.04 starts Nginx. The web server should already be up and running.
We can check with the systemd
init system to make sure the service is running by typing:
systemctl status nginx
Copy
Output● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-04-20 16:08:19 UTC; 3 days ago
Docs: man:nginx(8)
Main PID: 2369 (nginx)
Tasks: 2 (limit: 1153)
Memory: 3.5M
CGroup: /system.slice/nginx.service
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker process
As confirmed by this out, the service has started successfully. However, the best way to test this is to actually request a page from Nginx.
You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address. If you do not know your server’s IP address, you can find it by using the icanhazip.com tool, which will give you your public IP address as received from another location on the internet:
curl -4 icanhazip.com
Copy
When you have your server’s IP address, enter it into your browser’s address bar:
http://your_server_ip
You should receive the default Nginx landing page:

If you are on this page, your server is running correctly and is ready to be managed.
Step 4 – Managing the Nginx Process
Now that you have your web server up and running, let’s review some basic management commands.
To stop your web server, type:
sudo systemctl stop nginx
Copy
To start the web server when it is stopped, type:
sudo systemctl start nginx
Copy
To stop and then start the service again, type:
sudo systemctl restart nginx
Copy
If you are only making configuration changes, Nginx can often reload without dropping connections. To do this, type:
sudo systemctl reload nginx
Copy
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:
sudo systemctl disable nginx
Copy
To re-enable the service to start up at boot, you can type:
sudo systemctl enable nginx
Copy
You have now learned basic management commands and should be ready to configure the site to host more than one domain.
Step 1 — Installing and Configuring Nginx-RTMP
Most modern streaming tools support the RTMP protocol, which defines the basic parameters of an internet video stream. The Nginx web server includes a module that allows you to provide an RTMP stream with minimal configuration from a dedicated URL, just like it provides HTTP access to web pages by default. The Nginx RTMP module isn’t included automatically with Nginx, but on Ubuntu 20.04 and most other Linux distributions you can install it as an additional package.
Begin by running the following commands as a non-root user to update your package listings and install the Nginx module:
sudo apt update
sudo apt install libnginx-mod-rtmp
Copy
Installing the module won’t automatically start providing a stream. You’ll need to add a configuration block to your Nginx configuration file that defines where and how the stream will be available.
Using nano
or your favorite text editor, open Nginx’s main configuration file, /etc/nginx/nginx.conf
, and add this configuration block to the end of the file:
sudo nano /etc/nginx/nginx.conf
Copy
/etc/nginx/nginx.conf
. . .
rtmp {
server {
listen 1935;
chunk_size 4096;
allow publish 127.0.0.1;
deny publish all;
application live {
live on;
record off;
}
}
}
listen 1935
means that RTMP will be listening for connections on port 1935, which is standard.chunk_size 4096
means that RTMP will be sending data in 4KB blocks, which is also standard.allow publish 127.0.0.1
anddeny publish all
mean that the server will only allow video to be published from the same server, to avoid any other users pushing their own streams.application live
defines an application block that will be available at the/live
URL path.live on
enables live mode so that multiple users can connect to your stream concurrently, a baseline assumption of video streaming.record off
disables Nginx-RTMP’s recording functionality, so that all streams are not separately saved to disk by default.
Save and close the file. If you are using nano
, press Ctrl+X
, then when prompted, Y
and Enter.
This provides the beginning of your RTMP configuration. By default, it listens on port 1935
, which means you’ll need to open that port in your firewall. If you configured ufw
as part of your initial server setup run the following command.
sudo ufw allow 1935/tcp
Copy
Now you can reload Nginx with your changes:
sudo systemctl reload nginx.service
Copy
You should now have a working RTMP server. In the next section, we’ll cover streaming video to your RTMP server from both local and remote sources.
Step 2 — Sending Video to Your RTMP Server
There are multiple ways to send video to your RTMP server. One option is to use ffmpeg
, a popular command line audio-video utility, to play a video file directly on your server. If you don’t have a video file already on the server, you can download one using youtube-dl
, a command line tool for capturing video from streaming platforms like YouTube. In order to use youtube-dl
, you’ll need an up to date Python installation on your server as well.
First, install Python and its package manager, pip
:
sudo apt install python3-pip
Step 3 — Streaming Video to Your Server via OBS (Optional)
Streaming via ffmpeg is convenient when you have a prepared video that you want to play back, but live streaming can be much more dynamic. The most popular software for live streaming is OBS, or Open Broadcaster Software – it is free, open source, and very powerful.
OBS is a desktop application, and will connect to your server from your local computer.
After installing OBS, configuring it means customizing which of your desktop windows and audio sources you want to add to your stream, and then adding credentials for a streaming service. This tutorial will not be covering your streaming configuration, as it is down to preference, and by default, you can have a working demo by just streaming your entire desktop. In order to set your streaming service credentials, open OBS’ settings menu, navigate to the Stream option and input the following options:
Streaming Service: Custom
Server: rtmp://your_domain/live
Play Path/Stream Key: obs_stream
obs_stream is an arbitrarily chosen path – in this case, your video would be available at rtmp://your_domain/live/obs_stream. You do not need to enable authentication, but you do need to add an additional entry to the IP whitelist that you configured in Step 1.
Back on the server, open Nginx’s main configuration file, /etc/nginx/nginx.conf, and add an additional allow publish entry for your local IP address. If you don’t know your local IP address, it’s best to just go to a site like What’s my IP which can tell you where you accessed it from:
sudo nano /etc/nginx/nginx.conf
/etc/nginx/nginx.conf
. . .
allow publish 127.0.0.1;
allow publish your_local_ip_address;
deny publish all;
. . .
Save and close the file, then reload Nginx:
sudo systemctl reload nginx.service
You should now be able to close OBS’ settings menu and click Start Streaming from the main interface! Try viewing your stream in a media player as before. Now that you’ve seen the fundamentals of streaming video in action, you can add a few other features to your server to make it more production-ready.