作者s25g5d4 (function(){})()
看板Linux
标题[心得] 以dynamic module安装nginx-rtmp @Ubuntu
时间Thu Mar 16 13:40:21 2017
与 Apache 不同,Nginx 的模组 (module) 是以静态连结的方式
直接编译进 nginx 执行档中,所以当新增或抽换模组时必须重新
编译 Nginx, 非常不方便。幸好在 Nginx 1.9.11 加入了 dynamic
modules 机制 [1],可以透过 shared object (.so) 於执行期动
态载入额外的模组。
nginx-rtmp-module [2] 为一个处理 rtmp 与 HLS, DASH 等串流
技术的第三方模组,可在 Nginx 中加入直播串流支援。但要安装
nginx-rtmp-module 就必须将其编译进 Nginx 中,在大多数 Linux
发行版本,透过套件库安装的 Nginx 是不含这个模组的,要使用
此模组势必要手动编译 Nginx, 而无法使用套件管理系统管理 Nginx.
但是透过 dynamic modules 机制,我们可以保留透过套件库安装
的 Nginx 版本,同时又可以使用 nginx-rtmp, 但 Nginx 版本要
高於 1.9.11 就是了。动态模组的还有一个好处是抽换版本很简单
,make modules 编完再取代旧 .so 就好。
本文以 Ubuntu 16.04.2 LTS, nginx/1.10.0 为环境。
--
[Step 1]
先把 nginx source 与 nginx-rtmp 抓下来
~ $ apt-get source nginx
~ $ git clone
https://github.com/arut/nginx-rtmp-module/
以下假设抓下来的档案位於
nginx: /home/example/nginx-1.10.0
nginx-rtmp: /home/example/nginx-rtmp-module
[Step 2]
进入 nginx-1.10.0 资料夹,编辑 debian/rules 档案
~ $ cd nginx-1.10.0
nginx-1.10.0 $ vim debian/rules
在 common_configure_flags 结尾加入
--add-dynamic-module=/home/example/nginx-rtmp-module
```
common_configure_flags := \
--with-cc-opt="$(debian_cflags)" \
--with-ld-opt="$(debian_ldflags)" \
--prefix=/usr/share/nginx \
...
...
--with-http_auth_request_module
\
--add-dynamic-module=/home/example/nginx-rtmp-module
```
注意: --with-http_auth_request_module \ 後方的斜线不能少
--add-dynamic-module=/home/example/nginx-rtmp-module 後方不要加斜线
修改 dpkg 编译选项 [3]
nginx-1.10.0 $ export DEB_BUILD_MAINT_OPTIONS="hardening=+all, reproducible=-timeless"
nginx-1.10.0 $ dpkg-buildflags
```
CFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security
CPPFLAGS=-D_FORTIFY_SOURCE=2
CXXFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security
FCFLAGS=-g -O2 -fstack-protector-strong
FFLAGS=-g -O2 -fstack-protector-strong
GCJFLAGS=-g -O2 -fstack-protector-strong
LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
OBJCFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security
OBJCXXFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security
```
检查 CPPFLAGS 中没有 -Wdate-time, LDFLAGS 中没有 -fPIE, -fpie
[Step 3]
安装 build dependencies
nginx-1.10.0 $ sudo apt-get build-dep nginx
设定 nginx 编译选项及环境 [4]
nginx-1.10.0 $ debian/rules config.arch.core
编译 nginx 模组
nginx-1.10.0 $ make -C debian/build-core/ modules
[Step 4]
编译後在 debian/build-core/objs/ 可以找到 ngx_rtmp_module.so
将它复制到 /usr/share/nginx/modules 资料夹下
nginx-1.10.0 $ sudo mkdir /usr/share/nginx/modules
nginx-1.10.0 $ sudo cp debian/build-core/objs/ngx_rtmp_module.so \
/usr/share/nginx/modules
编辑 /etc/nginx/nginx.conf,
在文件开头加入 load_module "modules/ngx_rtmp_module.so";
nginx-1.10.0 $ sudo vim /etc/nginx/nginx.conf
```
user www-data;
worker_processes auto;
pid /run/nginx.pid;
load_module "modules/ngx_rtmp_module.so";
...
...
```
重新启动 nginx
nginx-1.10.0 $ sudo systemctl restart nginx
没有错误讯息的话就是成功了
--
[1]:
https://www.nginx.com/blog/dynamic-modules-nginx-1-9-11/
[2]:
https://github.com/arut/nginx-rtmp-module
[3]:
http://man7.org/linux/man-pages/man1/dpkg-buildflags.1.html
由於我们要编译 shared object, 必须关闭 hardening - pie,
否则编译时会报错:
relocation R_X86_64_PC32 against symbol `ngx_rtmp_core_module'
can not be used when making a shared object; recompile with -fPIC
解决 -fPIE 问题後又会遇到错误:
error: macro "__DATE__" might prevent reproducible builds
error: macro "__TIME__" might prevent reproducible builds
所以还要再补上关闭 reproducible - timeless
[4]: 虽然可能安装在伺服器上的是 nginx-full, nginx-extras 之类的版本
不过这里我们只要编 rtmp 模组,所以直接选 nginix-core 来编就好
有疑虑的话也可以改成 debian/rules config.arch.<full|extras|light|core>
後面 make -C 的资料夹也要改成对应的 debian/build-<full|extras|light|core>
--
1F:推 dkchronos :招唤obov 04/16 19:23
2F:推 cesar0909 :楼下obov04/16 19:42
3F:推 SNGoMMX :楼下obov 04/16 21:50
4F:→ y3k :上面好多obov 楼下继续当obov 04/16 21:53
5F:推 obov :恩 没问题 继续当obov04/16 22:20
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.117.198.141
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Linux/M.1489642831.A.5C5.html
※ 编辑: s25g5d4 (140.117.198.141), 03/16/2017 13:42:33
※ 编辑: s25g5d4 (140.117.198.141), 03/16/2017 13:42:46
6F:推 danny8376: 我自己还是习惯静态 只是直接自己包套件就是 03/16 17:44
7F:→ s25g5d4: 我之前是这样做啊 但是下个 apt-get upgrade 就炸掉了 03/16 18:34
8F:→ s25g5d4: 当然我知道有方法把 nginx 排除,但是後来用不到 rtmp 03/16 18:35
9F:→ s25g5d4: 就懒了 最近又想拿回来做点东西 才稍微研究一下 XD 03/16 18:35
10F:→ danny8376: 你编dynamic也是有可能upgrade就爆炸啊wwww 03/19 22:13
11F:→ danny8376: dynamic不一定所有版本都共通啊w 03/19 22:14
12F:→ danny8376: 只要用了任何官方套件包提供的东西总是会有这问题的w 03/19 22:14
13F:→ s25g5d4: 只编 module 比较快嘛 而且我可以在更新前先把 .so 建好 03/19 23:23
14F:→ s25g5d4: 然後改一下 conf 就套用新的 module 了 03/19 23:23
15F:→ s25g5d4: 况且如果是模组改版的话至少我还能抽换 so 不用重包 03/19 23:24
16F:→ danny8376: 各有个的好坏啦 我的习惯是模组没重大安全性问题 03/20 21:34
17F:→ danny8376: 我只会跟着nginx版本更新XD 03/20 21:34
18F:推 zaramaru2: 蛮好奇各位都是何方神圣的,小弟linux年龄1年 06/23 05:26