Select | Name | |
---|---|---|
Peter Parker | [email protected] |
How to add or remove rows inside a table dynamically using jQuery
By Thanh Tùng
Posted at tháng 8 24, 2020
jQuery
No comments
GETTING STARTED WITH NGINX RTMP
By Thanh Tùng
Posted at tháng 2 07, 2020
VPS
No comments
Build Utilities
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev git zlib1g-dev -y
On Amazon Linux: sudo yum install git gcc make pcre-devel openssl-devel zlib1g-dev
Make and CD to build directory (home)
sudo mkdir ~/build && cd ~/build
Download & unpack latest nginx-rtmp (you can also use http)
sudo git clone git://github.com/arut/nginx-rtmp-module.git
Download & unpack nginx (you can also use svn)
sudo wget http://nginx.org/download/nginx-1.14.1.tar.gz
sudo tar xzf nginx-1.14.1.tar.gz
cd nginx-1.14.1
Build nginx with nginx-rtmp
sudo ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
sudo make
sudo make install
Start nginx Server
sudo /usr/local/nginx/sbin/nginx
SET UP LIVE STREAMING
To set up RTMP support you need to add
rtmp{}
section to nginx.conf
(can be found in PREFIX/conf/nginx.conf). Stock nginx.conf
contains only http{}
section.sudo nano /usr/local/nginx/conf/nginx.conf
Use this
nginx.conf
instead of stock config:#user nobody;
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
# sample handlers
#location /on_play {
# if ($arg_pageUrl ~* localhost) {
# return 201;
# }
# return 202;
#}
#location /on_publish {
# return 201;
#}
#location /vod {
# alias /var/myvideos;
#}
# rtmp stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# you can move stat.xsl to a different location
root /usr/build/nginx-rtmp-module;
}
# rtmp control
location /control {
rtmp_control all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application myapp {
live on;
# sample play/publish handlers
#on_play http://localhost:8080/on_play;
#on_publish http://localhost:8080/on_publish;
# sample recorder
#recorder rec1 {
# record all;
# record_interval 30s;
# record_path /tmp;
# record_unique on;
#}
# sample HLS
#hls on;
#hls_path /tmp/hls;
#hls_sync 100ms;
}
# Video on demand
#application vod {
# play /var/Videos;
#}
# Video on demand over HTTP
#application vod_http {
# play http://localhost:8080/vod/;
#}
}
}
Restart nginx with:
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx
STATISTICS
Navigate your browser to
http://localhost:8080/stat
to see current streaming statistics, connected clients, bandwidth etc.CONFIG RTMP HLS
push rtmp://localhost:1935/show
rtmp { server { listen 1935; # Listen on standard RTMP port chunk_size 4000; application show { live on; # Turn on HLS hls on; hls_path /usr/local/nginx/html/hls; hls_fragment 3; hls_playlist_length 60; # disable consuming the stream from nginx as rtmp allow play all; } } }
REPUSH SERVER ANOTHER
push rtmp://domain.com:1935/application/;
HTTP SERVER CONFIG
Since HLS consists of static files, a simple http server can be set up with two additions, correct MIME types and CORS headers.
server { listen 8080; location /hls { # 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/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /mnt/; } }
TẠO TOKEN BẢO MẬT CHO NGINX
By Thanh Tùng
Posted at tháng 2 07, 2020
PHP
No comments
Code tạo token
<?PHP$secret = ‘vietgithiviet’;
$id = $_GET[‘id’];
$ttl = 7200;
$ip = $_SERVER[‘REMOTE_ADDR’]; //nếu là local thì điền 127.0.0.1
$token = buildSecureLink($secret,$ttl,$ip);
function buildSecureLink($secret,$ttl,$ip){
$expires = time() + $ttl;
$md5 = md5(“$expires$ip $secret”, true);
$md5 = base64_encode($md5);
$md5 = strtr($md5, ‘+/’, ‘-_’);
$md5 = str_replace(‘=’, ”, $md5);
return $md5.’/’.$expires;
};$m3u = ‘http://localhost/live/’.$token.’/’.$id.’.m3u8′;
echo $m3u;?>
Code bảo mật trong nginx
rewrite ^/live/(.*)/(.*)/(.*)$ /live/$3?token=$1&expires=$2 last;secure_link $arg_token,$arg_expires;
secure_link_md5 “$secure_link_expires$remote_addr vietgithiviet”;if ($secure_link = “”) { return 403; }
if ($secure_link = “0”) { return 410; }
Link xuất sẽ có dạng
http://localhost/live/’.$token.’/’.$id.’.m3u8
LẤY IP THẬT CỦA CLIENT QUA CLOUDFLARE
By Thanh Tùng
Posted at tháng 2 07, 2020
PHP
No comments
function getUserIP() {
if (isset($_SERVER[“HTTP_CF_CONNECTING_IP”])) {
$_SERVER[‘REMOTE_ADDR’] = $_SERVER[“HTTP_CF_CONNECTING_IP”];
$_SERVER[‘HTTP_CLIENT_IP’] = $_SERVER[“HTTP_CF_CONNECTING_IP”];
}
$client = @$_SERVER[‘HTTP_CLIENT_IP’];
$forward = @$_SERVER[‘HTTP_X_FORWARDED_FOR’];
$remote = $_SERVER[‘REMOTE_ADDR’];
if (isset($_SERVER[“HTTP_CF_CONNECTING_IP”])) {
$_SERVER[‘REMOTE_ADDR’] = $_SERVER[“HTTP_CF_CONNECTING_IP”];
$_SERVER[‘HTTP_CLIENT_IP’] = $_SERVER[“HTTP_CF_CONNECTING_IP”];
}
$client = @$_SERVER[‘HTTP_CLIENT_IP’];
$forward = @$_SERVER[‘HTTP_X_FORWARDED_FOR’];
$remote = $_SERVER[‘REMOTE_ADDR’];
if(filter_var($client, FILTER_VALIDATE_IP)) { $ip = $client; }
elseif(filter_var($forward, FILTER_VALIDATE_IP)) { $ip = $forward; }
else { $ip = $remote; }
return $ip;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP)) { $ip = $forward; }
else { $ip = $remote; }
return $ip;
}
echo getUserIP();
Hướng dẫn cài đặt screen linux
By Thanh Tùng
Posted at tháng 1 16, 2020
VPS
No comments
Sysadmins thường làm việc với Linux terminal. Linux Screen còn có nhiều ưu điểm hơn nữa để hỗ trợ cho Terminal! Đây là ứng dụng ưa thích của tất cả các chuyên gia linux.
Có thể nó hơi lạ lúc ban đầu nhưng nếu quen dùng Linux bạn sẽ thấy nó hỗ trợ vô hạn tính năng. Việc sử dụng tài nguyên hệ thống hiệu quả cũng khiến nó là một tool không thể thay thế.
Vì vậy, bài viết hôm nay chúng tôi sẽ giới thiệ cho bạn biết Linux Screen là gì và làm thế nào để cài đặt, sử dụng nó hiệu quả để tăng sức mạnh và tính linh hoạt cho Linux Terminal.
Linux Screen là gì?
Linux Screen là ứng dụng terminal được lập trình bởi GNU project. Tài liệu chính thức gọi nó là GNU Screen. Screen có mục – mở nhiều cửa sổ shell riêng biệt trong duy nhất một cửa sổ lệnh.
Hay nói cách khác, Screen chia những terminal vật lý thành nhiều session ảo, và vì vậy kết hợp được các hoạt động chúng.
Nếu bạn mở một session terminal bằng Sceen, khi bạn kết thúc và tắt máy thì nó vẫn để sesion chạy. Khi terminal được mở lại, terminal sẽ quay lại screen tại thời điểm tắt.
Sử dụng Screen là một kỹ năng cơ bản. Nó không đòi hỏi kiến thức kỹ thuậtt gì phức tạp, nhưng ai dùng Linux cũng nên biết. Tóm lại, Screen Linux là chương trình giúp lập trình viên:
- Mở nhiều cửa sổ shell từ một terminal command để thực thi nhiều lệnh cùng lúc: chạy lệnh đa nhiệm.
- Giữ Shell hoạt động kể cả khi ngắt kết nối (mất điện, rớt mạng, hoặc giữ một chương trình chạy trong thời gian dài).
- Khôi phục lại kết nối nhưng vẫn giữ phiên làm việc của shell từ bất kỳ máy nào.
Làm thế nào để cài đặt và sử dụng Linux Screen?
Screen được cài đặt mặt định trên các Linux distribution phổ biến. Nếu hệ thống không có cài đặt sẵn cũng không sao. Vì cách cài rất đơn giản.
Đầu tiên, bạn cần truy cập server qua lệnh SSH sau:
ssh [email protected]
Mặt khác, nếu bạn đang chạy Linux trên máy tính riêng, chỉ cần mở terminal từ trong menu chính.
Trong trường hợp Debian, Ubuntu, Linux Mint và các biến thể của nó, thực thi lệnh sau:
sudo apt install screen

Nếu bạn đang dùng CentOS 7, bạn có thể cài đặt linux screen bằng lệnh sau:
sudo yum install screen
Hoặc nếu user đó hông thể chạy lệnh sudo, bạn cần chạy nó bằng user root.
yum install screen
Sau khi kết thúc cài đặt, bạn kiểm tra lại phiên bản vừa cài là gì bằng lệnh sau:
screen –version

Sau đó bạn đã có thể dùng nó rồi.
Sử dụng Linux Screen trên bất kỳ Linux Distribution nào
Để chạy Linux Screen, bạn cần gõ lệnh sau trong terminal session:
screen

Sau khi nhấn nút khoảng trắng, bạn sẽ thấy terminal lần nữa. Đừng lo, chúng ta đã bắt đầu một Screen session mới. Để thoát và kiểm tra lại, gõ exit và nhấn nút enter.

Bạn sẽ thấy thông báo screen bị tắt, để tạo mới session mới cứ vậy mà gõ screen:
screen
Chúng ta cũng có thể tạo mới session và gắn cho nó một cái tên với biến -S. Ví dụ:
screen -S session1
Chúng tôi khuyên bạn mô tả ngắn gọn cho session đó để dễ ghi nhớ bạn cần làm gì.
Screen sử dụng dòng lệnh để thực thi terminal multiplexing. Các lệnh này rất dễ học. Chúng đều bắt đầu với cấu trúc CTRL+* *, * là biến.

Làm việc trong một session mới
Trước khi tạo session mới, bạn nhớ học cách sử dụng trước. Có một số lệnh thông dụng mà bạn sẽ cần dùng là:
Lệnh | Mô tả |
CTRL+a c | Tạo cửa sổ mới |
CTRL+a ” | Liệt kê tất cả cửa sổ đã được tạo |
CTRL+a a | Với lệnh này, bạn có thể xóa CTRL+a. |
CTRL+a CTRL+d | Để session chạy. |
Bạn cũng có thể chia terminal screen ra. Ví dụ, sử dụng lệnh CTRL+a S để chia terminal theo chiều ngang.

Để thay đổi terminal tiếp theo, nhấn CTRL+a TAB. Để đóng nó, nhấn CTRL+a X.
Sức mạnh của Linux Screen
Một trong các ưu điểm của Screen là khả năng thực thi tác vụ và để nó chạy nền, để khi quay lại thì nó vẫn còn chạy. Đây là các ưu điểm vượt trội của server administrator.
Cho ví dụ này, chúng tôi sẽ dùng htop. Chúng ta có thể cài nó lên Debian, Ubuntu, Linux Mint cũng như các biến thể khác bằng lệnh sau:
sudo apt install htop
Đầu tiên chạy lệnh htop.

Tiếp theo, nhấn CTRL+a và CTRL+d để lưu thực thi của terminal. Sau đó, đóng tất cả session bằng cách gõ exit và nhấn enter.

Giờ, mở terminal mới lên và chạy:
screen -ls

Với lệnh này, chúng tao có thể hiển thị tất session “được lưu” và kết nối tới nó. Để làm vậy, chạy lệnh sau:
screen -r process_number
Trong trường hợp này nó là 4305.

Tóm lại
Việc biết cách sử dụng screen linux rất tốt, đặc biệt nếu bạn đang làm server administration. Bài viết này đã chỉ bạn một số lệnh cơ bản của screen linux và các bước cài đặt. Chúng tôi hy vọng nó hữu dụng cho bạn!
Nếu bạn muốn học các lệnh phức tạp hơn, chúng tôi khuyên bạn sử dụng bộ tài liệu chính thức để hỗ trợ. Bạn có từng sử dụng Screen Linux và cảm thấy nó thế nào? Hãy cho chúng tôi biết nhé.
Setting up HLS live streaming server using NGINX + nginx-rtmp-module on Ubuntu
By Thanh Tùng
Posted at tháng 1 13, 2020
Stream
No comments
1. Chuẩn bị
Thông tin về server:
OS: Ubuntu 16.04
RAM: 2GB
Cores: 2
Bandwidth: 1Gb/s
eth0: 192.168.100.197
Gateway: 192.168.100.1
NETWORK: 192.168.100.0/24
2. Cài đặt và cấu hình
2.1 Biên dịch NIGNX-RTMP
Cài đặt các trình biên dịch:
apt-get update -y
apt-get install build-essential libpcre3 libpcre3-dev libssl-dev git wget dpkg-dev zlib1g-dev unzip -y
Tải các gói cài đặt cần thiết:
Clone nginx-rtmp-module
1 | git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git |
Install nginx dependencies
1 | sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev |
Download nginx
Latest nginx can be downloaded from this page.
for example
Latest nginx can be downloaded from this page.
for example
nginx-1.10.1
can be downloaded from this link: http://nginx.org/download/nginx-1.10.1.tar.gz1 2 3 | wget http://nginx.org/download/nginx-1.10.1.tar.gz tar -xf nginx-1.10.1.tar.gz cd nginx-1.10.1 |
Compile nginx
1 2 3 | ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module make -j 1 sudo make install |
- Notice the
--add-module=../nginx-rtmp-module
argument, the path must point correctly to the cloned module - (Optional) replace -j 1 with the amount of cpu's on your computer to accelerate the compilation
2. Create nginx configuration file¶
rtmp module config
An application in nginx means an rtmp endpoint
Basic uri syntax:
We will be using
Basic uri syntax:
rtmp://nginx_host[:nginx_port]/app_name/stream_name
We will be using
stream
as our stream name so our endpoint will be: rtmp://localhost/show/stream
Which will later be available as http://localhost:8080/hls/stream.m3u8
For good HLS experience we recommend using 3 seconds fragments with 60 seconds playlist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | rtmp { server { listen 1935; # Listen on standard RTMP port chunk_size 4000; 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; } } } |
Note that the example points
You can change this to a different directory but make sure that nginx have write permissions.
/mnt/hls/
as the target path for the hls playlist and video files.You can change this to a different directory but make sure that nginx have write permissions.
http server config
Since HLS consists of static files, a simple http server can be set up with two additions, correct MIME types and CORS headers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | server { listen 8080; location /hls { # 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/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /mnt/; } } |
the complete nginx.conf
The default location for nginx conf is
/usr/local/nginx/conf/nginx.conf
or /etc/nginx/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | worker_processes auto; events { worker_connections 1024; } # RTMP configuration rtmp { server { listen 1935; # Listen on standard RTMP port chunk_size 4000; 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/; } } } |
3. Start nginx¶
The nginx binary is located wherever you compiled it to -
/usr/local/nginx/sbin/nginx
by default. Change it to reflect your path.
Test the configuration file
1 | /usr/local/nginx/sbin/nginx -t |
Start nginx in the background
1 | /usr/local/nginx/sbin/nginx |
Start nginx in the foreground
1 | /usr/local/nginx/sbin/nginx -g 'daemon off;' |
Reload the config on the go
1 | /usr/local/nginx/sbin/nginx -t && nginx -s reload |
Kill nginx
1 | /usr/local/nginx/sbin/nginx -s stop |
4. Pushing live stream to nginx using rtmp¶
nginx accepts rtmp stream as input. For a proper HLS stream the video codec should be
x264
and audio codec aac
/mp3
/ac3
most commonly being aac
.Options 1: From existing rtmp stream already in h264¶
if you have an existing rtmp stream in the correct codec, you can skip ffmpeg and tell nginx to pull the stream directly. In order to do so add a
pull
directive under application
section in nginx.conf like so:1 2 3 4 5 6 7 8 | application show { live on; pull rtmp://example.com:4567/sports/channel3 live=1; # to change the local stream name use this syntax: ... live=1 name=ch3; # other directives... # hls_... } |
Read on more available options here
Options 2: From local webcam/different rtmp/file¶
To achieve the stream encoding and muxing we will use the almighty ffmpeg.
To install ffmpeg using PPA run these commands
1 2 3 | sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install ffmpeg |
There are several source from which you can produce an rtmp stream. here are couple examples: Update
localhost
to your nginx server ip/domain
Capture webcam on
/dev/video0
and stream it to nginx1 | ffmpeg -re -f video4linux2 -i /dev/video0 -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -f flv rtmp://localhost/show/stream |
-re
- consume stream on media's native bitrate (and not as fast as possible)-f
- use video4linux2 plugin-i
- select physical device to capture from-vcodec
- specify video codec to output-vprofile
- use x264 baseline profile-acodec
- use aac audio codec-strict
- allow using the experimental aac codec-f
- specify format to outputrtmp://localhost/show/stream
- rtmp endpoint to stream to. if the target port is not1935
is should be included in the uri.
The last path component is the stream name - that means that multiple channels can be pushed using different names
Stream file example-vid.mp4
1 | ffmpeg -re -i example-vid.mp4 -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 -f flv rtmp://localhost/show/stream |
Stream another rtmp stream
1 | ffmpeg -i rtmp://example.com/appname/streamname -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -f flv rtmp://localhost/show/stream |
5. Take the server for a test run!¶
Now that we are pushing our stream into nginx, a manifest file in the format
stream-name.m3u8
is created in the target folder along with the video fragments.
For our example, the manifest is available at:
http://localhost:8080/hls/stream.m3u8
.
For testing our new HLS live stream we will use videojs5.
player.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Live Streaming</title> <link href="//vjs.zencdn.net/5.8/video-js.min.css" rel="stylesheet"> <script src="//vjs.zencdn.net/5.8/video.min.js"></script> </head> <body> <video id="player" class="video-js vjs-default-skin" height="360" width="640" controls preload="none"> <source src="http://localhost:8080/hls/stream.m3u8" type="application/x-mpegURL" /> </video> <script> var player = videojs('#player'); </script> </body> </html> |
With Peer5 plugin (get your Customer ID here):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JW7 Player test</title> <!-- peer5 client library --> <script src="//api.peer5.com/peer5.js?id=YOUR_PEER5_CUSTOMER_ID"></script> <!-- videojs5 scripts - You can change to your self hosted scripts --> <link rel="stylesheet" href="//api.peer5.com/videojs5/assets/video-js.min.css"> <script src="//api.peer5.com/videojs5.js"></script> <!-- peer5 plugin for videojs5 --> <script src="//api.peer5.com/peer5.videojs5.hlsjs.plugin.js"></script> </head> <body> <video id="player" class="video-js vjs-default-skin" height="360" width="640" controls preload="none"> <source src="http://localhost:8080/hls/stream.m3u8" type="application/x-mpegURL" /> </video> <script> var player = videojs('#player'); </script> </body> </html> |
It's alive!
Suu tam