前言:
百度查了很多nginx配置,可能是因爲自己理解有問題,一直配置不成功,發現國外一個帖子寫的很詳細,按照步驟配置下來沒有任何問題,特此翻譯留存一下
Nginx RTMP是一種基於Nginx的流媒體播放器,具有RTMP、HLS和MPEG-DASH直播流。Nginx RTMP模塊具有很多功能,包括H264/AAC支持、HTTP回調支持、HTTP音視頻錄製控制模塊等。
在本教程中,我將向您展示如何使用RTMP模塊安裝和配置Nginx web服務器。我們將使用最新的CentOS 7服務器,本指南將包括nginx和rtmp模塊的配置以及創建rtmp實時流和視頻點播。
前提要求:
- CentOS 7 Server
- Root 權限
- ps:本人配置爲阿里雲ECS
下面我們將要做的內容:
- 安裝依賴庫
- 下載Nginx附加包和RTMP模塊
- 編譯Nginx並從源代碼安裝
- 將Nginx配置爲服務
- 配置Nginx RTMP模塊
- 開啓第一個RTMP實時流
- 測試
第一步:安裝依賴庫
在本教程中,我們將從源代碼構建Nginx web服務器。我們需要安裝所需的所有包依賴項,包括開發工具、EPEL存儲庫和其他包。
安裝CentOS“開發包”
sudo yum -y groupinstall 'Development Tools'
添加EPEL庫
sudo yum -y install epel-release
安裝Nginx依賴
sudo yum install -y wget git unzip perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel pcre-devel GeoIP GeoIP-devel
等待安裝完成
第二步:下載Nginx附加包和RTMP模塊
在這一步中,我們將下載nginx源代碼以及其他依賴項,包括pcre、zlib和OpenSSL。
進入“/usr/local/src”目錄
cd /usr/local/src
下載 Nginx 1.14.0 並解壓
wget https://nginx.org/download/nginx-1.14.0.tar.gz
tar -xzvf nginx-1.14.0.tar.gz
下載pcre包並解壓
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
unzip pcre-8.42.zip
下載 zlib包並解壓
wget https://www.zlib.net/zlib-1.2.11.tar.gz
tar -xzvf zlib-1.2.11.tar.gz
下載OpenSSL 包並解壓
wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
tar -xzvf openssl-1.1.0h.tar.gz
克隆rtmp模塊
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
刪除所有無用 tar.gz 和.zip 文件
rm -f *.tar.gz *.zip
查看當前目錄詳細內容
ls -lah
如下圖
如上圖,則我們已經做好編譯和安裝的準備了
第三步:編譯Nginx並從源代碼安裝
進入到 nginx-1.14.0文件夾
cd nginx-1.14.0/
實用下面參數配置 nginx 1.14.0
./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.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
如圖所示
編譯並安裝
sudo make
sudo make install
安裝完成後,我們需要創建一個新的symlink模塊目錄,創建一個新的nginx用戶和組,並創建一個新的nginx cache 目錄。
創建nginx symlink模塊到 ‘/etc/nginx’ 目錄
sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules
創建新的 ‘nginx’ 用戶組和用戶
sudo useradd -r -d /var/cache/nginx/ -s /sbin/nologin -U nginx
在 ‘/var/cache/nginx’ 創建一個新的Nginx cache 目錄並把所有權賦予 ‘nginx’ 用戶組
mkdir -p /var/cache/nginx/
chown -R nginx:nginx /var/cache/nginx/
測試 nginx 配置,查看nginx版本
nginx -t
nginx -V
顯示如下結果,說明正常
至此,Nginx服務器以及RTMP模塊已經被成功的安裝到CentOS 7 中。
第四步:將Nginx配置爲服務
在這一小節中,我們將把Nginx設置爲服務,需要在系統服務文件夾中創建nginx服務文件
進入到 ‘/lib/systemd/system’ 文件夾,用vim 命令創建 ‘nginx.service’
cd /lib/systemd/system/
vim nginx.service
將下面內容填充進去(Tips:貼完注意查看一遍,有時候最開始部分會少一些字符,不容易被檢查出來)
[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
保存並退出
重新加載系統文件(Tips:我在這一步出現了問題,該命令無效果,我這邊開啓不了服務,後來重啓之後纔有用)
systemctl daemon-reload
開啓服務,並設置開機啓動
systemctl start nginx
systemctl enable nginx
如下圖,在CentOS7 中Nginx作爲服務開啓
第五步:配置Nginx RTMP模塊
這一步我們將爲RTMP模塊創建一個額外的Nginx配置文件
進入’/etc/nginx’ 文件夾,備份該文件夾中的’nginx.conf’ 文件
cd /etc/nginx/
mv nginx.conf nginx.conf.asli
現在創建新的 ‘nginx.conf’
vim nginx.conf
將下面文字填充進去
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/;
}
}
}
保存並退出
接下來,我們需要爲HLS配置創建一個新目錄,在上面的配置中我們已經定義了web根目錄下的’/mnt’文件夾。
在“/mnt”目錄下創建“hls”目錄,並將該目錄的所有者更改爲nginx用戶和組。
mkdir -p /mnt/hls
chown -R nginx:nginx /mnt/hls
測試配置並重啓nginx服務
nginx -t
systemctl restart nginx
如下圖所示
第六步:開啓第一個RTMP實時流
在這一步,我們將使用服務器上的mp4視頻創建新的RTMP流視頻點播,並創建一個新的實時RTMP流,之後使用OBS軟件從本地計算機播放。
進入 '/etc/nginx’文件夾並編輯配置文件 ‘nginx.conf’
cd /etc/nginx/
vim nginx.conf
在’rtmp { … }'範圍內,編輯如下文字
# RTMP video on demand for mp4 files
application vod {
play /mnt/mp4s;
}
# RTMP stream using OBS
application stream {
live on;
}
保存並退出
創建 'mp4s’文件夾並修改所有權限
mkdir -p /mnt/mp4s
chown -R nginx:nginx /mnt/mp4s
測試nginx服務,確保沒有任何錯誤,之後重啓nginx 服務
nginx -t
systemctl restart nginx
至此,完成Nginx服務器對於RTMP實時流和vod流的配置
重要:至此基本配置已經完成,需要注意的是,本人使用的是阿里雲ECS服務器,在端口控制方面有特殊要求
1、配置本機端口(上面配置中用到了8080端口和1935端口)
#開啓端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=1935/tcp --permanent
#重啓服務
firewall-cmd --reload
#查看開放的端口
firewall-cmd --list-ports
2、配置阿里雲ECS安全規則
第七步:測試
1、安裝vlc軟件
2、打開“媒體”,選擇“打開網絡串流”,輸入RTMP流媒體地址
Tips:xxx.xxx.xxx.xxx爲你的服務器地址
media1.mp4需要預先上傳到你服務器/mnt/mp4s文件夾
rtmp://xxx.xxx.xxx.xxx:1935/vod/media1.mp4
如果沒什麼問題,你應該就可以看到視頻流內容了
後記:
翻譯網址:(https://www.howtoforge.com/tutorial/how-to-install-nginx-with-rtmp-module-on-centos/)
參考網址:(https://github.com/sergey-dryabzhinsky/nginx-rtmp-module)