1. 程式人生 > >Linux nginx+rtmp伺服器配置

Linux nginx+rtmp伺服器配置

nginx伺服器

nginx伺服器是開源的高效能的http伺服器,反向代理伺服器,是俄羅斯的大佬開發的,在國內許多公司應用比較多,具體看百科!

反向代理伺服器:代理伺服器接收internet請求,然後改代理伺服器將請求轉向內部的伺服器,內部伺服器將處理的結果返回代理伺服器,這個代理伺服器就為反向代理伺服器!

rtmp

實時訊息傳輸協議(real time messaging protocol),主要應用在流媒體傳輸,實時音視訊通訊的協議!

nginx伺服器與rtmp mudle

nginx伺服器支援許多功能模組,比如http–mudle,rtmp–mudle等等,不同的mudle佔用不同的埠,通過埠,客戶端就可以與nginx的mudle進行通訊!我們對音視訊通過rtmp協議進行推流到nginx伺服器,所以需要安裝nginx支援的rtmp–mudle!

安裝配置

開始我是在redhat版本上進行安裝的,遇到的許多問題:
    紅帽redhat沒有apt-get命令(ubantu),使用yum一直提示沒有可用的資源,就安裝不了編譯依賴工具
    去github下載指令碼的時候,不知道什麼原因也一直down不下來!
於是去下載apt-get,瞎搞了半天,沒有成功,就這樣前前後後折騰了幾天,實在不想折騰了,就去阿里雲買了一個低配置的雲主機,一個月45左右,接下來環境配置一路暢通!

1.下載nginx,rtmp支援的編譯依賴工具

    sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

2.下載nginx,以及rtmp原始碼,然後解壓

wget http://nginx.org/download/nginx-1.8.1.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

3.進行解壓後的ningx目錄,執行configue檔案

新增http,rmpt模組
./configure --with-http_ssl_module --add-module=../nginx-
rtmp-module-master

進行一些檔案檢測
checking for OS
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found      
checking for OpenSSL library ... found
checking for zlib library ... found
產生MakeFile檔案
creating objs/Makefile

Configuration summary
using system PCRE library
using system OpenSSL library
md5: using OpenSSL library
sha1: using OpenSSL library
using system zlib library
nginx安裝目錄:/usr/local/nginx
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

4.編譯MakeFile:make

make


產生一些臨時檔案
make[1]: Entering directory `/usr/stay/nginx-1.8.1'
src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I../nginx-rtmp-module-master -I s
rc/core -I src/event -I src/event/modules -I src/os/unix -I objs \      -o objs/src/core/ngx_log.o \
        src/core/ngx_log.c
................

c -o objs/nginx \
    objs/src/core/nginx.o \
    objs/src/core/ngx_log.o \
    objs/src/event/ngx_event_posted.o \
    objs/src/event/ngx_event_accept.o 


    ake[1]: Leaving directory `/usr/stay/nginx-1.8.1'
make -f objs/Makefile manpage
make[1]: Entering directory `/usr/stay/nginx-1.8.1'
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/usr/stay/nginx-1.8.1'

5.編譯完成之後,進行安裝:make install

make install

進行檔案的拷貝,到/usr/local/nginx
make[1]: Entering directory `/usr/stay/nginx-1.8.1'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin'         || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx'         || mv '/usr/local/nginx/sbin/nginx'             '
/usr/local/nginx/sbin/nginx.old'cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf'         || mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types'      || cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params'      || cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params      '/usr/local/nginx/conf/fastcgi_params.default'      
test -f '/usr/local/nginx/conf/nginx.conf'      || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.co
nf'cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' ||      mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/stay/nginx-1.8.1'

6.設定開機啟動

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

7.啟動nginx服務

sudo service nginx start
sudo service nginx stop

這樣,伺服器配置已經完成,接下來需要進行測試,看是否推流成功!

8.下載ffmpeg原始碼,並安裝(ffmpeg 命令檢視是否安裝成功)

    make
    make install

9.配置 nginx-rtmp 伺服器,然後sudo service nginx restart重啟
在開啟 /usr/local/nginx/conf/nginx.conf,在末尾新增如下配置:

rtmp {
server {
        listen 1935;//監聽埠
        chunk_size 4096;

        application live {
                live on;
                record off;
                exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
        }
        application live360p {
                live on;
                record off;
    }
}
}

10.進行推流
推流使用obs,ums或者github上的一些demo都可以!

11.在下載的windows版本的ffmpeg的bin ffplay

ffplay rtmp://120.77.182.106:1935/live/test