centos7.5 Nginx+rtmp 搭建流媒体服务器

2024-03-20 06:59

本文主要是介绍centos7.5 Nginx+rtmp 搭建流媒体服务器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

centos7.5 Nginx+rtmp 搭建流媒体服务器

一. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum -y install gcc gcc-c++


二. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel



三. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel



四. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

1、第一种安装方式

yum install -y openssl openssl-devel

2、第二种安装方式(源码编译安装)

sudo wget https://www.openssl.org/source/openssl-1.1.0e.tar.gzsudo tar -zxvf openssl-1.1.0e.tar.gzcd openssl-1.1.0e/sudo ./configsudo makesudo make install

3、第三种安装方式(已经使用此方案,安装成功)

# 查看openssl版本
openssl version

下载openssl

sudo wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz

  解压并编译安装

tar -zxvf openssl-1.1.1c.tar.gz
cd openssl-1.1.1c
./config --prefix=/usr/local/openssl   #如果此步骤报错,需要安装perl以及gcc包
make && make install
mv /usr/bin/openssl /usr/bin/openssl.bak
ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v                    # 设置生效
# 查看openssl版本
openssl version


五、安装nginx 跟rtmp  MP4模块

1.官网下载直接下载.tar.gz安装包

下载Nginx
 

wget http://nginx.org/download/nginx-1.12.1.tar.gz

下载 Rtmp

wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.0.tar.gz


下载flv和mp4支持模块

wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz


2.解压

tar -zxvf nginx-1.12.1.tar.gz

tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz

tar -zxvf v1.2.0.tar.gz

3、修改配置nginx openssl 的配置文件,否则会找不到openssl库


修改配置文件不然会报openssl库找不到错误
修改 /home/nginxdir/nginx-1.12.1/auto/lib/openssl/conf文件

增加下面这个分支替换增加的路径

if [ $ngx_found = no ]; thenngx_feature="OpenSSL library in //root/liushen/nginx/openssl-OpenSSL_1_1_0f/bin/include"ngx_feature_path="/root/liushen/nginx/openssl-OpenSSL_1_1_0f/bin/include"if [ $NGX_RPATH = YES ]; thenngx_feature_libs="-R/root/liushen/nginx/openssl-OpenSSL_1_1_0f/bin/lib -L/root/liushen/nginx/openssl-OpenSSL_1_1_0f/bin/ -lssl -lcrypto $NGX_LIBDL"elsengx_feature_libs="-L/root/liushen/nginx/openssl-OpenSSL_1_1_0f/bin/lib -lssl -lcrypto $NGX_LIBDL"fi. auto/featurefi



增加后截图如下:

这里写图片描述

4、生成Makefile
 

第1种方案直接编译生成带有指定模块功能的nginx服务程序:

./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module-1.2.0 --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_ssl_module --with-pcre=/home/nginx_dir/nginxdir/pcre-8.35 --with-zlib=/home/nginx_dir/nginxdir/zlib-1.2.11 --user=www --group=www --with-http_flv_module --with-http_mp4_module --with-http_stub_status_module

make && make install

注意:

4.1、依赖包的路径,最好是加进去,不过不加也可以

4.2、第2种方案:先编译安装nginx-rtmp-module-1.2.0 ,然后后期再编译nginx_mod_h264_streaming-2.2.7 ,然后删除安装目录中的nginx文件,将/home/nginxdir/nginx-1.12.1/objs下的编译好的nginx文件,复制到安装目录/usr/local/nginx/sbin
4.3、编译时如果报weberror的错误,将/home/nginxdir/nginx-1.12.1/objs/Makefile 中的-weberror删除掉,重新编译,其余的错误,就是那里报错,注释哪里

4.4.修改配置文件。进入 /usr/local/nginx/conf 文件夹。增添rtmp部分

rtmp {server {listen 1935;  #监听的端口 chunk_size 4000;application live {  #rtmp推流请求路径 live on;recorder rec1 {record all manual;record_unique on;record_notify on;record_max_size 512M;#record_interval 0s;record_path /usr/local/nginx/html/flv_file;record_suffix .flv;# exec_record_done /usr/local/nginx/shell/upload2oss.sh $path $filename $basename;}}}
}

http  server块中添加如下部分:

root /usr/local/nginx/html/flv_file/;#limit_rate_after 5m; ####在flv视频文件下载了5M以后开始限速
#limit_rate 512k; ####速度限制为512Kindex index.html;charset utf-8;location ~ \.flv {flv;}location ~ \.mp4$ {
mp4;
}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}location / {root   html;index  index.html index.htm;}location /control {rtmp_control all;}


5、进入 /usr/local/nginx/sbin文件夹,执行./nginx启动nginx

备注:nginx.conf配置文件1内容如下:

user root root;worker_processes  1;
#error_log  logs/error.log;events {worker_connections  1024;
}# RTMP configuration
rtmp {server {listen 1935; # Listen on standard RTMP portchunk_size 4000; # ping 30s;# notify_method get;# This application is to accept incoming streamapplication live {live on; # Allows live inputrecorder rec1{record all manual;#record_unique on;# record_notify on;record_max_size 512M;
#                       record_interval 0s;record_path /home/xujianjun/mp4dir;record_suffix .flv;exec_record_done '/usr/local/bin/ffmpeg' -y -i /home/xujianjun/mp4dir/test1.flv -vcodec libx264 -f mp4 /home/xujianjun/mp4dir/test_record.mp4 2>>/home/xujianjun/mp4dir/test_record.log;
#                       exec_record_done /usr/local/nginx/shell/upload2oss.sh $path $filename $basename;
}# for each received stream, transcode for adaptive streami			# This single ffmpeg command takes the input and transforms# This single ffmpeg command takes the input and transforms# the source into 4 different streams with different bitrates# and qualities. # these settings respect the aspect ratio.
#			exec_push  /usr/local/bin/ffmpeg -i rtmp://localhost:1935/$app/$name -async 1 -vsync -1
#						-c:v libx264 -c:a aac -b:v 256k  -b:a 64k  -vf "scale=480:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_low
#						-c:v libx264 -c:a aac -b:v 768k  -b:a 128k -vf "scale=720:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_mid
#						-c:v libx264 -c:a aac -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_high
#						-c:v libx264 -c:a aac -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_hd720
#						-c copy -f flv rtmp://localhost:1935/show/$name_src;			
#			drop_idle_publisher 10s; }
application vod {play /home/xujianjun/mp4dir;
}# This is the HLS applicationapplication show {live on; # Allows live input from above applicationdeny play all; # disable consuming the stream from nginx as rtmphls on; # Enable HTTP Live Streaminghls_fragment 3;hls_playlist_length 20;hls_path /mnt/hls/;  # hls fragments path# Instruct clients to adjust resolution according to bandwidthhls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolutionhls_variant _hd720 BANDWIDTH=2048000; # High bitrate, HD 720p resolutionhls_variant _high BANDWIDTH=1152000; # High bitrate, higher-than-SD resolutionhls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolutionhls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution# MPEG-DASHdash on;dash_path /mnt/dash/;  # dash fragments pathdash_fragment 3;dash_playlist_length 20;			}}
}http {sendfile off;tcp_nopush on;directio 512;# aio on;# HTTP server required to serve the player and HLS fragmentsserver {listen 8080;location / {root /usr/local/nginx/html/flv_file;index index.html;}        #   location /ts {#           ts_live 1935 app=live;#      }location /control {rtmp_control all;}location /rtmp_stat {rtmp_stat all;rtmp_stat_stylesheet /stat.xsl;}location /xstat {rtmp_stat all;}#        location /sys_stat {#           sys_stat;#      }#     location /live {#           flv_live 1935;#       }location /files {alias /home/xujianjun/mp4dir;autoindex on;autoindex_exact_size off;autoindex_localtime on;add_header Cache-Control no-store;}# Serve HLS fragmentslocation /hls {types {application/vnd.apple.mpegurl m3u8;video/mp2t ts;}root /mnt;add_header Cache-Control no-cache; # Disable cache# CORS setupadd_header 'Access-Control-Allow-Origin' '*' always;add_header 'Access-Control-Expose-Headers' 'Content-Length';# allow CORS preflight requestsif ($request_method = 'OPTIONS') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain charset=UTF-8';add_header 'Content-Length' 0;return 204;}}# Serve DASH fragmentslocation /dash {types {application/dash+xml mpd;video/mp4 mp4;}root /mnt;add_header Cache-Control no-cache; # Disable cache# CORS setupadd_header 'Access-Control-Allow-Origin' '*' always;add_header 'Access-Control-Expose-Headers' 'Content-Length';# Allow CORS preflight requestsif ($request_method = 'OPTIONS') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain charset=UTF-8';add_header 'Content-Length' 0;return 204;}}		# This URL provides RTMP statistics in XMLlocation /stat {rtmp_stat all;rtmp_stat_stylesheet stat.xsl; # Use stat.xsl stylesheet }location /stat.xsl {# XML stylesheet to view RTMP stats.root /usr/local/nginx/html;}}
}

 nginx.conf配置文件参考模板2:

user root root;
#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events 
{worker_connections  1024;
}rtmp 
{      server {    listen 1935;  #监听的端口 chunk_size 4000;application live {  #rtmp推流请求路径 live on;recorder rec1 {record all manual;#record_unique on;#record_notify on;record_max_size 512M;#record_interval 0s;record_path /usr/local/nginx/html/flv_file;record_suffix .flv;# exec_record_done /usr/local/nginx/shell/upload2oss.sh $path $filename $basename;}} application vod {play /usr/local/nginx/html/flv_file/;}}   
} http 
{include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;root /usr/local/nginx/html/flv_file/;#limit_rate_after 5m; ####在flv视频文件下载了5M以后开始限速#limit_rate 512k; ####速度限制为512Kindex index.html;charset utf-8;location ~ \.flv {flv;}location ~ \.mp4$ {mp4;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}location / {root   html;index  index.html;}location /control {rtmp_control all;}location /javawebapps {root html;index index.html;}location /files {alias /usr/local/nginx/html/flv_file;autoindex on;autoindex_exact_size off;autoindex_localtime on;add_header Cache-Control no-store;}location /stat {rtmp_stat all;rtmp_stat_stylesheet stat.xsl; # Use stat.xsl stylesheet }location /stat.xsl {# XML stylesheet to view RTMP statsroot /usr/local/nginx/html;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html##     error_page   500 502 503 504  /50x.html;#      location = /50x.html #      {#           root   html;#      }# proxy the PHP scripts to Apache listening on 127.0.0.1:80##      location ~ \.php$ #      {#           proxy_pass   http://127.0.0.1;#      }# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##      location ~ \.php$ #      {#           root           html;#           fastcgi_pass   127.0.0.1:9000;#           fastcgi_index  index.php;#           fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#           include        fastcgi_params;#      }# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##     location ~ /\.ht #     {#           deny  all;#     }}server {listen       8888;#默认端口是80,如果端口没被占用可以不用修改server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;root        /home/vue_project/dist;#vue项目的打包后的distlocation / {try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404index  index.html index.htm;}#对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件#因此需要rewrite到index.html中,然后交给路由在处理请求资源location @router {rewrite ^.*$ /index.html last;}#只需要将前端相关的html文件和js文件放到以下配置的指定目录即可使用location /{root /home/vue_project/webapps/;index  index.html;}#.......其他部分省略}# another virtual host using mix of IP-, name-, and port-based configuration##server #   {#       listen       8000;#       listen       somename:8080;#       server_name  somename  alias  another.alias;#       location / #       {#           root   html;#           index  index.html index.htm;#       }#    }# HTTPS server##server #    {#       listen       443 ssl;#       server_name  localhost;#       ssl_certificate      cert.pem;#       ssl_certificate_key  cert.key;#       ssl_session_cache    shared:SSL:1m;#       ssl_session_timeout  5m;#       ssl_ciphers  HIGH:!aNULL:!MD5;#       ssl_prefer_server_ciphers  on;#       location / #       {#           root   html;#           index  index.html index.htm;#       }#    }}

搭建期间所碰到的问题:

nginx: [error] open() "/usr/local/nginx//logs/nginx.pid" failed

首先ps aux | grep nginx查看nginx进程号

这里会发现跑了两个,我们要杀死nginx:master   和worker 这2个主进程,我这里的主进程号事27767 27768
使用kill -9 27767杀死

使用kill -9 27768杀死

此问题参考网址1:https://blog.csdn.net/weixin_44352609/article/details/93242894

此问题参考网址2:https://blog.csdn.net/weixin_45525272/article/details/107980445

其实只要正常执行 sudo ./nginx -s stop

再运行 sudo ./nginx

一般都是可以的

参考网址:

centos7 搭建Nginx+rtmp流媒体服务器

CentOS7升级OpenSSL版本

搭建nginx rtmp流媒体服务器(超详细)

nginx搭建mp4、flv流媒体服务器

 RTMP 流媒体服务器视频直播录制并上传至OSS实现

这篇关于centos7.5 Nginx+rtmp 搭建流媒体服务器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/828652

相关文章

流媒体平台/视频监控/安防视频汇聚EasyCVR播放暂停后视频画面黑屏是什么原因?

视频智能分析/视频监控/安防监控综合管理系统EasyCVR视频汇聚融合平台,是TSINGSEE青犀视频垂直深耕音视频流媒体技术、AI智能技术领域的杰出成果。该平台以其强大的视频处理、汇聚与融合能力,在构建全栈视频监控系统中展现出了独特的优势。视频监控管理系统EasyCVR平台内置了强大的视频解码、转码、压缩等技术,能够处理多种视频流格式,并以多种格式(RTMP、RTSP、HTTP-FLV、WebS

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

搭建Kafka+zookeeper集群调度

前言 硬件环境 172.18.0.5        kafkazk1        Kafka+zookeeper                Kafka Broker集群 172.18.0.6        kafkazk2        Kafka+zookeeper                Kafka Broker集群 172.18.0.7        kafkazk3

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

Linux服务器Java启动脚本

Linux服务器Java启动脚本 1、初版2、优化版本3、常用脚本仓库 本文章介绍了如何在Linux服务器上执行Java并启动jar包, 通常我们会使用nohup直接启动,但是还是需要手动停止然后再次启动, 那如何更优雅的在服务器上启动jar包呢,让我们一起探讨一下吧。 1、初版 第一个版本是常用的做法,直接使用nohup后台启动jar包, 并将日志输出到当前文件夹n

pico2 开发环境搭建-基于ubuntu

pico2 开发环境搭建-基于ubuntu 安装编译工具链下载sdk 和example编译example 安装编译工具链 sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib 注意cmake的版本,需要在3.17 以上 下载sdk 和ex

速盾:直播 cdn 服务器带宽?

在当今数字化时代,直播已经成为了一种非常流行的娱乐和商业活动形式。为了确保直播的流畅性和高质量,直播平台通常会使用 CDN(Content Delivery Network,内容分发网络)服务器来分发直播流。而 CDN 服务器的带宽则是影响直播质量的一个重要因素。下面我们就来探讨一下速盾视角下的直播 CDN 服务器带宽问题。 一、直播对带宽的需求 高清视频流 直播通常需要传输高清视频

Windows下Nginx的安装及开机启动

1、将nginx-1.16.1.zip解压拷贝至D:\web\nginx目录下。 2、启动Nginx,两种方法: (1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过。 (2)打开cmd命令窗口,切换到nginx目录下,输入命令 nginx.exe 或者 start nginx ,回车即可。 3、检查nginx是否启动成功。 直接在浏览器地址栏输入网址 http://lo

nginx介绍及常用功能

什么是nginx nginx跟Apache一样,是一个web服务器(网站服务器),通过HTTP协议提供各种网络服务。 Apache:重量级的,不支持高并发的服务器。在Apache上运行数以万计的并发访问,会导致服务器消耗大量内存。操作系统对其进行进程或线程间的切换也消耗了大量的CPU资源,导致HTTP请求的平均响应速度降低。这些都决定了Apache不可能成为高性能WEB服务器  nginx:

web群集--nginx配置文件location匹配符的优先级顺序详解及验证

文章目录 前言优先级顺序优先级顺序(详解)1. 精确匹配(Exact Match)2. 正则表达式匹配(Regex Match)3. 前缀匹配(Prefix Match) 匹配规则的综合应用验证优先级 前言 location的作用 在 NGINX 中,location 指令用于定义如何处理特定的请求 URI。由于网站往往需要不同的处理方式来适应各种请求,NGINX 提供了多种匹