本文分享自華為云社區《華為云ECS服務器安裝centos7.4鏡像,部署GINX服務器、搭建物聯網視頻監控系統》,作者:DS小龍哥。
在CentOS7.4服務器版本的環境下安裝Nginx服務器、配置文件服務器、流媒體服務器。
(1)配置NGINX為HTTP服務器,安裝rtmp模塊,完成rtmp視頻推流,支持緩存視頻到本地目錄、支持轉為HLS流,通過瀏覽器訪問直播流。
(2)部署開機自動啟動程序:方便設置自己的程序為開機啟動。
1. 環境介紹
環境介紹: 采用的是華為云的ECS彈性云服務器–鏡像安裝的CentOS7.4 64位 -----是服務器版,非桌面版哦。
在CentOS7.4服務器版本的環境下安裝nginx服務器、配置文件服務器、流媒體服務器。
(1)配置NGINX為HTTP服務器,安裝rtmp模塊,完成rtmp視頻推流,支持緩存視頻到本地目錄、支持轉為HLS流,通過瀏覽器訪問直播流。
(2)部署開機自動啟動程序:方便設置自己的程序為開機啟動。
2. Centos中安裝帶rtmp模塊的Nginx
2.1 新安裝的系統可以先安裝一些工具
yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc gcc-c++
yum install -y vim wget lsof git zip unzip
2.2 獲取Nginx二進制源碼
聽說srtmp模塊暫時只支持Nginx13-15版本,當前就在官網下載Nginx14
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar xvf nginx-1.14.2.tar.gz
2.3 獲取nginx-rtmp-module
wget https://github.com/arut/nginx-rtmp-module/archive/refs/tags/v1.2.1.tar.gz
tar xvf v1.2.1.tar.gz
2.4 編譯nginx
cd nginx-1.14.2
./configure --add-module=../nginx-rtmp-module-1.2.1/ --with-http_ssl_module
make && make install
#建立軟鏈接
ln -s /usr/local/nginx/sbin/nginx /usr/bin
特別說明:
如果在配置時報錯,一般就是缺東西了,安裝了再配置。
比如:報錯 ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the mo ....
解決:
yum -y install pcre-devel
yum -y install openssl openssl-devel
2.5 修改Nginx的配置文件
打開
/usr/local/nginx/conf/nginx.conf文件,在文件最后面加入下面的配置。
rtmp {
server {
listen 8888;
Application live {
live on;
}
}
}
上面8888是rtmp推流和拉流的端口。
修改nginx.conf之后,重啟nginx服務:
sudo service nginx restart
重啟服務之后,使?.NETstat -ltn命令查看TCP監聽的端口,確認下Nginx的監聽端口是否正常。
正常情況,一個是我們自己設置的rtmp服務監聽端口8888,還有一個80是Nginx默認的HTTP服務監聽端口。
接下來可以在瀏覽器里輸入本機IP地址:http://127.0.0.1/,查看Nginx服務開啟狀態。
2.6 設置開機啟動
wget http://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
chmod +x /etc/init.d/nginx
update-rc.d nginx defaults
2.7 控制nginx服務的3個命令: 啟動、停止、重啟
service nginx start
service nginx stop
service nginx restart 或者 nginx -s reload (運行中生效配置文件)
2.8 進行rtmp推流
服務器搭建好之后,推流和拉流的地址就是: rtmp://<服務器IP地址>:8888/live/<推流存放的目錄>
例如: rtmp://127.0.0.1:8888/live/xl
2.9 nginx保存推流視頻文件
如果需要讓推流上來的文件保存下來后續進行查看歷史文件,可以配置nginx進行保存。
在原來的
/usr/local/nginx/conf/nginx.conf配置文件里rtmp模塊中增加新的配置:
record all;
record_unique on;
record_path "./video"; #視頻緩存的路徑
record_suffix -%Y-%m-%d-%H_%M_%S.flv;
完整
/usr/local/nginx/conf/nginx.conf里的rtmp模塊的配置如下:
#RTMP服務
rtmp {
server {
listen 8888;
application live {
live on; #開啟實時
record all;
record_unique on;
record_path "./video"; #視頻緩存的路徑
record_suffix -%Y-%m-%d-%H_%M_%S.flv;
}
}
}
配置之后執行命令nginx -s reload重啟服務器即可。
2.10 rtmp直播流轉為hls直播流
什么是HLS直播流?
HLS 全稱是 HTTP Live Streaming,是一個由 Apple 公司提出的基于 HTTP 的媒體流傳輸協議,用于實時音視頻流的傳輸。目前HLS協議被廣泛的應用于視頻點播和直播領域。
原理介紹
HLS 跟 DASH 協議的原理非常類似。通過將整條流切割成一個小的可以通過 HTTP 下載的媒體文件,然后提供一個配套的媒體列表文件,提供給客戶端,讓客戶端順序地拉取這些媒體文件播放,來實現看上去是在播放一條流的效果。由于傳輸層協議只需要標準的 HTTP 協議,HLS 可以方便的透過防火墻或者代理服務器,而且可以很方便的利用 CDN 進行分發加速,并且客戶端實現起來也很方便。
HLS 把整個流分成一個個小的基于 HTTP 的文件來下載,每次只下載一些。HLS 協議由三部分組成:HTTP、M3U8、TS。這三部分中,HTTP 是傳輸協議,M3U8 是索引文件,TS 是音視頻的媒體信息。
HLS協議編碼格式要求:
視頻的編碼格式:H264
音頻的編碼格式:AAC、MP3、AC-3
視頻的封裝格式:ts
保存 ts 索引的 m3u8 文件
配置
/usr/local/nginx/conf/nginx.conf將RTMP流轉為HLS流。
在http模塊的server配置里增加新的配置:
location /live_hls{
types {
#m3u8 type設置
application/vnd.apple.mpegurl m3u8;
#ts分片文件設置
video/mp2t ts;
}
#指向訪問m3u8文件目錄
alias ./m3u8File; #和rtmp模塊里的hls_path設置路徑一樣
add_header Cache-Control no-cache; #禁止緩存
}
在rtmp模塊的server配置里增加新的配置:
hls on; #開啟hls
hls_path ./m3u8File; #hls的ts切片存放路徑 (這是個目錄,會自動創建的)
hls_fragment 2s; #本地切片長度
hls_playlist_length 6s;#HLS播放列表長度
/usr/local/nginx/conf/nginx.conf文件的完整的配置如下:
worker_processes 1; #Nginx進程數,建議設置為等于CPU總核數
events {
worker_connections 1024; #工作模式與連接數上限
}
rtmp_auto_push on;
#RTMP服務
rtmp {
server {
listen 8888;
application live {
live on; #開啟實時
record all;
record_unique on;
record_path "./video"; #視頻緩存的路徑
record_suffix -%Y-%m-%d-%H_%M_%S.flv;
hls on; #開啟hls
hls_path ./m3u8File; #hls的ts切片存放路徑
hls_fragment 2s; #本地切片長度
hls_playlist_length 6s;#HLS播放列表長度
}
}
}
#HTTP服務
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8099;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /live_hls{
types{
#m3u8 type設置
application/vnd.apple.mpegurl m3u8;
#ts分片文件設置
video/mp2t ts;
}
#指向訪問m3u8文件目錄
alias ./m3u8File;
add_header Cache-Control no-cache; #禁止緩存
}
location /control{
rtmp_control all;
}
location /stat{
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl{
root ./nginx-rtmp-module-master;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置之后重啟服務器即可。
按照前面的配置,RTMP推流地址和HTTP訪問地址如下:
RTMP推流和拉流地址: rtmp://127.0.0.1:8888/live/video01
那么對應的HTTP的訪問地址:http://127.0.0.1:8099/live_hls/video01.m3u8
說明: 轉為HLS流之后,如果瀏覽器支持HLS流就可以直接輸入地址播放。一般手機瀏覽器都支持的。比如:蘋果手機的自帶瀏覽器,QQ瀏覽器等瀏覽器都支持直接播放HLS流。PC機的谷歌瀏覽器默認是不支持的。
2.11 NGINX配置HTTP文件服務器
在5.8小節里介紹了如何配置NGINX保留RTMP推流的視頻文件,如果想做一個直播回放,歷史記錄查看的播放器,那么就可以將rtmp視頻緩存的目錄作為HTTP文件服務器訪問的根目錄,通過訪問這個根目錄獲取目錄下文件的索引,得到視頻文件的訪問地址就可以直接進行播放,就能做一個視頻回放播放器。
在http模塊里新增加一個server配置,并填入新的配置,詳細內容如下:
server {
listen 8090;
server_name localhost;
location / {
root ./video; #指定哪個目錄作為Http文件服務器的根目錄,如果你這里寫了file就是你的根目錄,那么訪問的時候file就不會出現在目錄中
autoindex on; #設置允許列出整個目錄
autoindex_exact_size off; #默認為on,顯示出文件的確切大小,單位是bytes。改為off后,顯示出文件的大概大小,單位是kB或者MB或者GB
autoindex_localtime on; #默認為off,顯示的文件時間為GMT時間。改為on后,顯示的文件時間為文件的服務器時間
charset utf-8; #防止文件亂碼顯示, 如果用utf-8還是亂碼,就改成gbk試試
}
}
特別說明: nginx是支持配置多個server配置,監聽不同的端口,可以給文件服務器單獨設置一個監聽端口,專門作為文件遍歷使用。
/usr/local/nginx/conf/nginx.conf文件的完整的配置如下:
worker_processes 1; #Nginx進程數,建議設置為等于CPU總核數
events {
worker_connections 1024; #工作模式與連接數上限
}
rtmp_auto_push on;
#RTMP服務
rtmp {
server {
listen 8888;
application live {
live on; #開啟實時
record all;
record_unique on;
record_path "./video"; #視頻緩存的路徑
record_suffix -%Y-%m-%d-%H_%M_%S.flv;
hls on; #開啟hls
hls_path ./m3u8File; #hls的ts切片存放路徑
hls_fragment 2s; #本地切片長度
hls_playlist_length 6s;#HLS播放列表長度
}
}
}
#HTTP服務
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8090;
server_name localhost;
location / {
root ./video; #指定哪個目錄作為Http文件服務器的根目錄,如果你這里寫了file就是你的根目錄,那么訪問的時候file就不會出現在目錄中
autoindex on; #設置允許列出整個目錄
autoindex_exact_size off; #默認為on,顯示出文件的確切大小,單位是bytes。改為off后,顯示出文件的大概大小,單位是kB或者MB或者GB
autoindex_localtime on; #默認為off,顯示的文件時間為GMT時間。改為on后,顯示的文件時間為文件的服務器時間
charset utf-8; #防止文件亂碼顯示, 如果用utf-8還是亂碼,就改成gbk試試
}
}
server {
listen 8099;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /live_hls{
types{
#m3u8 type設置
application/vnd.apple.mpegurl m3u8;
#ts分片文件設置
video/mp2t ts;
}
#指向訪問m3u8文件目錄
alias ./m3u8File;
add_header Cache-Control no-cache; #禁止緩存
}
location /control{
rtmp_control all;
}
location /stat{
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl{
root ./nginx-rtmp-module-master;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
訪問文件測試: http://127.0.0.1:8090
3. linux下開機啟動的執行流程
3.1 開機執行腳本順序
第一步:init /etc/inittab
第二步:啟動相應的腳本,并且打開終端
rc.sysinit
rc.d(里面的腳本)
rc.local
第三步:啟動login登錄界面 login
第四步:在用戶登錄的時候執行sh腳本的順序,每次登錄的時候都會完全執行的
/etc/profile.d/file
/etc/profile
/etc/bashrc
/root/.bashrc
/root/.bash_profile
3.2 Linux中修改環境變量及生效方法
修改/etc/profile 或者 /etc/bashrc 可以讓環境變量全部用戶全局生效(需要重啟系統)。
修改~/.bash_profile 或~/.bashrc對當前用戶全局有效(需要重啟系統)。
如果需要立即生效,修改完之后用source命令執行,如:
source .bash_profile
3.3 rcX.d的啟動級別
一般有開機自啟動的需求時,一般會在/etc/rc.local文件中寫命令行或腳本執行命令的方式來實現。也可以在/etc/profile文件里實現(不建議)。
現在很多Linux發行版,默認是沒有/etc/rc.local這個文件或者沒有去執行,而使用的是/etc/rcX.d。
rcX.d并不是指這個目錄或者文件就是叫rcX.d,這其中的X對應是0~6這7個數字,不同的數字對應著不同的級別
查看當前系統/etc/rcX.d目錄:
[root@ecs-c687-ecrs work]# ls /etc/ | grep rc
bashrc
csh.cshrc
inputrc
mail.rc
rc0.d
rc1.d
rc2.d
rc3.d
rc4.d
rc5.d
rc6.d
rc.d
rc.local
vimrc
virc
wgetrc
通過runlevel命令查看當前系統的啟動級別:
我當前使用的是CentOS7.4服務器版本,啟動級別如下:
[root@ecs-c687-ecrs ]# runlevel
N 3
查看/etc/rc3.d/目錄下文件的詳細信息:
[root@ecs-c687-ecrs ~]# ls /etc/rc3.d/ -l
total 0
lrwxrwxrwx. 1 root root 20 Feb 14 2022 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 17 Feb 14 2022 K90network -> ../init.d/network
lrwxrwxrwx 1 root root 19 Sep 15 22:07 S12hostguard -> ../init.d/hostguard
lrwxrwxrwx 1 root root 24 Feb 14 2022 S50multi-queue-hw -> ../init.d/multi-queue-hw
可以看到該目錄下的文件都是鏈接文件,而且都是指向/etc/init.d中的shell腳本或者其他可執行文件,它們的命名方式基本都是以S或者K開頭,其后緊跟一個數字,數字后則是鏈接文件的名字,這個名字可以自行定義。
命名規則解釋如下:
以K90network為例:
K表示stop,S表示start。(表示需要傳入參數),也就是說開機自啟動命令會向腳本傳入start或者stop,在腳本里可以收到參數$1進行一些判斷,完成一些不同情況下的邏輯處理。比如:開機執行什么代碼,關機執行什么代碼。
90 表示腳本執行等級。(通常越小越優先)
network與/etc/init.d下的腳本文件名稱保持一致。
3.4 利用rcX.d實現開機自動執行腳本
比如:需求是開機之后創建一個文件,并向文件里存放一些數據。
(1)先在/etc/init.d目錄下創建一個up_demo.sh腳本,編寫腳本代碼:
#!/bin/bash
echo $0 $1 >> /home/up_test.txt
修改腳本權限:
[root@ecs-c687-ecrs init.d]# chmod 777 /etc/init.d/up_demo.sh
[root@ecs-c687-ecrs init.d]# ls up_demo.sh -l
-rwxrwxrwx 1 root root 76 Sep 16 14:13 up_demo.sh
(2) 在/etc/rc3.d目錄里,創建軟連接。 (因為我的系統啟動級別為3)
[root@ecs-c687-ecrs rc3.d]# ln -s /etc/init.d/up_demo.sh S10up_demo
[root@ecs-c687-ecrs rc3.d]# ls -l
total 0
lrwxrwxrwx. 1 root root 20 Feb 14 2022 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 17 Feb 14 2022 K90network -> ../init.d/network
lrwxrwxrwx 1 root root 22 Sep 16 14:17 S10up_demo -> /etc/init.d/up_demo.sh
lrwxrwxrwx 1 root root 19 Sep 15 22:07 S12hostguard -> ../init.d/hostguard
lrwxrwxrwx 1 root root 24 Feb 14 2022 S50multi-queue-hw -> ../init.d/multi-queue-hw
(3)重啟系統,進入到/home目錄下查看文件內容,可以看到開機啟動成功,內容已經寫到up_test.txt文件里了。
Welcome to Huawei Cloud Service
[root@ecs-c687-ecrs ~]# cd /home/
[root@ecs-c687-ecrs home]# ls
lib_run.sh up_test.txt video work work_pc work.tar.gz
[root@ecs-c687-ecrs home]# cat up_test.txt
/etc/rc.d/init.d/up_demo.sh start
[root@ecs-c687-ecrs home]#
關注我,第一時間了解華為云新鮮技術~