搭建nginx+rtmp环境

1,准备一个linux服务器,我这里选择的系统是CentOS7,最小安装。
2,确认服务器是否可以联网,如不能,需配置网卡参数。
3,通过yum安装wget
   
 yum -y install wget
4,安装make
   
yum -y install gcc automake autoconf libtool make
5,安装g++
   
yum install gcc gcc-c++
6,下载nginx到/usr/local/src/并解压缩,我一般把安装源文件放在src这个文件夹
   
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
7,安装git,通过git下载rtmp插件
   
yum -y intall git
git clone https://github.com/arut/nginx-rtmp-module
8,安装nginx过程需要依赖 openssl,我们需要先安装
  
 yum -y install openssl openssl-devel
9,安装nginx和插件
  
./configure --prefix=/usr/local/nginx  --add-module=/usr/local/src/nginx-rtmp-module/  --with-http_ssl_module 
make && make install
10,检测是否安装成功
  
 /usr/local/nginx/sbin/nginx -V
    显示为
   
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/src/nginx-rtmp-module/ --with-http_ssl_module
11,至此,nginx安装完成,我们先启动nginx试试
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    这时候在浏览器中输入ip应该可以访问到nginx的主页
 

    如果,不能访问到,那可能被防火墙屏蔽了,需配置防火墙规则,这里我们先关闭防火墙
    systemctl stop firewalld.service
13,/home下创建一个hls文件夹,用于保存推流的数据
    mkdir hls
    给hls赋予读写权限
    chmod 777 hls
12,修改nginx配置(/usr/local/nginx/conf/nginx.conf),添加rtmp配置信息
    我们复制一份配置信息为rtmp.conf
    cp nginx.conf rtmp.conf
    具体的配置为:
#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 4000; 
        application hls {  #rtmp推流请求路径 
            live on;   
            hls on;   
            hls_path /home/hls;   
            hls_fragment 5s;   
        }
    }
}


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

	location /stat { 
            rtmp_stat all; 
            rtmp_stat_stylesheet stat.xsl; 
        } 
        location /stat.xsl { 
            root /usr/local/src/nginx-rtmp-module/; 
        }
        location /control { 
            rtmp_control all; 
        } 
        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /home;
            add_header Cache-Control no-cache;
        }

    }


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

}
13,验证配置文件的准确性
    /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/rtmp.conf
nginx: the configuration file /usr/local/nginx/conf/rtmp.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/rtmp.conf test is successful
14,使用rtmp.conf启动nginx,如果进程中已经存在nginx,需先关闭(kill -TERM 进程号)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/rtmp.conf  
15,查看进程,检测nginx是否启动成功
    ps -ef|grep nginx
16,通过浏览器查nginx是否启动成功

17,配置obs

直播文件名称这里配置为test
所以播放路径为: http://192.168.3.217/hls/test.m3u8
设置来源后,点击开始串流按钮,开始推流到服务器。

18,在win10自带的Edge浏览器中输入播放地址: http://192.168.3.217/hls/test.m3u8,即可播放。

19,设置nginx开机启动

到此,nginx+rtmp服务器搭建成功!
相关文章
相关标签/搜索