Linux搭建流服务器笔记

一、环境安装

CentOS

1、安装wget,yum install -y wget;

2、安装基础环境,yum -y install gcc gcc-c++ autoconf automake make;

3、下载安装nginx,wget http://nginx.org/download/nginx-1.15.0.tar.gz;

4、解压nginx,tar -zxvf nginx-1.15.0.tar.gz;

5、移动解压目录到 /usr/local/,mv nginx-1.15.0 /usr/local/;

6、使用git下载nginx-rtmp-module,git clone https://github.com/arut/nginx-rtmp-module.git(如果没有安装git使用yum进行安装,yum install git)

7、编译和安装(如果出现PCRE错误,执行yum -y install pcre-devel openssl openssl-devel);

cd /usr/local/nginx-1.15.0
./configure
make
make install

8、从/usr/local/nginx(默认路径)目录执行nginx,/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf;

9、打开浏览器,输入localhost:80尝试访问;

Ubuntu

1、新建一个目录/etc/rtmpServer(下载路径),mkdir -P /etc/rtmpServer;

2、安装基础环境,apt-get update,apt-get install libpcre3 libpcre3-dev,apt-get install openssl libssl-dev;

4、安装nginx,wget http://nginx.org/download/nginx-1.8.1.tar.gz;

5、解压,tar -zxvf nginx-1.8.1.tar.gz;

6、使用git下载nginx-rtmp-module,git clone https://github.com/arut/nginx-rtmp-module.git

7、配置并编译nginx,cd nginx-1.8.1,./configure --add-module=../nginx-rtmp-module,make,sudo make install;

8、进入/usr/local/nginx,运行命令./sbin/nginx(重载为./sbin/nginx -s reload);

9、打开浏览器,输入localhost:80尝试访问;

二、点播服务器配置(CentOS与Ubuntu同)

worker_processes  1;
events {
   worker_connections  1024;
}
rtmp {                #RTMP服务
   server {
       listen 1935;  #//服务端口
   chunk_size 4096;   #//数据传输块的大小


   application vod {
       play /opt/video/vod; #//视频文件存放位置。
   }
   }
}
http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  65;
   server {
       listen       80;
       server_name  localhost;
       location / {
           root   html;
           index  index.html index.htm;
       }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }
   }
}

三、直播服务器配置

worker_processes  1;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
    chunk_size 4096;

    application vod {
        play /opt/video/vod;
    }

    application live{ #第一处添加的直播字段
        live on;
    }
    }

}

http {
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen      80;
        server_name  localhost;

    location /stat {    #第二处添加的location字段。
            rtmp_stat all;
        rtmp_stat_stylesheet stat.xsl;
    }

    location /stat.xsl { #第二处添加的location字段。
       root /etc/rtmpServer/nginx-rtmp-module/;
   }

        location / {
            root  html;
            index  index.html index.htm;
        }

        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root  html;
        }
    }
}

四、实时回看视频服务器配置

可以在对应目录下看到ts文件和m3u8文件即为视频片段及目录,用拉流软件拉取即可,地址类似:http://localhost/live/rtmp/index.m3u8。如果用h5观看,html文件需放在nginx容器中,且src需要填写服务器的ip地址,不能填写localhost。

参考h5代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link href="http://vjs.zencdn.net/5.19/video-js.min.css" rel="stylesheet">
<script src="http://vjs.zencdn.net/5.19/video.min.js"></script>
</head>
<body>
<video
id="my-player"
class="video-js"
controls
preload="auto"
poster="//vjs.zencdn.net/v/oceans.png"
data-setup='{}'>
<source src='rtmp://你的服务器IP/live/rtmp' type='rtmp/flv'/> (关注此行!!!)
</p>
</video>
<script type="text/javascript">
var player = videojs('my-player');
var options = {};

var player = videojs('my-player', options, function onPlayerReady() {
videojs.log('Your player is ready!');
// In this context, `this` is the player that was created by Video.js.
this.play();
// How about an event listener?
this.on('ended', function() {
videojs.log('Awww...over so soon?!');
});
});

</script>
</body>
</html>

五、注意点

1、添加完成后需要重新启动nginx,由于这次nginx需要向服务器写切片视频文件,但nginx我又没有给nginx指定用户名只能走默认的nobody用户和nogroup用户组,其实就是没有组。所以我对需要写入的目录做了增大权限的修改。

2、默认形成RTMP的live流,可以通过推流软件修改流的名字,比如OBS里的输出设置。

3、Ubuntu安装拉流软件vlc时,可通过snap install vlc完成。

4、CentOS如果已经装了Apache的,需要通过systemctl stop httpd,systemctl start nginx命令(Ubuntu类似)。

5、注意防火墙是否关闭,CentOS的firewalld(systemctl diable firewalld),Ubuntu的ufw(ufw diable)。

6、下载推流软件,Ubuntu可以通过更新安装源下载,

sudo add-apt-repository ppa:obsproject/obs-studio //添加源
sudo apt-get update //更新源
sudo apt install obs-studio //安装

7、记得安装ffmpeg。

附件:完整nginx配置内容


#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 4096;  

   application vod {
       play /opt/video/vod; 
   }

    application live{        
        live on;
    hls on;
    wait_key on;
    hls_path /opt/video/hls;
    hls_fragment 5s;
    hls_playlist_length 60s;
    hls_continuous on;
    hls_cleanup on;
    hls_nested on;
    }  
 
   }
}

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;


    location /stat {    
            rtmp_stat all;
        rtmp_stat_stylesheet stat.xsl;
    }

    location /stat.xsl { 
       root /etc/rtmpServer/nginx-rtmp-module/;
   }

    location /live{
        types{
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
    }
    alias /opt/video/hls;
    expires -1;
    add_header Cache-Control no-cache;
}

        location / {
            root   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;
        }

        # 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;
        #}
    }


    # 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;
    #    }
    #}

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值