前言
- 最近接手了一個(gè)跟視頻監(jiān)控相關(guān)的項(xiàng)目岖沛,用了近年來(lái)越來(lái)越流行的 Web 服務(wù)器 nginx 加上 nginx-rtmp-module 搭建 rtmp 服務(wù)器鲜滩。使用了阿里云的服務(wù)器,系統(tǒng)
Ubuntu 16.04 鸿市。
步驟
- 更新源并安裝 nginx 奈泪。
sudo apt-get update
sudo apt-get install nginx
-
安裝完成后在瀏覽器打開(kāi)服務(wù)器 IP,看到下圖則說(shuō)明安裝成功灸芳。
- 輸入nginx -V 查看 nginx 版本,可以看到當(dāng)前版本號(hào)是 1.10.3拜姿,且可以看到編譯選項(xiàng)烙样。所以下一步要做的是下載相同版本的 nginx 源碼,使用相同的編譯選項(xiàng)并添加 nginx-rtmp-module蕊肥,替換原來(lái)的 nginx 谒获。
- 下載 nginx 1.10.3 的源碼和 nginx-rtmp-module 的源碼。
wget https://nginx.org/download/nginx-1.10.3.tar.gz
tar zxvf nginx-1.10.3.tar.gz
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
cp -r nginx-rtmp-module nginx-1.10.3
- 在第 3 步中可以得知安裝的 nginx 的編譯選項(xiàng)壁却,所以套用這些編譯選項(xiàng)批狱,在上一步已經(jīng)把 nginx-rtmp-module 復(fù)制到 nginx 源碼目錄,所以在結(jié)尾添加 --add-module=./nginx-rtmp-module 展东。在 nginx-1.10.3 目錄執(zhí)行以下命令:
./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --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 --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-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --add-module=./nginx-rtmp-module
- 上一步執(zhí)行后可能會(huì)提示以下幾個(gè)錯(cuò)誤赔硫,需安裝相關(guān)軟件包,然后再次執(zhí)行步驟 5 的命令盐肃。
./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: SSL modules require the OpenSSL library.
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
./configure: error: the HTTP image filter module requires the GD library.
./configure: error: the GeoIP module requires the GeoIP library.
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev
sudo apt-get install libxml2 libxml2-dev libxslt-dev
sudo apt-get install libgd2-xpm-dev
sudo apt-get install libgeoip-dev
- 上一步執(zhí)行完成后 make爪膊,等待 nginx 編譯完成权悟。編譯過(guò)程可能會(huì)出現(xiàn) error: macro "DATE" might prevent reproducible builds 錯(cuò)誤,在 CFLAGS 中添加 -Wno-error=date-time 參數(shù)即可推盛,也就是步驟5的命令改成
./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -Wno-error=date-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --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 --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-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --add-module=./nginx-rtmp-module
- 編譯完成后峦阁,在 objs 目錄下會(huì)有 nginx 的可執(zhí)行文件。首先停止 nginx 服務(wù)耘成,替換掉 nginx 榔昔。
sudo service nginx stop
cd /usr/sbin
sudo mv nginx nginx.bak
sudo cp ~/nginx-1.10.3/objs/nginx ./
- 修改 /etc/nginx/nginx.conf,在結(jié)尾添加使其開(kāi)啟 nginx-rtmp-module 相關(guān)的功能瘪菌。
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
}
}
}
- 執(zhí)行 sudo service nginx restart 重啟 nginx 服務(wù)撒会,然后執(zhí)行 netstat -a|grep 1935,可以看到 1935 端口處于LISTEN狀態(tài)控嗜,即可向 nginx 推流茧彤。更多強(qiáng)大的功能可以查看
https://github.com/sergey-dryabzhinsky/nginx-rtmp-module