centos7 操作系统利用nginx和rtmp-module搭建rtmp服务器

1. 下载编译nginx和rtmp-module

1.1 自动下载

# 使用yum安装git
yum -y install git
# 下载nginx-rtmp-module
# 通过git clone 的方式下载到服务器上
mkdir -p  /home/zenglg/src
cd /home/zenglg/src
git clone https://github.com/arut/nginx-rtmp-module.git
# yum 安装 openssl
yum -y install openssl openssl-devel
# yum 安装 gcc环境
yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64
# 下载nginx服务器
# 这里我安装的是 nginx-1.10.3 版本
wget http://nginx.org/download/nginx-1.10.3.tar.gz 
tar -zxvf nginx-1.10.3.tar.gz 
cd nginx-1.10.3
#添加rtmp和openssl支持
./configure --add-module=/home/zenglg/src/nginx-rtmp-module --with-http_ssl_module
make && make install
/usr/local/nginx/sbin/nginx

1.2 手动下载rtmp模块代码

1. git clone GitHub - arut/nginx-rtmp-module: NGINX-based Media Streaming Server ,如果下载失败,可以在windows上下载好拷贝过去

a. 打开网址 ,点击code按钮

通过finalshell拷贝到centos上,路径 /home/zenglg/src

1.4. 配置rtmp直播、录像、点播、hls直播

参考 Nginx+rtmp实现rtmp,hls直播流_nginx+rtmp 数据加密_卿十月的博客-CSDN博客

nginx搭建rtmp直播、rtmp点播、,hls直播服务配置指令详解_nginx 配置rtmp_innershar的博客-CSDN博客

参考链接 Nginx RTMP 模块 nginx-rtmp-module 指令详解

配置文件路径(nginx安装路径下的相对路径): nginx/conf/nginx.conf


user  root;
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 {
    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       80;
        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;
        }

        # http播放地址
        location /hls {
                types {
                 application/vnd.apple.mpegurl m3u8;
                 video/mp2t ts;
                }
                root /data;        #//hls文件路径
                   
                add_header Cache-Control no-cache;
        }


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

}

# 在http节点后面加上rtmp配置:
rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            record off;
            hls on;
            hls_path /data/hls;     #//hls 文件存放地址
          
         

            recorder rec {
                record all;
                record_path /home/tuners/vod;
                record_suffix _%Y%m%d_%H%M%S.mp3;
               #record_unique on;
                record_interval 10s;
            }
            

        }
         application vod {
            play /home/tuners/vod; #//视频文件存放位置。
       }
      
   
      

    }
}





1.5. 录像接口调用

参考文档 【nginx-rtmp】01、控制模块(Control module)_ENVON的博客-CSDN博客

http://192.168.61.128:80/control/record/start?app=rtmplive&name=235&rec=rec

http://192.168.61.128:80/control/record/stop?app=rtmplive&name=235&rec=rec

1.6. 启停nignx

1. 启动,默认http端口是80

/usr/local/nginx/sbin/nginx   

可以用浏览器打开链接: ip:80访问页面。

确认是否出现nginx页面,能打开则说明nginx运行成功;

2. 停止nginx

# 安装 psmisc,才能使用killall命令,killall不是系统自带的
yum install psmisc 
# 关闭nginx进程
killall nginx

3. 根据进程名查看进程是否在运行

ps aux | grep nginx

下图红色的就是进程名

1.7. 防火墙需要关闭

# 禁止防火墙服务器
systemctl disable firewalld.service
# 关闭运行的防火墙
systemctl stop firewalld.service

1.8. 推流测试

我们利用ffmpeg去推测试流

# 安装ffmpeg 
# 安装epel-release,因为安装需要使用其他的repo源,所以需要epel支持
yum install -y epel-release rpm
# 安装Nux-Dextop源:
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux
-dextop-release-0-1.el7.nux.noarch.rpm
yum repolist
yum install -y ffmpeg
# 推流测试命令
./ffmpeg  -stream_loop  -1  -re -i  D:\123.mp3 -acodec aac  -f flv  rtmp://192.168.61.128:1935/rtmplive/235 

注意: 推流格式视频必须是264,音频必须是aac

1.9. 验证rtmp流

vlc 播放 rtmp://192.168.61.128:1935/rtmplive/235 即可;235是stream_name可以自定义

1.10. 验证hls流

vlc 播放 http://192.168.61.128:80/hls/235.m3u8 接口;235是stream_name可以自定义

1.11 开机启动脚本

autostart.sh脚本调用程序的启动脚本


/home/zenglg/start.sh
# 将脚本移动到/etc/rc.d/init.d目录下
mv  autostart.sh /etc/rc.d/init.d
# 增加脚本的可执行权限
chmod 755 /etc/rc.d/init.d/autostart.sh
# 添加脚本到开机自动启动项目中
cd /etc/rc.d/init.d
chkconfig --add autostart.sh
chkconfig autostart.sh on

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

三希

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

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

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

打赏作者

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

抵扣说明:

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

余额充值