メインコンテンツへスキップ

nginxで動画配信サーバー構築

    nginxで動画配信サーバー構築メモ。

    構築済み環境

    お名前ドットコム VPS(KVM)メモリ2Gプラン

    CentOS Linux release 7.7.

    *注意

    作業はrootで行っていますが実際に運用する場合はrootで作業しないように。他、セキュリティーや運用上の観点から構成や設定が運用に耐えられません。あくまでも単体で動かす用。*まあ、普通に動くんだけども

    事前準備

    ソフトウェアを最新にして、必要なものをインストール。 

    yum -y update
    yum -y install git gcc pcre-devel openssl-devel wget

    作業用ディレクトリ作成とnginx取得

    
    mkdir works
    cd works/
    wget https://nginx.org/download/nginx-1.14.2.tar.gz

    nginxインストール

    nginx-rtmp-moduleをインストール

    tar zxvf nginx-1.14.2.tar.gz
    ls
    cd nginx-1.14.2
    ls
    git clone https://github.com/arut/nginx-rtmp-module.git
    ./configure --add-module=nginx-rtmp-module/
    make
    make install

    nginx設定

    Config設定

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

    HLS・MPEG-DASH・FLVファイルの出力先設定

    worker_processes auto;
    error_log  logs/error.log error;
    events {
           worker_connections  1024;
    }
    rtmp_auto_push on;
    rtmp {
       server {
           listen 1935;
           access_log logs/rtmp_access.log;
           chunk_size 4096;
           timeout 10s;
           application live {
               live on;
               # HLSの記述欄
               hls on;
               hls_path /var/www/html/hls;
               hls_fragment 10s;
               # MEPG-DASHの記述欄
               dash on;
               dash_path /var/www/html/dash;
               dash_fragment 10s;
               # FLVの記述欄
               record all;
               record_path /var/www/html/flv;
               record_unique on;
           }
       }
    }
    http {
       server {
           listen  80;
           include mime.types;
           default_type    application/octet-stream;
           server_name localhost;
           add_header  Access-Control-Allow-Origin *;
           location /hls {
               types {
                    application/vnd.apple.mpegurl m3u8;
               }
               root /var/www/html/;
           }
           location /dash {
               types {
                    application/vnd.apple.mpegurl mpd;
               }
               root /var/www/html/;
           }
       }
    }

    各種ファイル出力先ディレクトリを作成

    mkdir -p /var/www/html/hls
    chown nobody. /var/www/html/hls
    
    mkdir -p /var/www/html/dash
    chown nobody. /var/www/html/dash
    
    mkdir -p /var/www/html/flv
    chown nobody. /var/www/html/flv
    

    nginx設定確認

    /usr/local/nginx/sbin/nginx -t

    問題無い場合、以下のように表示される。

    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    

    起動スクリプトを作成

    vi /usr/lib/systemd/system/live_nginx.service
    
    [Unit]
    Description=nginx - high performance web server
    Documentation=http://nginx.org/en/docs/
    After=network.target remote-fs.target nss-lookup.target
    [Service]
    Type=forking
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s TERM $MAINPID
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target

    自動起動設定

    systemctl enable live_nginx.service

    nginx起動

    nginx起動

    systemctl start live_nginx.service

    nginx起動確認

     telnet localhost 80
     GET /

    以下のようにレスポンスがあればnginx起動中

    <html>
    <head><title>403 Forbidden</title></head>
    <body bgcolor="white">
    <center><h1>403 Forbidden</h1></center>
    <hr><center>nginx/1.14.2</center>
    </body>
    </html>
    

    nginx起動をブラウザから確認

    サーバーのIPが13.32.53.66の場合

    http://13.32.53.66/

    この時点でブラウザに反応が無いはず。

    ファイアーウォール設定

    firewall-cmd --add-service=http --zone=public --permanent
    firewall-cmd --reload

    現在、httpによる通信は推奨されていないため通常はファイアーウォールで閉じられています。運用する場合はhttps推奨。

    以上でブラウザからアクセス可能になったはず。

    nginx動作確認

    配信テスト前に適当な動画を置いてストリーミングされるか確認する。

    Big Buck Bunnyから動画をお借りします。

    cd /var/www/html/hls/
    wget http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4

    動画配信用html作成

    vi /usr/local/nginx/html/index.html
    
    <!DOCTYPE html>
    <html>
    <head>
     <meta charset="utf-8">
     <title>MediaElement</title>
     <!-- MediaElement style -->
     <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/mediaelement/4.2.9/mediaelementplayer.css" />
    </head>
    <body>
     <!-- MediaElement -->
     <script src="//cdnjs.cloudflare.com/ajax/libs/mediaelement/4.2.9/mediaelement-and-player.js"></script>
     <video id="player" width="640" height="360">
    </body>
    <script type="text/javascript">
         var player = new MediaElementPlayer('player', {
           success: function(mediaElement, originalNode) {
             console.log("Player initialised");
           }
         });
           player.setSrc("hls/bbb_sunflower_1080p_30fps_normal.mp4");
    </script>
    </html>
    
    

    サーバーのIPが13.32.53.66の場合

    http://13.32.53.66/

    を確認すると以下のように表示されているはずです。

    画像1

    再生ボタンを押すと動画の再生が開始します。

    画像2

    配信

    OBSを使用して配信します。

    画像3

    配信用HTML作成

    vi /usr/local/nginx/html/test.html
    
    <!DOCTYPE html>
    <html>
    <head>
     <meta charset="utf-8">
     <title>MediaElement</title>
     <!-- MediaElement style -->
     <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/mediaelement/4.2.9/mediaelementplayer.css" />
    </head>
    <body>
     <!-- MediaElement -->
     <script src="//cdnjs.cloudflare.com/ajax/libs/mediaelement/4.2.9/mediaelement-and-player.js"></script>
     <video id="player" width="640" height="360">
    </body>
    <script type="text/javascript">
         var player = new MediaElementPlayer('player', {
           success: function(mediaElement, originalNode) {
             console.log("Player initialised");
           }
         });
           player.setSrc("hls/test.m3u8");
    </script>
    </html>

    OBSで「配信開始」ボタンを押して配信を開始する。

    nginx側での動作確認。

    # ls -lt /var/www/html/hls/
    
    -rw-r--r-- 1 nobody nobody   6811428 Apr  7 13:17 test-2.ts
    -rw-r--r-- 1 nobody nobody       147 Apr  7 13:17 test.m3u8
    -rw-r--r-- 1 nobody nobody  11728192 Apr  7 13:17 test-1.ts
    -rw-r--r-- 1 nobody nobody  11395996 Apr  7 13:16 test-0.ts
    -rw-r--r-- 1 root   root   276134947 Dec 18  2013 bbb_sunflower_1080p_30fps_normal.mp4
    
    

    test.m3u8というファイルが作成されているのを確認。

    サーバーのIPが13.32.53.66の場合

    http://13.32.53.66/test.html

    を確認すると配信されているのが確認できるはずです。

    備考

    VPS(KVM)メモリ2Gプランでもサーバー単体で十分なほどの性能があるようです。オープンソースだけでYOUTUBEのような大規模なサービスも作れそうです。

    参考

    簡単にできる!Live配信サーバ構築

    動画配信サーバー構築(nginx+nginx-rtmp-module)

    nginx でストリーミングサーバを作る方法


     
     

    caz

     
     
    株式会社アテナ CEO https://www.athena.asia/ AI関連開発、システム開発、ブロックチェーン、中小企業飲食店のシステム構築運用

    あなたへのおすすめ