nginx RTMP服务器的搭建

一、下载

1、Nginx:https://github.com/nginx/nginx

2、OpenSSL:https://github.com/openssl/openssl

3、rtmp:https://github.com/arut/nginx-rtmp-module
 

 tips: 找到release界面,右击最新版本,复制连接地址,然后在linux命令行中用wget下载:

#wget https://github.com/nginx/nginx/archive/release-1.19.0.tar.gz

//#wget https://github.com/openssl/openssl/archive/openssl-3.0.0-alpha4.tar.gz
#wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1g.tar.gz

#wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz

当前openssl的最新版本是3.0.0.alpha4, 但实测发现后面配置时报错,因此改用了1.1.1g。

 

二、安装依赖工具(PCRE、ZLIB)

pcre用于重写rewrite,zlib用于gzip压缩。

1. pcre

(注意nginx不支持pcre2,所以当你找到pcre2版本时请忽略)

#wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
#tar zxvf pcre-8.40.tar.gz
#cd pcre-8.40
#./configure
#make
#make install

 安装成功的正确提示:

常见问题:

./configure时如果报错:

error: Invalid C++ compiler or C++ compiler flags

解决办法:

sudo apt-get install build-essential

2. zlib

  zlib适用于数据压缩的函数式库,是一个免费的、通用的、法律上不受阻碍(即没有被任何专利覆盖)的无损数据压缩库。几乎适用于任何计算器硬件和操作系统。

#wget http://www.zlib.net/zlib-1.2.11.tar.gz
#tar zxvf zlib-1.2.11.tar.gz
#cd zlib-1.2.11
#./configure
#make
#make install

三、编译Nginx

1. 编译OpenSSL

./config --prefix=`pwd`/libs
make
make install

 

 常见问题:试过openssl-3.0.0,配置时会报错

2. 编译Nginx

先准备好rtmp和openssl的路径,方便后面拷贝:

 配置编译安装:

./auto/configure --add-module=<路径1> --with-openssl=<路径2>
make
make install

 

Nginx生成目录:/usr/local/nginx

四、配置Nginx

用:nginx-rtmp-module-1.2.1/test/nginx.conf

替换

/usr/local/nginx/conf/nginx.conf

 看下nginx.conf里面配置什么:

worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;  #最大连接数
}

rtmp {
    server {
        listen 1935;  #监听端口

        application myapp {
            live on;

            #record keyframes;
            #record_path /tmp;
            #record_max_size 128K;
            #record_interval 30s;
            #record_suffix .this.is.flv;

            #on_publish http://localhost:8080/publish;
            #on_play http://localhost:8080/play;
            #on_record_done http://localhost:8080/record_done;
        }
    }
}

http {
    server {
        listen      8080;

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

        location /stat.xsl {
            root /path/to/nginx-rtmp-module/;
        }

        location /control {
            rtmp_control all;
        }

        #location /publish {
        #    return 201;
        #}

        #location /play {
        #    return 202;
        #}

        #location /record_done {
        #    return 203;
        #}

        location /rtmp-publisher {
            root /path/to/nginx-rtmp-module/test;
        }

        location / {

        location / {
            root /path/to/nginx-rtmp-module/test/www;
        }
    }
}

                              
  • 最大连接数:1024
  • rtmp服务端口:1935
  • http服务端口:8080
  • rtmp的应用名称:myapp

 

五、启动停止Nginx

1、启动:

#su
#cd /usr/local/nginx/sbin
#./nginx

2、停止:

#./nginx -s stop

六、测试推流

1. 用ifconfig查看rtmp服务器的ip地址

2. 准备一个视频

(这是h264编码的《小妇人》预告片)

3. 用ffmpeg推流:

ffmpeg -re -i xfr.mp4 -vcodec libx264 -acodec aac -f flv rtmp://192.168.4.222/myapp/mystream

4. 用VLC播放:

rtmp://192.168.4.222/myapp/mystream

终于看到我们推送的视频流了!

 

七、附录

ffmpeg下载:http://ffmpeg.org/

VLC下载:https://www.videolan.org/vlc/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章