RTMP adalah Real-Time Messaging Protocol yang dikembangkan oleh Macromedia yang dapat digunakan untuk streaming audio, video, dan data melalui Internet. Nginx RTMP adalah media streamer berbasis Nginx yang hadir dengan banyak fitur termasuk dukungan H264/AAC, transcoding online dengan FFmpeg, dukungan callback HTTP, dan modul kontrol HTTP untuk merekam audio/video.
Dalam tutorial ini, kami akan menunjukkan cara mengompilasi Nginx dengan modul RTMP dan membuat streaming langsung RTMP di CentOS 8.
Prasyarat
- VPS CentOS 8 baru di Platform Cloud Atlantic.Net
- Kata sandi root yang dikonfigurasi di server Anda
Langkah 1 – Buat Server Cloud Atlantic.Net
Pertama, masuk ke Server Cloud Atlantic.Net Anda. Buat server baru, pilih CentOS 8 sebagai sistem operasi dengan setidaknya 2 GB RAM. Hubungkan ke Server Cloud Anda melalui SSH dan masuk menggunakan kredensial yang disorot di bagian atas halaman.
Setelah Anda masuk ke server CentOS 8 Anda, jalankan perintah berikut untuk memperbarui sistem dasar Anda dengan paket terbaru yang tersedia.
yum update -y
Langkah 2 – Instal Dependensi yang Diperlukan
Sebelum memulai, Anda perlu menginstal dependensi yang diperlukan untuk mengompilasi Nginx dengan dukungan RTMP. Pertama, instal paket grup Alat Pengembangan dengan perintah berikut:
yum groupinstall 'Development Tools' -y
Selanjutnya, instal repositori EPEL dengan perintah berikut:
yum install epel-release -y
Setelah terinstal, instal dependensi lain yang diperlukan menggunakan perintah berikut:
yum install wget git unzip perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel pcre-devel GeoIP GeoIP-devel -y
Setelah semua paket terinstal, Anda dapat melanjutkan untuk mengompilasi Nginx.
Langkah 3 – Unduh Komponen yang Diperlukan
Pertama, Anda perlu mengunduh komponen yang diperlukan untuk mengompilasi Nginx dengan dukungan RTMP.
Pertama, unduh Nginx versi stabil terbaru dengan perintah berikut:
wget http://nginx.org/download/nginx-1.18.0.tar.gz
Setelah diunduh, ekstrak menggunakan perintah berikut:
tar -xvzf nginx-1.18.0.tar.gz
Selanjutnya download PCRE dan Zlib dengan perintah berikut:
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip wget https://www.zlib.net/zlib-1.2.11.tar.gz
Setelah diunduh, ekstrak dengan perintah berikut:
unzip pcre-8.42.zip tar -xvzf zlib-1.2.11.tar.gz
Setelah keduanya diekstrak, unduh OpenSSL dengan perintah berikut:
wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
Selanjutnya, ekstrak paket yang diunduh dengan perintah berikut:
tar -xzvf openssl-1.1.0h.tar.gz
Selanjutnya, unduh versi terbaru Modul Nginx RTMP dari repositori Git menggunakan perintah berikut:
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
Setelah semua komponen diunduh, Anda dapat melanjutkan ke langkah berikutnya.
Langkah 4 – Instal Nginx dengan Dukungan RTMP
Pertama, ubah direktori ke Nginx dengan perintah berikut:
cd nginx-1.18.0/
Selanjutnya, konfigurasikan Nginx dengan dukungan RTMP menggunakan perintah berikut:
./configure --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib64/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --user=nginx \ --group=nginx \ --build=CentOS \ --builddir=nginx-1.18.0 \ --with-select_module \ --with-poll_module \ --with-threads \ --with-file-aio \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_geoip_module=dynamic \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_auth_request_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_degradation_module \ --with-http_slice_module \ --with-http_stub_status_module \ --http-log-path=/var/log/nginx/access.log \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --with-mail=dynamic \ --with-mail_ssl_module \ --with-stream=dynamic \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_geoip_module=dynamic \ --with-stream_ssl_preread_module \ --with-compat \ --with-pcre=../pcre-8.42 \ --with-pcre-jit \ --with-zlib=../zlib-1.2.11 \ --with-openssl=../openssl-1.1.0h \ --with-openssl-opt=no-nextprotoneg \ --add-module=../nginx-rtmp-module \ --with-debug
Setelah proses konfigurasi selesai, Anda akan mendapatkan output berikut:
Configuration summary + using threads + using PCRE library: ../pcre-8.42 + using OpenSSL library: ../openssl-1.1.0h + using zlib library: ../zlib-1.2.11 nginx path prefix: "/etc/nginx" nginx binary file: "/usr/sbin/nginx" nginx modules path: "/usr/lib64/nginx/modules" nginx configuration prefix: "/etc/nginx" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/var/run/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/var/log/nginx/access.log" nginx http client request body temporary files: "/var/cache/nginx/client_temp" nginx http proxy temporary files: "/var/cache/nginx/proxy_temp" nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp" nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp" nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"
Selanjutnya, instal Nginx dengan perintah berikut:
make make install
Setelah menginstal Nginx, verifikasi versi Nginx yang diinstal dengan perintah berikut:
nginx -V
Anda akan mendapatkan output berikut:
nginx version: nginx/1.18.0 (CentOS) built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC) built with OpenSSL 1.1.0h 27 Mar 2018 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules- path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log- path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock -- user=nginx --group=nginx --build=CentOS --builddir=nginx-1.14.0 --with-select_module --with- poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with- http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with- http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module -- with-http_dav_module --with-http_flv_module --with-http_mp4_module --with- http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with- http_random_index_module --with-http_secure_link_module --with-http_degradation_module -- with-http_slice_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log - -http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp- path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http- uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module -- with-stream_realip_module --with-stream_geoip_module=dynamic --with- stream_ssl_preread_module --with-compat --with-pcre=../pcre-8.42 --with-pcre-jit --with- zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.0h --with-openssl-opt=no-nextprotoneg --add- module=../nginx-rtmp-module --with-debug
Langkah 5 – Buat File Layanan Systemd untuk Nginx
Selanjutnya, Anda perlu membuat file layanan Nginx untuk mengelola layanan Nginx. Anda dapat membuatnya dengan perintah berikut:
nano /lib/systemd/system/nginx.service
Tambahkan baris berikut:
[Unit] Description=nginx - high performance web server Documentation=https://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target
Simpan dan tutup file setelah selesai, lalu muat ulang daemon systemd dengan perintah berikut:
systemctl daemon-reload
Selanjutnya, mulai Nginx dan aktifkan untuk memulai pada sistem reboot dengan perintah berikut:
systemctl start nginx systemctl enable nginx
Langkah 6 – Konfigurasi Nginx untuk Menggunakan RTMP
First, create a backup copy of the Nginx main configuration file with the following command: mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
Selanjutnya, buat file konfigurasi Nginx baru menggunakan perintah berikut:
nano /etc/nginx/nginx.conf
Tambahkan baris berikut:
worker_processes auto; events { worker_connections 1024; } # RTMP configuration rtmp { server { listen 1935; # Listen on standard RTMP port chunk_size 4000; # Define the Application application show { live on; # Turn on HLS hls on; hls_path /mnt/hls/; hls_fragment 3; hls_playlist_length 60; # disable consuming the stream from nginx as rtmp deny play all; } } } http { sendfile off; tcp_nopush on; aio on; directio 512; default_type application/octet-stream; server { listen 8080; location / { # Disable cache add_header 'Cache-Control' 'no-cache'; # CORS setup add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Expose-Headers' 'Content-Length'; # allow CORS preflight requests if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } types { application/dash+xml mpd; application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /mnt/; } } }
Simpan dan tutup file, lalu buat direktori baru untuk konfigurasi HLS dan berikan kepemilikan yang tepat:
mkdir -p /mnt/hls chown -R nginx:nginx /mnt/hls
Simpan dan tutup file setelah Anda selesai.
Langkah 7 – Buat Streaming Langsung RTMP
Selanjutnya, Anda perlu membuat video streaming RTMP dan membuat streaming RTMP langsung baru. Anda dapat melakukannya dengan mengedit file konfigurasi utama Nginx:
nano /etc/nginx/nginx.conf
Temukan baris berikut:
hls_playlist_length 60; # disable consuming the stream from nginx as rtmp deny play all; }
Tambahkan baris berikut tepat di bawah baris di atas:
application vod { play /mnt/mp4s; } # RTMP stream using OBS application stream { live on; }
Simpan dan tutup file, lalu restart layanan Nginx dengan perintah berikut:
systemctl restart nginx
Selanjutnya, buat direktori untuk menyimpan video MP4:
mkdir -p /mnt/mp4s
Selanjutnya, unduh contoh video menggunakan perintah berikut:
cd /mnt/mp4s wget https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4 -O myfile.mp4
Selanjutnya, berikan kepemilikan yang tepat ke direktori /mnt/mp4s:
chown -R nginx:nginx /mnt/mp4s
Langkah 8 – Instal FFmpeg dan Mulai Server Streaming
Pertama, Anda perlu menginstal FFmpeg di server Anda. Secara default, itu tidak diinstal di repositori default CentOS 8, jadi Anda harus mengaktifkan repo Powertool di sistem Anda. Anda dapat mengaktifkannya dengan perintah berikut:
yum install epel-release dnf-utils yum-config-manager --set-enabled PowerTools yum-config-manager --add-repo=https://negativo17.org/repos/epel-multimedia.repo
Selanjutnya, instal FFmpeg dengan perintah berikut:
yum install ffmpeg -y
Setelah FFmpeg diinstal, mulai server streaming dengan perintah berikut:
ffmpeg -re -i /mnt/mp4s/myfile.mp4 -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 - f flv rtmp://your-server-ip:1935/show/vod
Langkah 9 – Uji Streaming RTMP
Selanjutnya, Anda perlu menguji aliran vod RTMP dengan pemutar media VLC.
Setelah sistem klien, buka pemutar media VLS, buka VLC Media => Buka Aliran Jaringan . Anda akan melihat layar berikut:
Berikan URL server streaming RTMP Anda dan klik Mainkan tombol.
Setelah Anda berhasil terhubung, Anda akan melihat video MP4 Anda di layar berikut:
Kesimpulan
Dalam tutorial di atas, Anda telah mempelajari cara menginstal Nginx dengan dukungan RTMP. Anda juga mempelajari cara mengonfigurasi Nginx untuk melakukan streaming video MP4 dan memutarnya dengan pemutar media VLC dari sistem jarak jauh. Coba instal Nginx dengan dukungan RTMP di VPS Hosting dari Atlantic.Net hari ini!
Pelajari lebih lanjut tentang layanan hosting VPS dan server pribadi Virtual kami.