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

By Annie
at 2017-03-16T13:40
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>
--
--
直接編譯進 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

By Frederica
at 2017-03-20T21:05
at 2017-03-20T21:05

By Rebecca
at 2017-03-25T11:00
at 2017-03-25T11:00

By Oliver
at 2017-03-29T01:34
at 2017-03-29T01:34

By Agatha
at 2017-03-30T00:13
at 2017-03-30T00:13

By Ophelia
at 2017-04-02T01:10
at 2017-04-02T01:10

By Frederic
at 2017-04-05T09:43
at 2017-04-05T09:43

By Xanthe
at 2017-04-07T05:55
at 2017-04-07T05:55

By Hardy
at 2017-04-08T14:37
at 2017-04-08T14:37

By Regina
at 2017-04-09T16:22
at 2017-04-09T16:22

By Noah
at 2017-04-10T02:33
at 2017-04-10T02:33

By Oscar
at 2017-04-14T21:02
at 2017-04-14T21:02

By Bethany
at 2017-04-18T18:20
at 2017-04-18T18:20

By Rebecca
at 2017-04-23T08:28
at 2017-04-23T08:28

By Oscar
at 2017-04-26T17:18
at 2017-04-26T17:18
Related Posts
使用 Gnome Wayland 時有辦法選擇 RGB 輸出範圍嗎?

By Suhail Hany
at 2017-03-16T00:26
at 2017-03-16T00:26
接收WebSocket的套件

By George
at 2017-03-15T18:38
at 2017-03-15T18:38
如何讓Angular2網頁程式在linux自動啟動

By Frederica
at 2017-03-14T10:34
at 2017-03-14T10:34
samba 設定無密碼瀏覽、私人目錄

By Daniel
at 2017-03-13T10:13
at 2017-03-13T10:13
ubuntu 用 iphone 上網問題

By Regina
at 2017-03-12T23:31
at 2017-03-12T23:31