【折腾】基于nginx的rtmp模块的直播服务器

@Xiaoluo  April 30, 2020

nginx是一个轻量级的web服务程序,它有着很强的功能,除了常用的web的服务外,他还能安装模块,成为直播服务器。
这个东西要怎么安装?具体如下:
**lnmp一键安装包
centos7.x**

首先:我们登录进入我们的系统
软件有很多:例如putty等
安装lnmp(Linux-nginx-mysql-php)
在登陆后,我们默认的目录在/home或者/root,我们直接在这里下载安装包就可以了,不需要切换到其他目录去。
注意:安装完成后,不要删除解压的目录

cd /root   或者 cd /home 
wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz -cO lnmp1.6.tar.gz && tar zxf lnmp1.6.tar.gz && cd lnmp1.6 && ./install.sh lnmp  **#来源https://lnmp.org/install.html**

等待安装完成,等待安装完成后访问http://ip ,如果出现这个画面,就表示,web服务已经安装完成,并已经可以使用了。
lnmp.jpg

安装rtmp扩展模块

git clone  https://github.com/arut/nginx-rtmp-module.git  #用git去下载rtmp模块(提示 git notfound就去安装 yum install git)

mkdir /usr/local/nginx/extend_module #创建nginx扩展的目录
mkdir /usr/local/nginx/extend_module/hls #创建用于临时存放hls的目录
mv nginx-rtmp-module/ /usr/local/nginx/extend_module/nginx-rtmp-module #将下载好的rtmp模块转移到扩展目录

查看下nginx的配置参数

nginx -V

复制下configure arguments后的参数

--user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.6/src/openssl-1.1.1d --with-openssl-opt='enable-weak-ssl-ciphers' 

关闭nginx和php-fpm服务

service nginx stop
service php-fpm stop

进入刚刚下载安装lnmp的目录

cd /root/lnmp1.6/src

解压一下nginx源码

tar -zxvf nginx-1.16.1.tar.gz 

进入目录

cd nginx-1.16.1

执行编译安装:执行./configure带上前面的参数,并在结尾添加 --add-module=这里填rtmp目录

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.6/src/openssl-1.1.1d --with-openssl-opt='enable-weak-ssl-ciphers' --add-module=/usr/local/nginx/extend_module/nginx-rtmp-module

然后执行make

make

如果提示/root/lnmp1.6/src/openssl-1.1.1d: No such file or directory错误,返回src上层目录把openssl-1.1.1d.tar.gz解压再次编译

等待编译后,就可以编译安装了

make install

到这里,rtmp就安装完了
尝试启动下nginx,并查看版本信息

service nginx start
service php-fpm start
nginx -V

如果看到

configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.6/src/openssl-1.1.1d --with-openssl-opt=enable-weak-ssl-ciphers --add-module=/usr/local/nginx/extend_module/nginx-rtmp-module

就表明安装成功了
配置RTMP
接下来需要配置下rtmp的服务
进入目录 cd /usr/local/nginx/conf
编辑下nginx的配置:vi nginx.conf

在结尾后面加入

rtmp {
server {
    listen 1935;
    chunk_size 4096;
    application live {
        live on;
        record off;
        hls on;
        hls_path /usr/local/nginx/extend_module/hls;
        hls_fragment 1s;
        hls_playlist_length 4s;
    }
    }
}

然后,在上面http的server里加入

 location **/你的应用名字** {
            types {
                    application/vnd.apple.mpegurl m3u8;
                    video/mp2t ts;
            }
            alias /usr/local/nginx/extend_module/hls;
            add_header Cache-Control no-cache;
            expires -1;
    }

如果你需要使用到其他端口,请在http里添加

http {
server {
    listen      8080;

    location **/你的应用名** {
        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        root /tmp;
        add_header Cache-Control no-cache;
    }
}

}
编辑完成后保存,退出 (ESC :wq)
重启nginx或者reload

service nginx restart
service nginx reload

注意:需要开放1935端口才能使用

/sbin/iptables -I INPUT -p tcp --dport 1935 -j ACCEPT

查看端口是否处于监听状态

 netstat -natp |grep 1935

状态如下:

tcp  0   0 0.0.0.0:1935   0.0.0.0:*   LISTEN      933/nginx: master p 

如何推流?
使用obs进行推流:地址是 rtmp://ip/你的应用名字/
然后obs的流名称填入自己需要的流名称
如何观看?
可以使用potplyer 或者vlc
填入http://ip/你的应用名称/流名称.m3u8
或者rtmp://ip/你的应用名称/流名称


这里不能写小纸条呢~