Ubuntu上部署nginx RTMP直播服务

1 源代码下载

创建存放待编译源代码的目录:

$ mkdir nginx-src

进入该目录,以获取相关代码:

  $ cd nginx-src

获取nginx源代码供编译:

$ git clone https://github.com/nginx/nginx.git

(也可通过wget或其它方式直接下载该代码,下同,如访问github较慢,可使用gitee镜像,git clone https://gitee.com/mirrors/nginx.git)

获取nginx-rtmp-module:

​
$ git clone GitHub - arut/nginx-rtmp-module: NGINX-based Media Streaming Server

或gitee镜像:

git clone https://gitee.com/mirrors/nginx-rtmp-module.git

获取pcre,本例中为8.44版:

$ wget ftp://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz

解压pcre:

$ tar -xzvf pcre-8.44.tar.gz

注意在本例中pcre需要解压后处理,否则用make过的包的话会出现timestamp问题

pcre也可以通过sudo apt install方式安装:

$ sudo apt install libpcre3 libpcre3-dev

2 编译安装

进入nginx目录,编辑cfg.sh文件,配置nginx功能模块。

$ cd nginx

将configure的命令封装成脚本

$ vim cfg.sh
#  cfg.sh文件的内容

auto/configure --prefix=/usr/local/nginx \

      --with-pcre=../pcre-8.44 \

              --with-http_ssl_module \

              --with-http_v2_module \

              --with-http_flv_module \

              --with-http_mp4_module \

              --add-module=../nginx-rtmp-module/
# 让文件成为可执行文件
$ chmod a+x cfg.sh 
$ ./cfg.sh
$ make
$ make install

编译生成过程中,注意在系统可能需要openssl、zlib支持,编译pcre需要g++支持,安装如下:

OpenSSL:

$ sudo apt install openssl libssl-dev

Zlib:

sudo apt-get install zlib1g-dev

(注:有些软件包的名字无法确定,可以借助ubuntu software center来确定其有效名字)

G++:

$ sudo apt install g++

编译成功并安装。

3 环境配置

安装完成后修改配置文件执行下列操作:

进入nginx配置目录

$ cd /usr/local/nginx/conf

编辑 nginx.conf,如果提示没有权限,可执行sudo chmod 777 nginx.conf即可取得所有权限

编辑nginx.conf文件,主要是添加rtmp服务

#rtmp config
rtmp {
    server {
       listen 1935;
       chunk_size 4096;
       application live {
           live on;
           record off;
       }

       application hls {
           live on;
           hls on;
           hls_path /home/gzsz/tmp/hls;
           hls_fragment 2s;
       }
    }
}

配置nginx的http服务,提供hls和播放器服务:

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   /home/gzsz/hlcplayer;
            index  index.html index.htm;
        }

       location /hls {
           types {
              application/vnd.apple.mpegurl m3u8;
              video/mp2t ts;
           }
           root /home/gzsz/tmp;
           add_header Cache-Control no-cache;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
       }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }



        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #

        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

配置完成后,保存文件。

执行下列命令重启nginx:

$ sudo /usr/local/nginx/sbin/nginx -s stop
$ sudo /usr/local/nginx/sbin/nginx

如果没有出现错误,这时已经成功搭建好了rtmp流媒体服务器。

可用ffmpeg、OBS等推流,用vlc或本例中的播放器拉流。

本例中播放器示例代码如下:

<html><head>
    <script
  src="jquery-3.5.1.min.js"></script>
  <script src="util.js"></script>
  <script src="mousetrap.min.js" charset="utf-8"></script>
  <style>
    body{
      background-color:black;
    }
    #video{
      position: absolute;
      top: 0px;
      right: 0px;
      bottom: 0px;
      left: 0px;
      margin: auto;
      max-height: 100%;
      max-width: 100%;
    }
  </style>
<title>直播流播放器</title></head>
<body>
  <video id="video" style="width: 100%; height: 100%;" controls=""></video>
  <script src="player.js"></script>
</body>
</html>

完整代码见:【免费】HLC直播视频流播放器资源-CSDN文库

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Humbunklung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值