基于CentOS7.8.2003搭建nginx-rtmp-ffmpeg流服务器

CentOS7.8.2003 搭建Nginx-rtmp-ffmpeg流服务器

查看系统版本

[root@VM-0-8-centos ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)

安装ffmpeg

注: 此 ffmpeg 安装方式适用于CentOS 7

参考:CentOS中yum安装ffmpeg

准备环境

安装EPEL Release,因为安装需要使用其他的repo源,所以需要EPEL支持:

[root@VM-0-8-centos nginx-rtmp-ffmpeg]# yum install -y epel-release
	...........                                                          
Complete!

#如果出现缺少Code提示,可以:

sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

安装完成之后,可以查看是否安装成功

[root@VM-0-8-centos /]# yum repolist
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.yun-idc.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                     repo name                         				  		 status
base/7/x86_64              CentOS-7 - Base                      					 10070
epel/x86_64                Extra Packages for Enterprise Linux 7 - x86_64            13426
extras/7/x86_64            CentOS-7 - Extras                                          413
updates/7/x86_64           CentOS-7 - Updates                                         1125
repolist: 27758

安装Nux-Dextop源

由于CentOS没有官方FFmpeg rpm软件包,所以使用第三方YUM源(Nux Dextop)完成此工作。

#导入一个Code
[root@VM-0-8-centos nginx-rtmp-ffmpeg]# rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
#安装nux-dextop 源
[root@VM-0-8-centos nginx-rtmp-ffmpeg]# rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
Retrieving http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:nux-dextop-release-0-5.el7.nux   ################################# [100%]

   
#查看repo源是否安装成功
[root@VM-0-8-centos nginx-rtmp-ffmpeg]# yum repolist
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.yun-idc.com
 * extras: mirrors.aliyun.com
 * nux-dextop: li.nux.ro
 * updates: mirrors.aliyun.com
repo id                     repo name                         				  		 status
base/7/x86_64              CentOS-7 - Base                      					 10070
epel/x86_64                Extra Packages for Enterprise Linux 7 - x86_64            13426
extras/7/x86_64            CentOS-7 - Extras                                          413
nux-dextop/x86_64          Nux.Ro RPMs for general desktop use                        2724
updates/7/x86_64           CentOS-7 - Updates                                         1125
repolist: 27758

安装ffmpeg

#yum安装ffmpeg:
[root@VM-0-8-centos nginx-rtmp-ffmpeg]# yum install ffmpeg ffmpeg-devel -y

#安装完成后检查ffmpeg 版本: 
[root@VM-0-8-centos /]# ffmpeg -version
ffmpeg version 2.8.15 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
libavutil      54. 31.100 / 54. 31.100
libavcodec     56. 60.100 / 56. 60.100
libavformat    56. 40.101 / 56. 40.101
libavdevice    56.  4.100 / 56.  4.100
libavfilter     5. 40.101 /  5. 40.101
libavresample   2.  1.  0 /  2.  1.  0
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  2.101 /  1.  2.101
libpostproc    53.  3.100 / 53.  3.100

检查ffmpeg编码

[root@VM-0-8-centos /]# ffmpeg -codecs

由于我需要编码aac得音频,所以查看编码方式:

encode

安装 ffmpeg 没有问题。

安装 nginx-rtmp

安装Nginx编译需要的依赖

[root@VM-0-8-centos /]# yum -y install gcc* zlib zlib-devel openssl openssl-devel pcre-devel

创建data/nginx-rtmp-ffmpeg目录

参考:linux中创建一次创建多个目录或创建多级目录

[root@VM-0-8-centos /]# cd /dev
[root@VM-0-8-centos dev]# mkdir -p ./data/nginx-rmpt
[root@VM-0-8-centos dev]# cd ./data/nginx-rmpt/
[root@VM-0-8-centos nginx-rmpt]# 

安装wget

[root@VM-0-8-centos nginx-rmpt]# yum install wget

下载nginx安装包以及nginx-rtmp-modul模块

由于需要编译安装第三方模块,所以选择下载安装包安装。

Nginx下载地址:https://nginx.org/

选择合适的版本,这里我现在稳定版本:nginx-1.18.0.tar.gz 。

可以使用wget命令安装,也可以从官网下载安装包安装。

[root@VM-0-8-centos nginx-rmpt]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@VM-0-8-centos nginx-rmpt]# wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
[root@VM-0-8-centos nginx-rmpt]# ll
total 1524
-rw-r--r-- 1 root root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz
-rw-r--r-- 1 root root  519919 Aug 24 17:07 v1.2.1.tar.gz

解压安装压缩包

[root@VM-0-8-centos nginx-rmpt]# tar -zxvf nginx-1.18.0.tar.gz
[root@VM-0-8-centos nginx-rmpt]# tar -zxvf v1.2.1.tar.gz
[root@VM-0-8-centos nginx-rmpt]# ll
total 1532
drwxr-xr-x 8 1001 1001    4096 Apr 21 22:09 nginx-1.18.0
-rw-r--r-- 1 root root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz
drwxrwxr-x 6 root root    4096 Nov 29  2017 nginx-rtmp-module-1.2.1
-rw-r--r-- 1 root root  519919 Aug 24 17:07 v1.2.1.tar.gz

进入nginx-1.18.0.tar.gz安装包

[root@VM-0-8-centos nginx-rmpt]# cd nginx-1.18.0/

配置模块

[root@VM-0-8-centos nginx-1.18.0]# ./configure --prefix=/usr/local/src/nginx  --add-module=../nginx-rtmp-module-1.2.1  --with-http_ssl_module
	.............
adding module in ../nginx-rtmp-module-1.2.1
 + ngx_rtmp_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/src/nginx"
  nginx binary file: "/usr/local/src/nginx/sbin/nginx"
  nginx modules path: "/usr/local/src/nginx/modules"
  nginx configuration prefix: "/usr/local/src/nginx/conf"
  nginx configuration file: "/usr/local/src/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/src/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/src/nginx/logs/error.log"
  nginx http access log file: "/usr/local/src/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

编译并安装

[root@VM-0-8-centos nginx-1.18.0]# make
	.................
	sed -e "s|%%PREFIX%%|/usr/local/src/nginx|" \
	-e "s|%%PID_PATH%%|/usr/local/src/nginx/logs/nginx.pid|" \
	-e "s|%%CONF_PATH%%|/usr/local/src/nginx/conf/nginx.conf|" \
	-e "s|%%ERROR_LOG_PATH%%|/usr/local/src/nginx/logs/error.log|" \
	< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/data/nginx-rmpt/nginx-1.18.0'
[root@VM-0-8-centos nginx-1.18.0]# make install
	.........
test -d '/usr/local/src/nginx/logs' \
	|| mkdir -p '/usr/local/src/nginx/logs'
make[1]: Leaving directory `/dev/data/nginx-rtmp-ffmpeg/nginx-1.18.0'

启动nginx

[root@VM-0-8-centos sbin]# /usr/local/src/nginx/sbin/nginx 
[root@VM-0-8-centos sbin]# ps -ef | grep nginx
root     10523     1  0 17:29 ?        00:00:00 nginx: master process ./nginx
nobody   10524 10523  0 17:29 ?        00:00:00 nginx: worker process
root     10537 17663  0 17:29 pts/0    00:00:00 grep --color=auto nginx

访问

00001

安装 nginx 没有问题。

nginx-rtmp 流服务配置

nginx.conf 配置

进入nginx的conf目录,手动替换配置内容

[root@VM-0-8-centos /]# cd /usr/local/src/nginx/conf
[root@VM-0-8-centos /]# vim nginx.conf

用下面的配置内容替换掉原nginx.conf内容。

以下是nginx-rtmp流服务的nginx.conf配置

#daemon off;
worker_processes 1;

events {
    worker_connections 1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
		
        application stream {
            live on;

            exec ffmpeg -i rtmp://localhost:1935/stream/$name
              -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1280x720 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name ;
        }

        application hls {
            live on;
            hls on;
            hls_fragment_naming system;
            hls_fragment 2;
            hls_playlist_length 20;
            hls_path /usr/local/src/nginx/data/hls;
            hls_nested on;
			hls_cleanup on;
        }
    }
}

http {


	include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
	
	gzip on;
	gzip_min_length 1k;
	gzip_comp_level 4;
	gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css application/json;
	gzip_disable "MSIE [[1-6]]\.";
	gzip_vary on;

	access_log  logs/access.log;
	log_format  main  '$remote_addr - $remote_user [$time_local] "$request" $http_host'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
	
    #ssl_ciphers         HIGH:!aNULL:!MD5;
    #ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    #ssl_session_cache   shared:SSL:10m;
    #ssl_session_timeout 10m;

    server {
        listen 80;
        server_name  localhost;

		location / {
            root   html;
            index  index.html index.htm;
        }

        location /hls {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Credentials' 'true';
			add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
			add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
			if ($request_method = 'OPTIONS') {
				add_header 'Access-Control-Max-Age' 1728000;
				add_header 'Content-Type' 'text/plain; charset=utf-8';
				add_header 'Content-Length' 0;
				return 200;
			}
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /usr/local/src/nginx/data;
            
        }

        location /live {
          alias /usr/local/src/nginx/data/hls;
          types {
              application/vnd.apple.mpegurl m3u8;
              video/mp2t ts;
          }
          add_header Cache-Control no-cache;
          add_header Access-Control-Allow-Origin *;
        }
    }
	
	# HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      /usr/local/src/nginx/certs/server.crt;
        ssl_certificate_key  /usr/local/src/nginx/certs/server.key;

        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
		
		
		location / {
            root   html;
            index  index.html index.htm;
        }

		location /hls {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Credentials' 'true';
			add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
			add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
			add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
			if ($request_method = 'OPTIONS') {
				add_header 'Access-Control-Max-Age' 1728000;
				add_header 'Content-Type' 'text/plain; charset=utf-8';
				add_header 'Content-Length' 0;
				return 200;
			}
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /usr/local/src/nginx/data;
            
        }

        location /live {
          alias /usr/local/src/nginx/data/hls;
          types {
              application/vnd.apple.mpegurl m3u8;
              video/mp2t ts;
          }
          add_header Cache-Control no-cache;
          add_header Access-Control-Allow-Origin *;
        }
    }
}

创建流媒体存储文件目录

[root@VM-0-8-centos /]# cd /usr/local/src/nginx
[root@VM-0-8-centos nginx]# mkdir -p ./data/hls 

重启nginx

[root@VM-0-8-centos nginx]# ./sbin/nginx -s reload

报错:

00008

原因是没有用于https的ssl证书

如果你不需要用到https,那么将上面的nginx.conf文件的配置内容中的HTTPS server 配置部分注释或删除后启动即可。

如果需要HTTPS server的配置,那么操作如下:

  1. 在容器内创建放置证书的目录:

    [root@VM-0-8-centos /]# cd /usr/local/src/nginx
    [root@VM-0-8-centos nginx]# mkdir certs
    [root@VM-0-8-centos nginx]# ls
    certs client_body_temp conf data daya fastcgi_temp html logs proxy_temp sbin  scgi_temp uwsgi_temp
    [root@VM-0-8-centos nginx]# cd certs/
    [root@VM-0-8-centos certs]# pwd
    /usr/local/src/nginx/certs
    

    这里我将证书目录放置到nginx安装目录下

  2. 将可用的ssl证书放到宿主机的某一目录下,使用以下命令复制到容器中刚创建的证书目录certs下:

    [root@VM-0-8-centos /]# docker cp /usr/local/src/nginx/certs/server.crt 34091d225d00:/usr/local/src/nginx/certs
    [root@VM-0-8-centos /]# docker cp /usr/local/src/nginx/certs/server.key 34091d225d00:/usr/local/src/nginx/certs
    

    这里我把证书放到了宿主机的 /usr/local/src/nginx/certs 目录下

  3. 查看容器中创建的证书目录certs:

    [root@VM-0-8-centos /]# cd /usr/local/src/nginx/certs
    [root@VM-0-8-centos certs]# ls
    ca.crt  ca.key  server.crt  server.csr  server.key
    

    可以发现已经拥有证书了

    PS: OpenSSL证书生成方式见文章 创建HTTPS访问SSL免费证书

再次启动nginx

[root@VM-0-8-centos /]# /usr/local/src/nginx/sbin/nginx 
[root@VM-0-8-centos /]# ps -ef | grep nginx
root      4001     0  0 08:55 ?        00:00:00 nginx: master process ./sbin/nginx
nobody    4002  4001  0 08:55 ?        00:00:00 nginx: worker process
nobody    4003  4001  0 08:55 ?        00:00:00 nginx: cache manager process
root      4005    14  0 08:55 pts/1    00:00:00 grep --color=auto nginx

访问

浏览器输入:

https://58.87.107.146

http://58.87.107.146

访问正常。

推拉流验证

下载安装OBS

00002

配置OBS

00003

下载安装VLC

00004

配置VLC

00005

推流

00006

拉流

00007

posted @ 2020-09-30 16:44  IT-小浣熊  阅读(104)  评论(0)    收藏  举报