BPI-M4 : Nginx 스트리밍 서버 구축(컴파일)

OS/Banana Pi /BPI-Bit 2021. 2. 23. 01:44 Posted by 파란크리스마스

출처

openssl 라이브러리 소스 다운로드

pi@bpi-iot-ros-ai:~$ wget http://www.openssl.org/source/openssl-1.1.1g.tar.gz
pi@bpi-iot-ros-ai:~$ tar xvf openssl-1.1.1g.tar.gz

PCRE 라이브러리 설치

pi@bpi-iot-ros-ai:~$ wget https://acc.dl.osdn.jp/sfnet/p/pc/pcre/pcre/8.32/pcre-8.32.tar.gz
pi@bpi-iot-ros-ai:~$ tar xvf pcre-8.32.tar.gz
pi@bpi-iot-ros-ai:~$ cd pcre-8.32/
pi@bpi-iot-ros-ai:~/pcre-8.32$ ./configure --prefix=/usr/local
pi@bpi-iot-ros-ai:~/pcre-8.32$ make
pi@bpi-iot-ros-ai:~/pcre-8.32$ sudo make install

zip 라이브러리 설치

pi@bpi-iot-ros-ai:~$ wget https://zlib.net/fossils/zlib-1.2.8.tar.gz
pi@bpi-iot-ros-ai:~$ tar xvf zlib-1.2.8.tar.gz 
pi@bpi-iot-ros-ai:~$ cd zlib-1.2.8/
pi@bpi-iot-ros-ai:~/zlib-1.2.8$ ./configure --prefix=/usr/localmake
pi@bpi-iot-ros-ai:~/zlib-1.2.8$ make -j4
pi@bpi-iot-ros-ai:~/zlib-1.2.8$ sudo make install

nginx 설치

nginx-vod-module 소스 다운로드

pi@bpi-iot-ros-ai:~$ git clone https://github.com/kaltura/nginx-vod-module.git

nginx 소스 다운로드

pi@bpi-iot-ros-ai:~$ wget http://nginx.org/download/nginx-1.19.7.tar.gz
pi@bpi-iot-ros-ai:~$ tar xvf nginx-1.19.7.tar.gz
pi@bpi-iot-ros-ai:~$ cd nginx-1.19.7/

빌드 환경 설정 - configure

  • --add-module=../nginx-vod-module : 스트리밍 관련 라이브러리 추가
  • --pid-path : pid 파일 생성 기본 경로 / 해당 옵션이 없는 경우 nginx.conf에서 설정 가능
pi@bpi-iot-ros-ai:~/nginx-1.19.7$ ./configure \
  --add-module=../nginx-vod-module \
  --prefix=/usr/local/nginx \
  --conf-path=/usr/local/nginx/conf/nginx.conf \
  --error-log-path=/usr/local/nginx/logs/error.log \
  --http-log-path=/usr/local/nginx/logs/access.log \
  --user=nginx \
  --group=nginx \
  --lock-path=/usr/local/nginx/system/nginx.lock \
  --pid-path=/usr/local/nginx/system/nginx.pid \
  --without-http_autoindex_module \
  --without-http_ssi_module \
  --with-file-aio \
  --with-http_ssl_module \
  --with-openssl=../openssl-1.1.1g \
  --with-openssl-opt=enable-weak-ssl-ciphers \
  --with-http_gunzip_module \
  --with-http_gzip_static_module \
  --with-http_random_index_module \
  --with-http_secure_link_module \
  --with-http_stub_status_module \
  --with-http_auth_request_module \
  --with-http_addition_module \
  --with-http_realip_module \
  --with-http_stub_status_module \
  --with-threads --with-stream \
  --with-stream_ssl_module \
  --with-http_v2_module \
  --http-client-body-temp-path=/var/cache/nginx/client_body_temp \
  --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
  --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
  --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
  --http-scgi-temp-path=/var/cache/nginx/scgi_temp

빌드

pi@bpi-iot-ros-ai:~/nginx-1.19.7$ make -j4

설치

pi@bpi-iot-ros-ai:~/nginx-1.19.7$ sudo make install
make -f objs/Makefile install
make[1]: Entering directory '/home/pi/nginx-1.19.7'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
cp conf/fastcgi_params \
        '/usr/local/nginx/conf/fastcgi_params.default'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
cp conf/uwsgi_params \
        '/usr/local/nginx/conf/uwsgi_params.default'
cp conf/scgi_params \
        '/usr/local/nginx/conf/scgi_params.default'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/system' \
        || mkdir -p '/usr/local/nginx/system'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/home/pi/nginx-1.19.7'

nginx 계정 추가

error.log에 해당 오류 발견시 계정 추가 필요 nginx: [emerg] getpwnam("nginx") failed

pi@bpi-iot-ros-ai:~$ sudo groupadd nginx
pi@bpi-iot-ros-ai:~$ sudo useradd -g nginx -s /bin/bash -m nginx

nginx 서비스 등록

nginx.service 파일 생성

pi@bpi-iot-ros-ai:~$ sudo vi /etc/systemd/system/nginx.service

 

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/system/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

nginx.service 서비스 등록

pi@bpi-iot-ros-ai:~$ sudo systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /etc/systemd/system/nginx.service.

nginx.service 서비스 시작

pi@bpi-iot-ros-ai:~$ sudo systemctl start nginx.service

nginx.service 서비스 상태확인

pi@bpi-iot-ros-ai:~$ sudo systemctl status nginx.service

nginx 스트리밍 관련 환경 설정

nginx.conf 수정

pi@bpi-iot-ros-ai:~$ sudo vi /usr/local/nginx/conf/nginx.conf

 

    server {
        listen       80;
        server_name  localhost;

        # vod settings
        vod_mode local;
        vod_fallback_upstream_location /fallback;
        vod_last_modified 'Sun, 19 Nov 2000 08:52:00 GMT';
        vod_last_modified_types *;
        vod_metadata_cache metadata_cache 512m;
        vod_response_cache response_cache 128m;
        gzip on;
        gzip_types application/vnd.apple.mpegurl;

        vod_segment_duration 1000;

        open_file_cache          max=1000 inactive=5m;
        open_file_cache_valid    2m;
        open_file_cache_min_uses 1;
        open_file_cache_errors   on;
        aio on;
        location /content/ {
                root /media_content/;
                vod hls;
                add_header Access-Control-Allow-Headers '*';
                add_header Access-Control-Expose-Headers 'Server,range,Content-Length,Content-Range';
                add_header Access-Control-Allow-Methods 'GET, HEAD, OPTIONS';
                add_header Access-Control-Allow-Origin '*';
                expires 100d;
        }

nginx.service 서비스 재시작

pi@bpi-iot-ros-ai:~$ sudo systemctl restart nginx.service

동영상 파일 복사

pi@bpi-iot-ros-ai:~$ sudo mkdir -p /media_content/content
pi@bpi-iot-ros-ai:~$ cd /media_content/content
pi@bpi-iot-ros-ai:/media_content/content$ wget https://www.radiantmediaplayer.com/media/big-buck-bunny-360p.mp4

동영상 구현 (hls.js)

pi@bpi-iot-ros-ai:~$ cd /usr/local/nginx/html
pi@bpi-iot-ros-ai:/usr/local/nginx/html$ sudo vi player.htm

 

<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<video id="video" controls></video>
<script>
  if(Hls.isSupported()) {
    var video = document.getElementById('video');
    var hls = new Hls();
    hls.loadSource('http://192.168.0.87/content/big-buck-bunny-360p.mp4/index.m3u8');
    hls.attachMedia(video);
    hls.on(Hls.Events.MANIFEST_PARSED,function() {
      // video.play();
    });
  }
</script>

확인

TAG ,

댓글을 달아 주세요