Ubuntu 搭建Nginx-RTMP流媒体服务器

unix课程的课程设计,不想换个系统环境写代码,就选择了一个服务器搭建的题目,本来以为很成熟了,但是因为太成熟了,已经没有人再继续弄这些了,软件,文档都是上古版本了,Helix已经没有授权密钥了,最后找到了这个文章,并且有B站视频,但是离录制也有年代了,源代码有的找不到了,有的已经换新版了,各处搜集,最终终于在多个设备上看到了自己的桌面直播,为了记录这次的过程,也为了感谢大佬的教学,方便再往后有可能需要的,再按照我的成功过程写如下一个记录文档,并开始养成写文档的好习惯!

操作系统: Kubuntu 20.04

主要参考文档:https://blog.csdn.net/u011298145/article/details/78883598

1.先下载安装  nginx 和 nginx-rtmp 编译依赖工具

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

2. 创建一个工作目录,并切换到工作目录

mkdir ~/working

cd ~/working

3. 下载 nginx 和 nginx-rtmp源码

原步骤是安装1.7.5版本,但是在make && make install时有很多错误,逐个检查后发现无法完全解决,最后发现是版本问题,所以使用了1.20.0版本

参考文档

wget http://nginx.org/download/nginx-1.20.0.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

4. 安装unzip工具,解压下载的安装包

sudo apt-get install unzip

5.解压 nginx 和 nginx-rtmp安装包

tar -zxvf nginx-1.20.0.tar.gz
unzip master.zip

6. 切换到 nginx-目录

cd nginx-1.20.0

7.添加 nginx-rtmp 模板编译到 nginx

./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master

8.编译安装

make && make install

9. 安装nginx init 脚本

原文档的原代码已经过期,无法获取,手动创建脚本文件实现nginx服务器自启动

参考文档

在/etc/init.d/目录下编写一个服务脚本

vim /etc/init.d/nginx

写入代码

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf



#注意:这里的三个变量需要根据具体的环境而做修改。
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid



RETVAL=0
prog="nginx"

# Check that networking is up.
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ]  
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
  echo -n $"Stopping $prog: "
  $nginxd -s stop
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid
}
# reload nginx service functions.
reload() {
  echo -n $"Reloading $prog: "
  kill -HUP `cat ${nginx_pid}`
  RETVAL=$?
  echo
}
# See how we were called.
case "$1" in
  start)
          start
          ;;
  stop)
          stop
          ;;
  reload)
          reload
          ;;
  restart)
          stop
          start
          ;;
  status)
          status $prog
          RETVAL=$?
          ;;
  *)
          echo $"Usage: $prog {start|stop|restart|reload|status|help}"
          exit 1
esac
exit $RETVAL

为所有用户添加权限

sudo chmod a+x /etc/init.d/nginx

在rc.local文件的最后中添加如下的一行: /etc/init.d/nginx start

10.启动nginx

可以通过 /etc/init.d/nginx start或者

service nginx start

进行启动,也可以通过进入 /usr/local/nginx/sbin/ 目录,使用 ./nginx 启动nginx服务

启动不成功,查看错误,如果提示地址已被使用,查看端口占用情况:

netstat -apn

在/usr/local/nginx/conf/nginx.conf更改http的listen地址或者直接杀死占用端口的进程

启动成功后可以在浏览器输入http://本机ip地址:80进入服务器,服务器显示Welcome to nginx!欢迎界面

查看本机ip地址命令:ip addr show

11.配置 Nginx-rtmp 服务器

打开 /usr/local/nginx/conf/nginx.conf

在末尾添加如下 配置

rtmp {

    server {

            listen 1935;

            chunk_size 4096;





            application live {

                    live on;

                    record off;

hls on; 

hls_path /usr/share/nginx/html/hls; 

hls_fragment 2s;



                    exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;

            }

            application live360p {               #该挂载点为360p的压缩画面

                    live on;

                    record off;

        }

application hls360p {     #个人测试,单独开放的rtmp+hls360p挂载点和hls2挂载点的360p hls流

                live on; 

                hls on; 

                hls_path /usr/share/nginx/html/hls2; 

hls_fragment 2s;

            } 

    }

}





#注意:需要创建hls的目录:mkdir -p /usr/share/nginx/html/hls

12. 保存上面配置文件,然后重新启动nginx服务

sudo service nginx restart

13. 如果你使用了防火墙,请允许端口 tcp 1935,81

iptables -A INPUT -p tcp --dport 1935 -j ACCEPT

iptables -A OUTPUT -p tcp --sport 1935 -j ACCEPT

iptables -A INPUT -p tcp --dport 81 -j ACCEPT

iptables -A OUTPUT -p tcp --sport 81 -j ACCEPT

14.安装obs推流工具

官网:https://obsproject.com/zh-cn/download

Linux支持的版本是由ubuntu14.04或者更新版本的官方提供的。 需要FFmpeg支持。对于ubuntu15.04以及之后的版本,FFmpeg被正式包括在内:

sudo apt-get install ffmpeg

在安装完FFmpeg后,使用以下指令安装OBS工作室版:

sudo add-apt-repository ppa:obsproject/obs-studio

sudo apt-get update && sudo apt-get install obs-studio

15.获取 VLC for Ubuntu

官网:https://www.videolan.org/vlc/#download

sudo snap install vlc

16.使用obs推流:

参考文档:https://zhuanlan.zhihu.com/p/144275422

来源点击“+”号,测试使用屏幕采集,点击控件菜单的设置

推流菜单,服务选择自定义,服务器填入rtmp://192.168.43.140(这里填写你的nginx服务器IP)/live传流密钥随意

输出菜单,输出模式选择高级,码率控制选择ABR,比特率600Kbps,关键帧间隔2

17.使用vlc拉流:

点击媒体,选择网络串流,输入地址rtmp://服务器IP地址/live/传流密钥,点击播放

18.手机使用EasyPlayer拉流

自己通过搜索找到了手机的RTMP播放器EasyPlayer

官网:http://www.easydarwin.org/

安卓RTMP版本:http://app.tsingsee.com/EasyRTMPlayer

点击右上角+号,同样输入地址rtmp://服务器IP地址/live/传流密钥,点击首页的图片进入播放

  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值