centos7上通过nginx+ffmpeg搭建rtsp直播流转rtmp视频服务器

准备:

操作系统:CentOS 7
Web服务器:Nginx
版本控制器:Git

搭建:

大部分视频直播平台使用的协议都是RTMP,Nginx也有RTMP的扩展模块,所以我也选用RTMP进行服务器的搭建。

1.获取nginx-rtmp-module模块扩展

1.1.nginx-rtmp-module的Git地址是https://github.com/arut/nginx-rtmp-module.git,直接获取:

git clone https://github.com/arut/nginx-rtmp-module.git

2.安装Nginx

2.1.下载Nginx

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

2.2.解压

tar -zxvf nginx-1.15.7.tar.gz

2.3.编译并安装

cd nginx-1.15.7

./configure --prefix={Nginx安装路径}  --add-module={nginx-rtmp-module的路径}  --with-http_ssl_module

make && make install

编译安装过程中可能会出错,一般错误都是缺少一些组件,根据报错信息使用 yum install 进行安装。

3.配置Nginx

3.1.修改Nginx配置

vim {Nginx安装路径}/conf/nginx.conf

打开Nginx配置文件,加入如下部分:

rtmp {   
    server {   
        listen 1935;  #监听的端口 
        chunk_size 4000;   #流整合的最大的块大小,这个值设置的越大 CPU 负载就越小
        #增加对hls的支持
        application hls {  #rtmp推流请求路径
            live on;   #开启实时
            hls on;   #开启hls
            hls_path /usr/share/nginx/html/hls; #推流文件保存的路径,要有写入权限
            hls_fragment 5s;   # 每个文件包含5秒的视频内容
        }   
    }   
} 
Nginx中RTMP模块配置的更多参数可以参考官方文档,到这里Nginx的配置已经完成并且加入了RTMP模块的扩展。

4.启动Nginx

{Nginx安装路径}/sbin/nginx -c {Nginx安装路径}/conf/nginx.conf 
使用浏览器访问服务器,如果出现以下画面表示Nginx成功启动。

在这里插入图片描述
Linux命令

systemctl status firewalld		//查看防火墙状态 
systemctl start firewalld  		//开启防火墙 
systemctl stop firewalld		//关闭防火墙 
service firewalld start 		//开启防火墙 
若遇到无法开启
先用:systemctl unmask firewalld.service 
然后:systemctl start firewalld.service
firewall-cmd --query-port=666/tcp        //查询666端口是否开放;提示 yes,表示开启;no表示未开启。
firewall-cmd --add-port=666/tcp --permanent //添加指定需要开放的端口
firewall-cmd --reload		//重载入添加的端口
firewall-cmd --query-port=666/tcp 	//查询指定端口是否开启成功

4.配置nginx的service服务

vim /etc/init.d/nginx 将下面的代码复制进去保存

#!/bin/bash  
# nginx Startup script for the Nginx HTTP Server  
#  
# 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  
nginxlog=/var/log/nginx/activity_screen
nginxd=/usr/local/nginx/sbin/nginx  
nginx_config=/usr/local/nginx/conf/nginx.conf  
nginx_pid=/usr/local/nginx/logs/nginx.pid  
RETVAL=0  
prog="nginx" 
# Source function library.  
. /etc/init.d/functions
# Source networking configuration.  
. /etc/sysconfig/network  
# Check that networking is up.  
[ ${NETWORKING} = "no" ] && exit 0  
[ -x $nginxd ] || exit 0  
[ -d $nginxlog ] || mkdir -p $nginxlog  
# Start nginx daemons functions.  
start() {  
if [ -e $nginx_pid ];then 
   echo "nginx already running...." 
   exit 1  
fi  
   echo -n $"Starting $prog: " 
   daemon $nginxd -c ${nginx_config}  
   RETVAL=$?  
   echo  
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx  
   return $RETVAL  
}  
# Stop nginx daemons functions.  
stop() {  
        echo -n $"Stopping $prog: " 
        killproc $nginxd  
        RETVAL=$?  
        echo  
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid  
}  
# reload nginx service functions.  
reload() {  
    echo -n $"Reloading $prog: " 
   $nginxd -s reload  
    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 



切换目录,启动服务

cd /etc/init.d

chmod +x nginx

/sbin/chkconfig --level 345 nginx on

service nginx restart  可选  start | stop | restart | reload | status |  help

5.设置开机启动

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

6.启动nginx服务

sudo service nginx start
sudo service nginx stop
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值