1. 程式人生 > 實用技巧 >Centos7+Nginx+Rtmp+Ffmpeg搭建流媒體伺服器

Centos7+Nginx+Rtmp+Ffmpeg搭建流媒體伺服器

安裝前需要的工具

#net-tool 查本地IP
#wget 下載安裝包
#unzip 解壓zip包
#gcc gcc-c++ perl 編譯軟體包用
yum install -y net-tools wget unzip gcc gcc-c++ perl

將Centos的yum源更換為國內的阿里雲源

#備份yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
#下載阿里源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#清空快取 yum makecache

安裝nginx及rtmp


切換到 /home 下

cd /home

切換到 /home 下,下載並解壓pcre

#下載pcre包
wget    https://netix.dl.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz
#解壓pcre包
tar -zxvf pcre-8.41.tar.gz

切換到 /home 下,下載並解壓zlib

#下載zlib包
wget http://www.zlib.net/zlib-1.2.11.tar.gz
#解壓zlib包
tar -zxvf zlib-1.2.11
.tar.gz

切換到 /home 下,下載並安裝openssl

#下載openssl包
wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1i.tar.gz
#解壓openssl包
tar -zxvf openssl-1.0.1i.tar.gz
#切換到openssl裡
cd openssl-1.0.1i
#生成配置檔案 預設配置
./config --prefix=/usr/local --openssldir=/usr/local/openssl
#編譯程式
make 
#安裝程式
make install

切換到 /home 下,下載並解壓nginx-rtmp-model

#下載rtmp包
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
#解壓下載包
unzip -o master.zip
#修改資料夾名
mv master nginx-rtmp-module

切換到 /home 下,安裝nginx

#下載nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz
#解壓nignx 
tar -zxvf nginx-1.12.2.tar.gz
#切換到nginx中
cd nginx-1.12.2
#生成配置檔案,將上述下載的檔案配置到configure中
./configure --prefix=/usr/local/nginx --with-pcre=/home/pcre-8.41 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.0.1i --add-module=/home/nginx-rtmp-module  
#編譯程式
make
#安裝程式
make install

切換到 /home 下,安裝ffmpeg

#安裝epel包
yum install -y epel-release 
#匯入簽名
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
#匯入簽名
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 update -y
#安裝ffmpeg
yum install -y ffmpeg
#檢查安裝成功
ffmpeg -version

修改配置執行服務


修改nginx配置

vi /usr/local/nginx/conf/nginx.conf

修改之後的nginx配置

#user  nobody;

#工作程序
worker_processes  1;

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

#指定pid存放的路徑
pid /usr/local/nginx/logs/nginx.pid;

events {
        #使用的網路I/O模型,linux系統推薦是epoll而freeBSD是kqueue
        use  epoll;
        #允許的連線數,最大的高併發連線數為worker_processes*worker_connections
        worker_connections  1024;
}

#RTMP配置
rtmp {
    server {
        #監聽埠
        listen 1935;
        #直播流
        application myapp {
            live on;
            record off;
        }
        #hls配置
        application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
        }
    }
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  伺服器IP;

        #資料格式設定
        charset utf-8;

        location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
                #設定為你的nginx-rtmp-module資料夾路徑,注意訪問許可權
                root /home/nginx-rtmp-module/;
        }

        location /rtmp-publisher {
                #設定為你的nginx-rtmp-module資料夾路徑,注意訪問許可權
                root /home/nginx-rtmp-module/test;
        }

        location / {
            #設定為你的nginx-rtmp-module資料夾路徑,注意訪問許可權,可使用預設設定
            root /home/nginx-rtmp-module/test/www;
            index  index.html index.htm;
        }

        #配置hls
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }
       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

啟動或重啟nginx

/usr/local/nginx/sbin/nginx

或

/usr/local/nginx/sbin/nginx -s reload

可能會出現報錯:Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid" failed(2:No such file or directory)

解決辦法:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

使用nginx -c的引數指定nginx.conf檔案的位置

開啟防火牆

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

在阿里雲增加安全組規則,配置1935埠號

測試功能

1. 檢視IP

ifconfig

2. 上傳測試視訊

在伺服器的home資料夾下上傳一個test.mp4視訊

3. 推送rtmp流

同時,下載一個VLC軟體,輸入"rtmp://伺服器IP:1935/myapp/推送後的視訊名字"點選播放進行測試

ffmpeg -re -i "/home/test.mp4" -vcodec libx264 -vprofile baseline -acodec aac  -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://伺服器IP:1935/myapp/推送後的視訊名字 

推送hls流

寫個HTML測試

ffmpeg -re -i "/home/test.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://伺服器IP:1935/hls/推送後的視訊名字

同時,開啟你的HTML

<script src="https://cdn.jsdelivr.net/hls.js/latest/hls.min.js"></script>
<video id="video"></video>
<script>
  if(Hls.isSupported()) {
    var video = document.getElementById('video');
    var hls = new Hls();
    hls.loadSource('http://伺服器IP/hls/推送後的視訊名字.m3u8');
    hls.attachMedia(video);
    hls.on(Hls.Events.MANIFEST_PARSED,function() {
      video.play();
  });
 }
</script>

參考

  • https://juejin.im/post/6844903623017627656
  • https://blog.csdn.net/kingroc/article/details/50839994
  • https://blog.csdn.net/loyachen/article/details/50909854
  • https://blog.csdn.net/szydwy/article/details/78632222

【部署問題】解決Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid" failed(2:No such file or directory)