最近几年,直播行业比较火,不管是传统行业的直播,仍是购物、游戏、教育,都在涉及直播。做为在互联网行业奋斗了多年的小伙伴,你有没有想过若是使用Nginx搭建一套直播环境,那咱们该如何搭建呢?别急,接下来,咱们就一块儿使用Nginx来搭建一套直播环境。html
注意:这里以CentOS 6.8服务器为例,以root用户身份来安装Nginx。nginx
yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel* libaio libaio-devel bzr libtool
wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz tar -zxvf openssl-1.0.2s.tar.gz cd /usr/local/src/openssl-1.0.2s ./config --prefix=/usr/local/openssl-1.0.2s make make install
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz tar -zxvf pcre-8.43.tar.gz cd /usr/local/src/pcre-8.43 ./configure --prefix=/usr/local/pcre-8.43 make make install
wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd /usr/local/src/zlib-1.2.11 ./configure --prefix=/usr/local/zlib-1.2.11 make make
nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-modulec++
使用命令:git
git clone https://github.com/arut/nginx-rtmp-module.git
wget http://nginx.org/download/nginx-1.19.1.tar.gz tar -zxvf nginx-1.19.1.tar.gz cd /usr/local/src/nginx-1.19.1 ./configure --prefix=/usr/local/nginx-1.19.1 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --add-module=/usr/local/src/nginx-rtmp-module --with-http_ssl_module make make install
这里须要注意的是:安装Nginx时,指定的是openssl、pcre和zlib的源码解压目录,安装完成后Nginx配置文件的完整路径为:/usr/local/nginx-1.19.1/conf/nginx.conf。github
配置Nginx主要是对Nginx的nginx.conf文件进行配置,咱们能够在命令行输入以下命令编辑nginx.conf文件。面试
vim /usr/local/nginx-1.19.1/conf/nginx.conf
在文件中添加以下内容。vim
rtmp { server { listen 1935; #监听的端口 chunk_size 4096; application hls { #rtmp推流请求路径 live on; hls on; hls_path /usr/share/nginx/html/hls; hls_fragment 5s; } } }
其中,hls_path须要可读可写的权限。接下来,咱们建立/usr/share/nginx/html/hls 目录。设计模式
mkdir -p /usr/share/nginx/html/hls chmod -R 777 /usr/share/nginx/html/hls
接下来,修改http中的server模块:bash
server { listen 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #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; } }
而后启动Nginx:服务器
/usr/local/nginx-1.19.1/sbin/nginx -c /usr/local/nginx-1.19.1/conf/nginx.conf
OBS(Open Broadcaster Software) 是以互联网流媒体直播内容为目的免费和开放源码软件。须要下载这个软件,借助这个软件进行推流(电脑没有摄像头的貌似安装不了。。。)
OBS的下载连接为:https://obsproject.com/zh-cn/download。
安装后,桌面上会有一个以下所示的图表。
打开后咱们须要有一个场景,而且在这个场景下有一个流的来源(能够是窗口,若是选的是视频则会自动识别摄像头),接下来就是设置了。
在配置中最须要关注的就是流的配置,因为是自建的流媒体服务器因此咱们按照以下所示的方式进行配置。
rtmp://你的服务器ip:端口(1935)/live #URL填写流的地址
设置完成咱们就能够 开始推流了。
推荐一个拉流的测试地址,里面针对各类协议都能测试拉流测试,须要注意图中几个地方,因为咱们使用的rtmp协议,咱们选择这一栏,底下填写咱们推流的地址和咱们在上面obs的设置里面配置的流的名称,start, ok搞定!!!
关注「 冰河技术 」微信公众号,后台回复 “设计模式” 关键字领取《深刻浅出Java 23种设计模式》PDF文档。回复“Java8”关键字领取《Java8新特性教程》PDF文档。
好了,今天就聊到这儿吧!别忘了点个赞,给个在看和转发,让更多的人看到,一块儿学习,一块儿进步!!
若是你以为冰河写的还不错,请微信搜索并关注「 冰河技术 」微信公众号,跟冰河学习高并发、分布式、微服务、大数据、互联网和云原生技术,「 冰河技术 」微信公众号更新了大量技术专题,每一篇技术文章干货满满!很多读者已经经过阅读「 冰河技术 」微信公众号文章,吊打面试官,成功跳槽到大厂;也有很多读者实现了技术上的飞跃,成为公司的技术骨干!若是你也想像他们同样提高本身的能力,实现技术能力的飞跃,进大厂,升职加薪,那就关注「 冰河技术 」微信公众号吧,天天更新超硬核技术干货,让你对如何提高技术能力再也不迷茫!