linux CentOS7 nginx nginx-rtmp-module搭建直播推流

1. 安装 Nginx 依赖软件
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel 

2. 安装nginx与rtmp模块 

a)获取nginx安装包

cd /usr/local/

wget http://nginx.org/download/nginx-1.19.5.tar.gz  

tar  zxvf  nginx-1.19.5.tar.gz

b)创建nginx安装目录/usr/local/nginx与模块目录/usr/local/nginx-1.19.5/module、视频数据存放位置/tmp/hls

mkdir  -p  /usr/local/nginx

mkdir  -p   /tmp/hls

mkdir  -p  /usr/local/nginx-1.19.5/module

c)下载nginx-rtmp-module,下载后的文件夹名叫nginx-rtmp-module(没有安装git的请安装,执行语句的时候没有git会提示你,yum install git 安装之

cd /usr/local/nginx-1.19.5/module

git clone  https://github.com/arut/nginx-rtmp-module.git

d)进入解压后的nginx目录中,指定nginx安装目录并且指定了安装的模块

cd /usr/local/nginx-1.19.5/

./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-1.19.5/module/nginx-rtmp-module

make -j3 && make install

3. 配置nginx与rtmp模块 ,先备份原始的配置文件ngnix.conf

mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

vim /usr/local/nginx/conf/nginx.conf ,写入如下内容

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       9909;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
rtmp {    
    server {    
        listen 1935;
        max_streams 512;    
        
        application live {    
            live on;    
        }    
        application hls {    
            live on;    
            hls on;    
            hls_path /tmp/hls;    
        }    
    }    
}

未在防火墙上开放9909和1935端口的先开放这两个端口

5.启动nginx

/usr/local/nginx/sbin/nginx

此时如果没有问题,那么你就可以访问http://x.x.x.x:9909了,会出现welcome界面。x.x.x.x是该安装nginx的机器的ip

6.启动推流

a)ffmpeg推流

ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://x.x.x.x:1935/live/0001

  • acc:RTMP的音频格式
  • flv: RTMP的视频格式
  • 命令中的0001可以随意命令,拉取流时一样就可以

b)obs推流也可以,自行研究

7.拉取流,可以使用VLC播放器,在菜单 “媒体”-->“打开网络串流”,输入rtmp://x.x.x.x/live/0001,即可以播放了

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值