▪ 前言

通过 nginx 扩展 nginx-rtmp-module 简单做了一个流媒体直播

有时我们需要在现有的web服务器上增加新的模块实现更为丰富的功能,Nginx 算是比较常用的 web 服务器,但是 nginx 也不仅仅只可以做web服务器,只要有对应的插件还可用作反向代理、即时通讯、文件下载、流媒体服务等功能,如果已经安装好了 Nginx 有不想重新安装覆盖的前提下如何新增模块呢?下面通过安装 nginx-rtmp-module 作为示例进行说明,安装其他模块也是同样的道理。

 

▪ 下载 nginx-rtmp-module

从 GIT 上拷贝 nginx-rtmp-module 源码,然后进行编译安装,下载代码如下:

cd /usr/local
git clone https://github.com/arut/nginx-rtmp-module.git

如果提示 git 错误的,装下:yum install git

这个时候,根目录下应该会有一个 nginx-rtmp-module 文件夹

 

▪ 安装 nginx-rtmp-module

首先我们需要知道 nginx 安装的目录,该文章我们就假设 nginx 是安装在 /usr/local/nginx

1. 查看之前 nginx 的安装参数

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

该命令执行后,系统将我们之前安装好的 Nginx 配置都列了出来:

[root@izbp1c75kyx5u9su57ffvmz ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.13.6
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 --user=nginx --group=nginx --pid-path=/var/runtime/nginx/nginx.pid --lock-path=/var/runtime/nginx/nginx.lock --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-debug --with-http_dav_module --with-http_flv_module --with-http_ssl_module --with-http_xslt_module --with-http_realip_module --with-http_addition_module --with-http_stub_status_module --with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module --with-file-aio --with-mail --with-mail_ssl_module --with-http_gzip_static_module --with-http_perl_module --with-pcre=pcre-8.37 --with-zlib=zlib-1.2.11 --with-ld-opt=-Wl,-E --http-scgi-temp-path=/var/runtime/nginx/temp_scgi --http-uwsgi-temp-path=/var/runtime/nginx/temp_uwsgi --http-proxy-temp-path=/var/runtime/nginx/temp_proxy --http-fastcgi-temp-path=/var/runtime/nginx/temp_fastcgi --http-client-body-temp-path=/var/runtime/nginx/temp_client_body

从上面的 “configure arguments:” 中我们看到之前没有安装过 nginx-rtmp-module 模块。所以我们这次需要扩展并安装。

2. 下载之前已安装的 Nginx 源程序

上面显示我们之前安装的 Nginx 版本是 1.13.6   我们要使用对应版本的 Nginx 的安装包。

cd /usr/local/src
wget http://nginx.org/download/nginx-1.13.6.tar.gz 
tar -zxvf nginx-1.13.6.tar.gz 
cd nginx-1.13.6

3. 重新编译 Nginx 源程序

接下来是重要的一步,基于我们之前已安装过的 Nginx 配置参数上,添加新的模块

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --pid-path=/var/runtime/nginx/nginx.pid --lock-path=/var/runtime/nginx/nginx.lock --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-debug --with-http_dav_module --with-http_flv_module --with-http_ssl_module --with-http_xslt_module --with-http_realip_module --with-http_addition_module --with-http_stub_status_module --with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module --with-file-aio --with-mail --with-mail_ssl_module --with-http_gzip_static_module --with-http_perl_module --with-pcre=pcre-8.37 --with-zlib=zlib-1.2.11 --with-ld-opt=-Wl,-E --http-scgi-temp-path=/var/runtime/nginx/temp_scgi --http-uwsgi-temp-path=/var/runtime/nginx/temp_uwsgi --http-proxy-temp-path=/var/runtime/nginx/temp_proxy --http-fastcgi-temp-path=/var/runtime/nginx/temp_fastcgi --http-client-body-temp-path=/var/runtime/nginx/temp_client_body --add-module=/usr/local/src/nginx-rtmp-module

注意,在配置参数的最后我加了 --add-module=/usr/local/nginx-rtmp-module,这个就是我们需要额外添加的模块

执行编译命令:

make

注意千万不要执行 make install 命令,否则原来的 Nginx 将全部被替换,包括一些配置。

 

4. 替换 Nginx 主程序

编译完成后,我们 /usr/local/src/nginx-1.13.6/objs/ 目录下就能找到一个名为 nginx 的文件( 没有扩展名)

nginx 文件复制到我们之前安装的 /usr/local/nginx/sbin/ 目录,替换旧的 nginx 文件(建议备份一下旧的 nginx 文件),然后重启下 nginx 服务即可

 

5. 验证 Nginx

这个时候我们在查看下nginx 配置

[root@izbp1c75kyx5u9su57ffvmz objs]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.13.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --pid-path=/var/runtime/nginx/nginx.pid --lock-path=/var/runtime/nginx/nginx.lock --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-debug --with-http_dav_module --with-http_flv_module --with-http_ssl_module --with-http_xslt_module --with-http_realip_module --with-http_addition_module --with-http_stub_status_module --with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module --with-file-aio --with-mail --with-mail_ssl_module --with-http_gzip_static_module --with-http_perl_module --with-pcre=pcre-8.37 --with-zlib=zlib-1.2.11 --with-ld-opt=-Wl,-E --http-scgi-temp-path=/var/runtime/nginx/temp_scgi --http-uwsgi-temp-path=/var/runtime/nginx/temp_uwsgi --http-proxy-temp-path=/var/runtime/nginx/temp_proxy --http-fastcgi-temp-path=/var/runtime/nginx/temp_fastcgi --http-client-body-temp-path=/var/runtime/nginx/temp_client_body --add-module=/usr/local/nginx-rtmp-module

在 “configure arguments:” 的最后,我们看到 nginx-rtmp-module 模块已经有了

接下来,我们更改下 nginx.conf 文件,让支持  rtmp

 

▪ 修改 nginx 配置文件

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

在文件里加入下面内容 http{ ... } 段外面(加载在最后面就行,独立模块)

rtmp{
    server{    
        listen 1935;  # 监听的端口  
        chunk_size 4000;  # 数据传输块的大小
         
        application live {  # rtmp 推流请求路径  
            live on;    
            hls on;    
            hls_path /data/httpd/htdocs/rtmp/live;
           hls_fragment 5s;    
        } 
    }    
}

/data/httpd/htdocs/rtmp/live 是一个目录路径,可以自由设置,同时确保目录存在,主要用于存放流文件(需要改权限可读可写的权限)