以dynamic module安裝nginx-rtmp @Ubuntu - Linux

Annie avatar
By Annie
at 2017-03-16T13:40

Table of Contents

與 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

--
Tags: Linux

All Comments

Frederica avatar
By Frederica
at 2017-03-20T21:05
招喚obov
Rebecca avatar
By Rebecca
at 2017-03-25T11:00
樓下obov
Oliver avatar
By Oliver
at 2017-03-29T01:34
樓下obov
Agatha avatar
By Agatha
at 2017-03-30T00:13
上面好多obov 樓下繼續當obov
Ophelia avatar
By Ophelia
at 2017-04-02T01:10
恩 沒問題 繼續當obov
Frederic avatar
By Frederic
at 2017-04-05T09:43
我自己還是習慣靜態 只是直接自己包套件就是
Xanthe avatar
By Xanthe
at 2017-04-07T05:55
我之前是這樣做啊 但是下個 apt-get upgrade 就炸掉了
Hardy avatar
By Hardy
at 2017-04-08T14:37
當然我知道有方法把 nginx 排除,但是後來用不到 rtmp
就懶了 最近又想拿回來做點東西 才稍微研究一下 XD
Regina avatar
By Regina
at 2017-04-09T16:22
你編dynamic也是有可能upgrade就爆炸啊wwww
Noah avatar
By Noah
at 2017-04-10T02:33
dynamic不一定所有版本都共通啊w
只要用了任何官方套件包提供的東西總是會有這問題的w
Oscar avatar
By Oscar
at 2017-04-14T21:02
只編 module 比較快嘛 而且我可以在更新前先把 .so 建好
然後改一下 conf 就套用新的 module 了
Bethany avatar
By Bethany
at 2017-04-18T18:20
況且如果是模組改版的話至少我還能抽換 so 不用重包
Rebecca avatar
By Rebecca
at 2017-04-23T08:28
各有個的好壞啦 我的習慣是模組沒重大安全性問題
我只會跟著nginx版本更新XD
Oscar avatar
By Oscar
at 2017-04-26T17:18
蠻好奇各位都是何方神聖的,小弟linux年齡1年

使用 Gnome Wayland 時有辦法選擇 RGB 輸出範圍嗎?

Suhail Hany avatar
By Suhail Hany
at 2017-03-16T00:26
之前用 Xorg 可以使用 xrandr --output HDMI1 --set and#34;Broadcast RGBand#34; and#34;Fulland#34; 來達成目的,但是在 Wayland 下這方法是失效的,導致畫面總是有點灰灰的, intel 內顯 HDMI 的輸出貌似就是有這個 ...

接收WebSocket的套件

George avatar
By George
at 2017-03-15T18:38
Hi, 這是我的一個專案 https://github.com/hn12404988/hast_web/blob/master/README_Chinese.md 是用來在Linux上建立一個接收Websockt的套件 特色是「動態多執行緒」,因為github上大多的websocket伺服器大多是單執行 ...

如何讓Angular2網頁程式在linux自動啟動

Frederica avatar
By Frederica
at 2017-03-14T10:34
如果你要 deploy NodeJS 的 web app 可以考慮使用 https://www.phusionpassenger.com 之類的工具 然後搭配 Nginx 或 Apache 來使用 ※ 引述《z20240 (培根)》之銘言: : 各位技術高超的大大們好, : 第一次在這邊發文,如果有冒犯的地方 ...

samba 設定無密碼瀏覽、私人目錄

Daniel avatar
By Daniel
at 2017-03-13T10:13
目標: 1.當我輸入 samba server 的 IP 時 //192.168.1.254 希望可以出現 home 、public_read、public_write 等目錄, 在這個階段,不用輸入帳號密碼。 2.當我點選 home 目錄時,才會跳出驗證使用者的步驟。 ...

ubuntu 用 iphone 上網問題

Regina avatar
By Regina
at 2017-03-12T23:31
小弟新手最近灌了ubuntu 16.10 用 iphone 透過 USB 分享給筆電網路 我接上 USB 線之後就能正常上網 (沒做過任何設定) 但是發現一個問題,只要線重拔過就無法正常上網(但是右上角的圖示 還是正常顯示 有線網路) 必須重開機才可以正常上網 想請問大大是哪裡出了問題呢? 謝謝~ ...