前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx实现最简单的直播平台

nginx实现最简单的直播平台

作者头像
DriverZeng
发布2022-09-26 10:09:44
6210
发布2022-09-26 10:09:44
举报
文章被收录于专栏:Linux云计算及前后端开发

-曾老湿, 江湖人称曾老大。


-多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作。 -擅长Web集群架构与自动化运维,曾负责国内某大型金融公司运维工作。 -devops项目经理兼DBA。 -开发过一套自动化运维平台(功能如下): 1)整合了各个公有云API,自主创建云主机。 2)ELK自动化收集日志功能。 3)Saltstack自动化运维统一配置管理工具。 4)Git、Jenkins自动化代码上线及自动化测试平台。 5)堡垒机,连接Linux、Windows平台及日志审计。 6)SQL执行及审批流程。 7)慢查询日志分析web界面。


环境准备

代码语言:javascript
复制
#系统版本
[root@centos7 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

#selinux状态,如果开启,则关闭
[root@centos7 ~]# getenforce
Enforcing

#关闭selinux(临时关闭)
[root@centos7 ~]# setenforce 0
#永久关闭
[root@centos7 ~]# vim /etc/sysconfig/selinux
#将SELINUX=enforcing 替换为 SELINUX=disabled

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

#关闭防火墙
[root@centos7 ~]# systemctl stop firewalld

#IP地址
[root@centos7 ~]# hostname -I
10.0.0.100

nginx直播插件准备

代码语言:javascript
复制
#安装git命令
[root@centos7 ~]# yum install -y git

#git拉取nginx-rtmp插件
[root@centos7 ~]# git clone https://github.com/arut/nginx-rtmp-module.git
正克隆到 'nginx-rtmp-module'...
remote: Enumerating objects: 4314, done.
remote: Total 4314 (delta 0), reused 0 (delta 0), pack-reused 4314
接收对象中: 100% (4314/4314), 3.10 MiB | 14.00 KiB/s, done.
处理 delta 中: 100% (2686/2686), done.

#查看克隆的插件
[root@centos7 ~]# ll
总用量 4
drwxr-xr-x. 7 root root 4096 5月   3 12:37 nginx-rtmp-module

源码安装nginx

代码语言:javascript
复制
#安装上传下载命令
[root@centos7 ~]# yum install -y lrzsz
#上传nginx的包,或者去官网下载
[root@centos7 ~]# rz
[root@centos7 ~]# wget http://nginx.org/download/nginx-1.10.0.tar.gz
#查看上传的包
[root@centos7 ~]# ll
总用量 896
-rwxr-xr-x. 1 root root 911509 6月   1 2017 nginx-1.10.3.tar.gz
drwxr-xr-x. 7 root root   4096 5月   3 12:37 nginx-rtmp-module
#解压nginx安装包
[root@centos7 ~]# tar xf nginx-1.10.3.tar.gz
#进入nginx目录
[root@centos7 ~]# cd nginx-1.10.3
#安装nginx依赖包
[root@centos7 nginx-1.10.3]# yum install pcre-devel openssl-devel gcc gcc-c++ -y
#创建nginx用户
[root@centos7 nginx-1.10.3]# useradd nginx -s /sbin/nologin -M
#生成nginx编译文件
[root@centos7 nginx-1.10.3]# ./configure --user=nginx --group=nginx --with-http_ssl_module --prefix=/usr/local/nginx --add-module=/root/nginx-rtmp-module
#编译
[root@centos7 nginx-1.10.3]# make
#安装
[root@centos7 nginx-1.10.3]# make instal

启动并配置nginx

代码语言:javascript
复制
#进入nginx安装目录
[root@centos7 nginx-1.10.3]# cd /usr/local/nginx/
#查看文件
[root@centos7 nginx]# ll
总用量 4
#配置文件目录
drwxr-xr-x. 2 root root 4096 5月   3 12:53 conf
#站点目录
drwxr-xr-x. 2 root root   40 5月   3 12:53 html
#日志目录
drwxr-xr-x. 2 root root    6 5月   3 12:53 logs
#程序目录
drwxr-xr-x. 2 root root   19 5月   3 12:53 sbin

#编辑nginx配置文件
[root@centos7 nginx]# vim conf/nginx.conf
worker_processes 1;

events {

worker_connections 1024;

}

rtmp {

    server {

        listen 1935;

        chunk_size 4096;

        application live {

            live on;

            hls on;

            hls_path /usr/local/nginx/html/live;

            hls_fragment 5s;
        }

    }

}



http {

    include mime.types;

    default_type application/octet-stream;

    sendfile on;

    keepalive_timeout 65;

    server {

        listen 80;

        server_name localhost;

        location /live {

            types {

            application/vnd.apple.mpegurl m3u8;

            video/mp2t ts;

            }

        alias /usr/local/nginx/html/live;

        expires -1;

        add_header Cache-Control no-cache;

        }

        location / {

        root html;

        index index.html index.htm;

        }

    }

}

#检查nginx语法
[root@centos7 nginx]# /usr/local/nginx/sbin/nginx -t
#启动nginx
[root@centos7 nginx]# /usr/local/nginx/sbin/nginx

#检测nginx端口
[root@centos7 nginx]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      4898/nginx: master
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4898/nginx: master

#检测nginx进程
[root@centos7 nginx]# ps -ef|grep [n]ginx
root       4898      1  0 13:01 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4899   4898  0 13:01 ?        00:00:00 nginx: worker process
nginx      4900   4898  0 13:01 ?        00:00:00 nginx: cache manager process

使用EV录屏实现推流

串流地址:rtmp://10.0.0.100/live 地址秘钥:zls

这里地址秘钥随便填写

如果此时开启直播,那么访问http://10.0.0.100/live/zls.m38u可以下载一个直播视频文件

那么此时,你离成功又近了一步

代码语言:javascript
复制
#编辑直播前端页面
[root@centos7 ~]# vim /usr/local/nginx/html/index.html
<!DOCTYPE html>

<html lang="zh-CN">

<head>

<meta charset="UTF-8">

<title>前端播放m3u8格式视频</title>

<link rel="stylesheet" href="http://vjs.zencdn.net/5.5.3/video-js.css">

<script src="http://vjs.zencdn.net/5.5.3/video.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.12.2/videojs-contrib-hls.js"></script>

</head>

<body>

<video id="myVideo" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="1080" height="708" data-setup='{}'>

<source id="source" src="http://10.0.0.100/live/zls.m3u8" type="application/x-mpegURL">

</video>

</body>

<script>

// videojs 简单使用

var myVideo = videojs('myVideo',{

bigPlayButton : true,

textTrackDisplay : false,

posterImage: false,

errorDisplay : false,

})

myVideo.play() // 视频播放

myVideo.pause() // 视频暂停

</script>

</html>

打开浏览器,访问:http://10.0.0.100

就可以看到直播的界面了

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-12-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境准备
  • nginx直播插件准备
  • 源码安装nginx
  • 启动并配置nginx
  • 使用EV录屏实现推流
相关产品与服务
云直播
云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档