ubuntu18.04 nginx编译安装扩展模块nginx-rtmp-module

我的需求:已经安装了nginx了,只(通过第三方源代码编译)添加扩展模块
我用的系统环境是子系统ubuntu18.04
文章时间:2019-11-20
编译原则:
只编译不安装(也就是只make,但是不要make install),
跟已安装nginx版本一致

附带把mp4和flv支持也带上,这个官方已自带,无需第三方,参照
http://nginx.org/en/docs/http/ngx_http_mp4_module.html

编译需要的源码,
nignx官方源码,
http://nginx.org/en/download.html
其次扩展的源码,在下面网址可以查到:
https://www.nginx.com/resources/wiki/modules/
编译前要知道您的nginx版本和之前安装的配置,我这里以1.14.0为例
nginx -V (大写V)
又出来一大堆英文
--with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-DUghaW/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module

下载nginx源码
cd /usr/local/src
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz

cd /usr/local/src/nginx-1.14.0
查看官方自带模块
http://nginx.org/en/docs/ 查看Modules reference部分
./configure --help | grep dynamic

编译环境必备
# 查看zlib是否安装
dpkg -l | grep zlib
sudo apt-get install openssl libssl-dev 已经有
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install libxml2 libxml2-dev 已经有
sudo apt-get install libxslt-dev
# GD gd安装需要需要一点时间
sudo apt-get install libgd-dev
sudo apt-get install zlib1g zlib1g-dev 已经有

下载nginx-rtmp-module源码

mkdir /usr/local/src/rtmq
cd /usr/local/src/rtmq
git clone https://github.com/arut/nginx-rtmp-module.git
或者

cd /usr/local/src
wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
tar -zxvf v1.2.1.tar.gz

# 官方已经自带mp4和flv,不需要这个第三方
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz

返回nginx源码目录
cd /usr/local/src/nginx-1.14.0
./configure \
--prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module \
--with-http_mp4_module \
--with-http_flv_module \
--add-dynamic-module=/usr/local/src/nginx-rtmp-module-1.2.1
# 官方自带with-http_mp4_module所以不需要
--add-dynamic-module=/usr/local/src/nginx_mod_h264_streaming-2.2.7

注意上面扩展版本号1.2.1
# --prefix=/usr/local/src/nginx-1.14.0/ \ #不用这个指定路径
# --with-http_ssl_module \
# --without-http_rewrite_module
注:
--with-http_ssl_module 依赖openssl openssl-devel
--with-http_rewrite_module 依赖 pcre

make -f objs/Makefile modules
或者
make modules

两个make命令相同
最好填写为绝对路径/usr/local/src/nginx-rtmp-module-1.2.1
不然可能报Nginx error, no config found
最后编译出来,so文件会出现在objs文件夹下面

因为编辑了新东西,比如:http_mp4_module http_flv_module
所以要替换nginx主程序,如果只是动态添加nginx-rtmp-module是不需要替换nginx的
如果你不知道nginx放哪里了,
你可以
vim /lib/systemd/system/nginx.service
文件找到他
我这里是
ExecStartPre=/usr/sbin/nginx
所以
备份旧的
cp /usr/sbin/nginx /usr/sbin/nginx.bak
替换新的
cp ./objs/nginx /usr/sbin/nginx

cp ./objs/ngx_rtmp_module.so /usr/lib/nginx/modules/ngx_rtmp_module.so
echo "load_module modules/ngx_rtmp_module.so;" >> /usr/share/nginx/modules-available/mod-rtmp.conf
ln -s /usr/share/nginx/modules-available/mod-rtmp.conf /etc/nginx/modules-enabled/mod-rtmp.conf

安装完以后,以下这个为状态配置例子
/usr/local/src/nginx-rtmp-module-1.2.1/stat.xsl

你可以自己指定别的路径,在nginx配置。

题外,只是额外理解:
NGINX > 1.9.11 动态加载模块 load_module 方法,也就是说,在nginx.conf中动态启用
现在默认版本都是1.14以上
以ubuntu18.04为例,别的系统未知。
模块存放路径
/usr/lib/nginx/modules 软链接 /usr/share/nginx/modules
模块配置路径,里面的每个模块对应一个*.conf文件
/usr/share/nginx/modules-available/***.conf 软链接 /etc/nginx/modules-enabled/***.conf

官方默认扩展如下
ngx_http_geoip_module
ngx_http_image_filter_module
ngx_http_xslt_filter_module
ngx_mail_module
ngx_stream_module

nginx 1.1.3之后已经默认支持mp4,flv模块,无须第三方模块支持,确定是
https://www.linuxidc.com/Linux/2016-07/133143.htm?utm_source=tuicool

在文件: h264源码目录/src/ngx_http_streaming_module.c
将如下部分注释掉:
/* TODO: Win32 */

if (r->zero_in_uri)
{
return NGX_DECLINED;
}

在文件: nginx源码目录/objs/Makefile
删除参数 -Werror

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注