学而实习之 不亦乐乎

Linux 中 nginx 添加 nginx-rtmp-module 模块

2023-11-11 19:29:52

一、下载 nginx 和 nginx-rtmp-module

通过 git 命令进行下载 nginx-rtmp-module
// git命令下载,nginx-rtmp-module 的官方 github 地址:https://github.com/arut/nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git

下载 nginx,也可以去官网下载(如果已安装 Nginx,则下载相同版本)
wget http://nginx.org/download/nginx-1.18.0.tar.gz

二、安装 nginx 以及 nginx-rtmp-module

1、Linux 中未安装 nginx

【1】解压 nginx 和 nginx-rtmp-module

$ tar -zxvf nginx-1.18.0.tar.gz      
$ cd nginx-1.18.0

【2】nginx第三方模块安装方法如下:

$ ./configure --prefix=/你的安装目录  --add-module=/第三方模块目录
如:
$ ./configure --prefix=/usr/local/nginx --add-module=/nginx-rtmp-module --with-http_ssl_module  

【3】如果 Linux 系统中要是未安装过 nginx,则执行

$ make && make install

如果已经安装了 nginx,就不能 make install,这样会会覆盖掉原有的安装。

2、linux 系统安装过 nginx,安装第三方模块方法

【1】先查看原有 nginx 的配置参数并拷贝出来

$ /usr/local/nginx/sbin/nginx -V

注意:下载的 Nginx 版本要一致,结果中的配置参数要拷贝下来,configure 时要用,我这里如下:

configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' 

【2】在 ./configure 中添加安装第三方模块语句 –add-module=/root/nginx-rtmp-module。
将【1】中拷贝的配置参数添加上,再加上  –add-module=/root/nginx-rtmp-module,然后执行此语句,要注意环境不同时,这里的参数会不一样,要注意路径。这里的 Nginx 是通过 yum 安装过的。

如下:

./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=../nginx-rtmp-module-master

如果需要安装其他的第三方模块也是一样的,然后执行 make,这里切记不能执行 make install,否则会覆盖原先安装的 nginx。

【3】在编译完 nginx 之后,目录下有个 objs 目录,进入 objs 文件中找到 nginx 文件,复制 nginx 文件到原来安装 nginx 的目录中,如:

$ cp ./nginx /usr/local/nginx/sbin/ 

【4】查看nginx配置:

$ /usr/local/nginx/sbin/nginx -V

我们可以看到 nginx-rtmp-module 模块已经有了。

三、nginx-rtmp-module 实战

编辑 ngixn.conf

$ vi ngixn.conf

在 nginx.conf 文件中添加

rtmp {            
    server {            
        listen 1935;  #监听的端口              
        chunk_size 4000;                        
        application hls {  #rtmp推流请求路径            
            live on;              
            hls on;                
            hls_path /usr/local/nginx/hls;#视频播放位置              
            hls_fragment 5s;          
        }          
    }
}

保存退出,安装配置完毕。