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

相关文章

mysql中的服务器架构详解

《mysql中的服务器架构详解》:本文主要介绍mysql中的服务器架构,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、mysql服务器架构解释3、总结1、背景简单理解一下mysqphpl的服务器架构。2、mysjsql服务器架构解释mysql的架

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

如何使用Haporxy搭建Web群集

《如何使用Haporxy搭建Web群集》Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多如LVS和Nginx,本案例介绍使用Haproxy及Nginx搭建一套Web群集,感兴趣的... 目录一、案例分析1.案例概述2.案例前置知识点2.1 HTTP请求2.2 负载均衡常用调度算法 2.

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热