nginx-rtmp服务器搭建教程

1,准备ubuntu机器或者虚拟机,本教程采用的镜像是:ubuntu-16.04.4-desktop-amd64.iso
2,nginx依赖以下模块:
    1,rewrite模块需要 pcre 库
        官网:http://www.pcre.org/
        下载文件的链接:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.bz2
    2,ssl 功能需要 openssl 库
        官网:https://www.openssl.org/source/
        下载文件的链接:https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz
    3,gzip模块需要 zlib 库
        官网:http://www.zlib.net/
        下载文件的链接:http://www.zlib.net/zlib-1.2.11.tar.gz
3,nginx
    官网:http://nginx.org/
    下载地址:http://nginx.org/download/nginx-1.15.2.tar.gz
4,rtmp模块
    下载地址:https://github.com/arut/nginx-rtmp-module
5,文件解压:所有下载的文件放到所有下载的文件放到 ~/nginx
    tar -zxf nginx-1.15.2.tar.gz
    unzip nginx-rtmp-module-master.zip
    tar -zxf openssl-1.1.1-pre8.tar.gz
    tar -xjf pcre-8.42.tar.bz2
    tar -zxf zlib-1.2.11.tar.gz
6,安装:
    cd nginx-1.15.2
    ./configure \
        --with-http_ssl_module \
        --with-http_stub_status_module \
        --add-module=../nginx-rtmp-module-master \
        --with-openssl=../openssl-1.1.1-pre8 \
        --with-pcre=../pcre-8.42 \
        --with-zlib=../zlib-1.2.11
    make
    sudo make install
7,启动:
    sudo /usr/local/nginx/sbin/nginx
    检查是否启动成功,浏览器打开(我的ip是192.168.1.238):http://192.168.1.238/
    如果需要开机自启动,把命令添加到 /etc/rc.local。
        sudo gedit /etc/rc.local
8,配置nginx使用RTMP:
    配置前的准备:
        sudo mkdir /HLS
        sudo mkdir /HLS/mobile
        sudo mkdir /HLS/live
        sudo mkdir /video_recordings
        sudo chmod -R 777  /video_recordings
    配置文件路径:/usr/local/nginx/conf/nginx.conf
    cd /usr/local/nginx/conf/
    sudo cp nginx.conf nginx.conf.bak
    sudo gedit nginx.conf

        #下面的配置文件的内容:
        worker_processes 1;
        error_log logs/error.log debug;
        events {
            worker_connections 1024;
        }

        rtmp {
            server {
                listen 1935;
                allow play all;

                #creates our "live" full-resolution HLS videostream from our incoming encoder stream and tells where to put the HLS video manifest and video fragments
                application live {
                    allow play all;
                    live on;
                    record all;
                    record_path /video_recordings;
                    record_unique on;
                    hls on;
                    hls_nested on;
                    hls_path /HLS/live;
                    hls_fragment 10s;
                }
                #creates our "mobile" lower-resolution HLS videostream from the ffmpeg-created stream and tells where to put the HLS video manifest and video fragments
                application mobile {
                    allow play all;
                    live on;
                    hls on;
                    hls_nested on;
                    hls_path /HLS/mobile;
                    hls_fragment 10s;
                }

                #allows you to play your recordings of your live streams using a URL like "rtmp://my-ip:1935/vod/filename.flv"
                application vod {
                    play /video_recordings;
                }
            }
        }

        http {
            include mime.types;
            default_type application/octet-stream;

            server {
                listen 80;
                server_name localhost;

                #creates the http-location for our full-resolution (desktop) HLS stream - "http://my-ip/live/my-stream-key/index.m3u8" 
                location /live {
                    types {
                        application/vnd.apple.mpegurl m3u8;
                    }
                    alias /HLS/live;
                    add_header Cache-Control no-cache;
                }

                #creates the http-location for our mobile-device HLS stream - "http://my-ip/mobile/my-stream-key/index.m3u8" 
                location /mobile {
                    types {
                        application/vnd.apple.mpegurl m3u8;
                    }
                    alias /HLS/mobile;
                    add_header Cache-Control no-cache;
                }

                #allows us to see how stats on viewers on our Nginx site using a URL like: "http://my-ip/stats" 
                location /stats {
                    stub_status;
                }

                #allows us to host some webpages which can show our videos: "http://my-ip/my-page.html" 
                location / {
                    root html;
                    index index.html index.htm;
                } 
            }
        }
9,重新启动nginx:
    sudo /usr/local/nginx/sbin/nginx -s stop
    sudo /usr/local/nginx/sbin/nginx
10,测试是否可以推流:
    工具:ffmpeg
    官网:http://ffmpeg.org/
    下载地址:https://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-20180811-00ae349-win64-static.zip
    推流命令:ffmpeg -re -i 视频名称.mp4 -vcodec libx264 -acodec aac -s 800x600 -f flv rtmp://192.168.1.238/mobile/test1
        需要准备一个mp4视频,放到bin目录下。
    随便找一个可以播放rtmp的播放器,播放:rtmp://192.168.1.238/mobile/test1
11,over。

 

资源打包下载,传送门:https://download.csdn.net/download/tianjin11/10601202

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值