搭建RTMP服务器

一、前期配置

配置yun源

[root@RTMP-Service sbin]# cat /etc/yum.repos.d/epel-aliyun.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
[root@RTMP-Service sbin]# yum clean all
[root@RTMP-Service sbin]# yum makecache

关闭防火墙&selinux

systemctl disable firewalld.service
systemctl stop firewalld.service

vim /etc/selinux/config
...
SELINUX=permissive
...

setenforce 0

二、安装ffmpeg

yum -y update
yum -y localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm 
yum -y install ffmpeg

安装好后 运行ffmpeg -version查看是否安装成功

三、安装linux编译包

yum -y install gcc
yum -y install pcre pcre-devel pcre-static pcre-tools
yum -y install openssl openssl-static openssl-devel
yum -y install wget unzip

四、配置Nginx

#文章末尾可选使用docker部署Nginx

下载nginxnginx-http-flv-module

nginx-1.14.2.tar.gz
nginx-http-flv-module-1.2.9.tar.gz

nginx-http-flv-module更多相关使用和配置请查看官网说明nginx-http-flv-module官网地址

解压两个安装包

tar -zxvf nginx-1.14.2.tar.gz
tar -zxvf nginx-http-flv-module-1.2.9.tar.gz

进入nginx目录编译项目

cd nginx-1.14.2
./configure --with-http_ssl_module --with-http_secure_link_module --add-module=../nginx-http-flv-module-1.2.9
make && make install

编译好的nginx在/usr/local/nginx/

创建文件夹mkdir -p /var/log/nginx/并且修改/usr/local/nginx/conf/nginx.conf配置文件

使pid等于/var/run/nginx.pid;

创建自启动文件 /lib/systemd/system/nginx.service 内容如下
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload			# 重新加载
systemctl enable nginx.service  # 设置开机自启动
systemctl start nginx.service   # 设置启动nginx
systemctl stop nginx.service    # 停止nginx

修改/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        /var/run/nginx.pid;


events {
    worker_connections  4096;
}


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

        location /live {
            flv_live on; #打开 HTTP 播放 FLV 直播流功能
            chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回复

            add_header 'Access-Control-Allow-Origin' '*'; #添加额外的 HTTP 头
            add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
        }
        
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /tmp;
            add_header 'Cache-Control' 'no-cache';
        }

        location /dash {
            root /tmp;
            add_header 'Cache-Control' 'no-cache';
        }

        location /stat {
            #推流播放和录制统计数据的配置
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root html; #指定 stat.xsl 的位置
        }

        location /control {
            rtmp_control all; #rtmp 控制模块的配置
        }

    }
}

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;

rtmp {

    out_queue           4096;
    out_cork            8;
    max_streams         128;
    timeout             15s;
    drop_idle_publisher 15s;

    log_interval 5s; #log 模块在 access.log 中记录日志的间隔时间,对调试非常有用
    log_size     1m; #log 模块用来记录日志的缓冲区大小

    server {
        listen 1935;
        notify_method get;
        access_log  /var/log/nginx/access.log;
        chunk_size 4096;
        application live {
            # 开启直播模式
            live on;
            gop_cache on; #打开 GOP 缓存,减少首屏等待时间
            # 允许从任何源push流
            allow publish all;
            # 允许从任何地方来播放流
            allow play all;
            # 20秒内没有push,就断开链接。
            drop_idle_publisher 20s;
        }

        application myapp{
            live on;
            gop_cache on; #打开 GOP 缓存,减少首屏等待时间
        }

        application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
        }

        application dash {
            live on;
            dash on;
            dash_path /tmp/dash;
        }
    }
}

五、尝试推流并且使用 VLC media player 播放器播放视频

测试短视频
视频播放软件

推送视频流

# ffmpeg -re -i MEDIA_FILE_NAME -c copy -f flv 'rtmp://example.com[:port]/appname/streamname'
将.flv文件上传服务器,使用ffmpeg开始推流
ffmpeg -re -i lage2.flv -c copy -f flv 'rtmp://192.168.x.xxx/live/lage'

-re:这个选项告诉 ffmpeg 以实时(real-time)模式运行,即尽可能快地处理输入文件,而不是按照文件的原始时间戳。
-i lage2.flv:-i 选项后面跟着的是输入文件的名称,这里是 lage2.flv。这告诉 ffmpeg 从这个文件读取数据。
-c copy:-c 选项后面跟着的是编解码器名称,这里使用 copy 表示不对视频和音频进行重新编码,而是直接复制原始流。
-f flv:-f 选项后面跟着的是输出格式,这里指定输出格式为 FLV。
'rtmp://192.168.x.xxx/live/lage':这是输出 URL,指定了 RTMP 服务器的地址和流名称。

播放

# indows主机安装视频播放软件
打开 VLC media player
播放》打开媒体》网络》输入URL》开始播放
#http://example.com[:port]/dir?[port=xxx&]app=appname&stream=streamname
http://192.168.x.xxx/live?app=live&stream=lage

在这里插入图片描述
在这里插入图片描述

# 使用docker部署Nginx

1、将Nginx和模块tar包放置在./tar/目录下

wget -O ./tar https://shuiyunxiansheng2018.oss-cn-shenzhen.aliyuncs.com/ffmpeg/nginx-1.14.2.tar.gz 
wget -O ./tar https://shuiyunxiansheng2018.oss-cn-shenzhen.aliyuncs.com/一些安装包/nginx-http-flv-module-1.2.9.tar.gz
wget -O ./tar http://mirrors.aliyun.com/repo/Centos-7.repo

nginx-1.14.2.tar.gz
nginx-http-flv-module-1.2.9.tar.gz

2、编译Nginx镜像

vim dockerfile

## RTMP Nginx
FROM centos:7 as builder
ADD ./tar/nginx-1.14.2.tar.gz /
ADD ./tar/nginx-http-flv-module-1.2.9.tar.gz /
COPY ./tar/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum -y install gcc make pcre pcre-devel pcre-static pcre-tools openssl openssl-static openssl-devel
WORKDIR /nginx-1.14.2
RUN ./configure --with-http_ssl_module --with-http_secure_link_module --add-module=../nginx-http-flv-module-1.2.9
RUN make && make install

FROM centos:7
COPY ./tar/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum -y install pcre pcre-devel pcre-static pcre-tools openssl openssl-static openssl-devel && yum clean all
RUN mkdir -p /var/run
COPY --from=builder /usr/local/nginx /usr/local/nginx
ENV PATH=${PATH}:/usr/local/nginx/sbin
WORKDIR /usr/local/nginx
EXPOSE 80
EXPOSE 443
EXPOSE 1935                # rtmp端口
CMD ["nginx", "-g", "daemon off;"]

3、创建Nginx容器

vim docker-compose.yml

version: "2"
services:
  rtmp_nginx:
    build:
      context: ./
      dockerfile: dockerfile
    container_name: rtmp_nginx
    restart: always
    volumes:
      - "/etc/localtime:/etc/localtime:ro"
      - "./conf/nginx.conf:/usr/local/nginx/conf/nginx.conf"
      - "./logs:/var/log/nginx"
    ports:
      - 80:80
      - 1935:1935                # rtmp端口
    #network_mode: "host"
    healthcheck:
      test: ["CMD", "curl", "-I", "127.0.0.1:80"]
      interval: 30s
      timeout: 30s
      retries: 5
# 启动
docker-compose up -d

[root@Node rtmp]# docker-compose ps
NAME                COMMAND                  SERVICE             STATUS              PORTS
rtmp_nginx          "nginx -g 'daemon of…"   rtmp_nginx          running (healthy)   0.0.0.0:80->80/tcp, 0.0.0.0:1935->1935/tcp, :::80->80/tcp, :::1935->1935/tcp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

财源广进L

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

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

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

打赏作者

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

抵扣说明:

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

余额充值