2020Ubuntuにnginx+rtmp moduleで、RTMPエンコードして映像配信したいだけ

WebARENAの安いクラウド

349円/月額のLinuxサーバを配信基盤にして映像配信サービスを稼働させます。

nginx安定板をダウンロード

root@cloud01:/home/ubuntu# wget https://nginx.org/download/nginx-1.18.0.tar.gz
--2020-10-30 16:49:24--  https://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘nginx-1.18.0.tar.gz’

nginx-1.18.0.tar.gz            100%[===================================================>]   1015K   777KB/s    in 1.3s

2020-10-30 16:49:27 (777 KB/s) - ‘nginx-1.18.0.tar.gz’ saved [1039530/1039530]

root@cloud01:/home/ubuntu# tar xzvf nginx-1.18.0.tar.gz

gitを取得

root@cloud01:/home/ubuntu# apt-get install git
Reading package lists... Done
Building dependency tree
Reading state information... Done
git is already the newest version (1:2.17.1-1ubuntu0.7).
The following packages were automatically installed and are no longer required:
  grub-pc-bin libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-argcomplete python-ipaddr
  python-minimal python2.7 python2.7-minimal
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.

 

nginx-rtmp-moduleを取得

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

 

コンパイル準備

apt-get install libpcre3-dev zlib1g-dev openssl libssl-dev make gcc

configureとmake

root@cloud01:/home/ubuntu# cd nginx-1.18.0
root@cloud01:/home/ubuntu/nginx-1.18.0#./configure --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --add-module=../nginx-rtmp-module --with-debug --prefix=/usr/local/nginx --with-http_secure_link_module --user=www-data --group=www-data
root@cloud01:/home/ubuntu/nginx-1.18.0#make
root@cloud01:/home/ubuntu/nginx-1.18.0#make install

PHPのインストール

apt-get install php php-cgi php-cli php-fpm

 

エンコード受付用パスワード

cd /usr/local/nginx/html/
mkdir publishauth
cd publishauth/
cat auth.php
// www.server.com/auth.php?user=felix&pass=felixpassword

//check if querystrings exist or not
if(empty($_GET['user']) || empty($_GET['pass']))
   {
    //no querystrings or wrong syntax
    echo "File not found.";
    header('HTTP/1.0 404 Not Found');
    exit(1);
   }

else
   {
    //querystring exist
    $username = $_GET['user'];
    $password = $_GET['pass'];
   }

$savedpassword =  livepass;
$saveduser = liveuser;


//check pass and user string
if (strcmp($password,$savedpassword)==0 &&  strcmp($username,$saveduser)==0 )
      {
        echo "Password and Username OK! ";

        }

else
      {
        echo "password or username wrong! ";
        header('HTTP/1.0 404 Not Found'); //kein stream
        }

?>

 

nginx.confの設定

grep -v '^\s*#' /etc/nginx/nginx.conf |grep -v '^\s*$'
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
             fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
             include fastcgi_params;
        }
    }
}
rtmp {
      server {
      listen 1935;
      notify_method get;
      buflen 5s;
      drop_idle_publisher 5s;
      publish_notify on;
      wait_key on;
      wait_video on;
      sync 10ms;

      application translate {
         live on;
         on_publish http://localhost/publishauth/auth.php;

         #LANからの映像配信しか受け付けない
         allow publish 192.168.0.0/16;
         deny publish all;

      }
   }
}

 

apacheが動いているんで削除

root@webarena01:/home/ubuntu# apt-get remove --purge apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  apache2-data apache2-utils grub-pc-bin libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-argcomplete python-ipaddr
  python-minimal python2.7 python2.7-minimal
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  apache2*
0 upgraded, 0 newly installed, 1 to remove and 7 not upgraded.
After this operation, 536 kB disk space will be freed.
Do you want to continue? [Y/n]

 

nginxとPHPの起動確認

root@Cloudn01:/etc/nginx# /usr/sbin/nginx
root@Cloudn01:/etc/nginx# netstat -an |grep :80
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
root@Cloudn01:/etc/nginx# netstat -an |grep :1935
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN
root@Cloudn01:/etc/nginx# ps aux |egrep 'www-data'
www-data   397  0.0  0.7  44744  3820 ?        S    17:04   0:00 nginx: worker process
www-data   398  0.0  0.7  44480  3940 ?        S    17:04   0:00 nginx: cache manager process
root       413  0.0  0.2  14428  1080 pts/0    S+   17:04   0:00 grep -E --color=auto www-data
www-data  9985  0.0  0.5 439996  2476 ?        S    Oct16   0:00 php-fpm: pool www
www-data  9986  0.0  0.4 439996  2132 ?        S    Oct16   0:00 php-fpm: pool www

 

phpinfo.phpを置いてみる

vi /usr/local/nginx/html/phpinfo.php
<?php
phpinfo();
?>

ダッシュボードの設置

root@webarena01:/usr/local/nginx/html# cp /home/ubuntu/nginx-rtmp-module/stat.xsl .

ダッシュボードへのアクセス

http://server

nginxの自動起動

### BEGIN INIT INFO
# Provides:   nginx
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

test_nginx_config() {
  if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
    return 0
  else
    $DAEMON -t $DAEMON_OPTS
    return $?
  fi
}
start() {
    test_nginx_config
    # Check if the ULIMIT is set in /etc/default/nginx
    if [ -n "$ULIMIT" ]; then
      # Set the ulimits
      ulimit $ULIMIT
    fi
    start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
      --retry 5 --exec $DAEMON -- $DAEMON_OPTS || true
}

stop() {
    start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
      --retry 5 --exec $DAEMON || true
}

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start
    echo "$NAME."
    ;;

  stop)
    echo -n "Stopping $DESC: "
    stop
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    stop
    sleep 1
    echo "$NAME."
    start
    ;;

  reload)
    echo -n "Reloading $DESC configuration: "
    test_nginx_config
    start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
      --exec $DAEMON || true
    echo "$NAME."
    ;;

  configtest|testconfig)
    echo -n "Testing $DESC configuration: "
    if test_nginx_config; then
      echo "$NAME."
    else
      exit $?
    fi
    ;;

  status)
    status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
    ;;

  *)
    echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
    exit 1
    ;;
esac

実行権限を与える

root@webarena01:/usr/local/nginx/html# chmod 755 /etc/init.d/nginx

登録して、rc2.dに反映されているか確認する

root@webarena01:/usr/local/nginx/html# update-rc.d nginx defaults
root@webarena01:/usr/local/nginx/html# ls /etc/rc2.d/
S01acpid   S01console-setup.sh  S01grub-common   S01lvm2-lvmpolld  S01mdadm  S01open-vm-tools  S01postgresql  S01ssh                  S01uuidd
S01apport  S01cron              S01irqbalance    S01lxcfs          S01nginx  S01php7.2-fpm     S01rsync       S01sysstat              S01vpnserver
S01atd     S01dbus              S01lvm2-lvmetad  S01lxd            S01ntp    S01plymouth       S01rsyslog     S01unattended-upgrades

 

エンコードしてみる

rtmp://サーバIP/live/streamkey?user= felix&pass= felixpassword

OBSでの設定はこんな感じになります

サーバIP 192.168.200.50

配信ポイント live

ストリーム名 translate

ユーザ名 liveuser

パスワード livepass

配信用パスワードつける

root@webarena01:/usr/local/nginx/passwd# htpasswd -c .htpasswd hanako
New password:
Re-type new password:
Adding password for user hanako