作者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>
--
推 dkchronos :招喚obov 04/16 19:23
推 cesar0909 :樓下obov04/16 19:42
推 SNGoMMX :樓下obov 04/16 21:50
→ y3k :上面好多obov 樓下繼續當obov 04/16 21:53
推 obov :恩 沒問題 繼續當obov04/16 22:20
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.117.198.141
※ 文章網址: https://www.ptt.cc/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
推 danny8376: 我自己還是習慣靜態 只是直接自己包套件就是 03/16 17:44
→ s25g5d4: 我之前是這樣做啊 但是下個 apt-get upgrade 就炸掉了 03/16 18:34
→ s25g5d4: 當然我知道有方法把 nginx 排除,但是後來用不到 rtmp 03/16 18:35
→ s25g5d4: 就懶了 最近又想拿回來做點東西 才稍微研究一下 XD 03/16 18:35
→ danny8376: 你編dynamic也是有可能upgrade就爆炸啊wwww 03/19 22:13
→ danny8376: dynamic不一定所有版本都共通啊w 03/19 22:14
→ danny8376: 只要用了任何官方套件包提供的東西總是會有這問題的w 03/19 22:14
→ s25g5d4: 只編 module 比較快嘛 而且我可以在更新前先把 .so 建好 03/19 23:23
→ s25g5d4: 然後改一下 conf 就套用新的 module 了 03/19 23:23
→ s25g5d4: 況且如果是模組改版的話至少我還能抽換 so 不用重包 03/19 23:24
→ danny8376: 各有個的好壞啦 我的習慣是模組沒重大安全性問題 03/20 21:34
→ danny8376: 我只會跟著nginx版本更新XD 03/20 21:34
推 zaramaru2: 蠻好奇各位都是何方神聖的,小弟linux年齡1年 06/23 05:26