ubuntu18.01 nginx配置播放直播之编译模块

你也可以直接
sudo apt-get install nginx-extras
安装后直接可以支持mp4

以下为编译方式

我的实验环境: win10子系统ubuntu18.04 安装的是nginx 1.14.0
这里跟php没有关系,nginx默认没有那些功能,但可以编译配置。
单纯就是nginx实现rtmp,mp4,flv,hls播放
用rtmp协议需要用到第三方
ngx_http_rtmp_module
要IP连接数量控制需要官方自带模块
ngx_http_limit_conn_module
用http协议需要官方自带模块
ngx_http_mp4_module
ngx_http_flv_module
ngx_http_hls_module

每个模块可以参考官方
nginx.org/en/docs/
或者中文文档
https://www.docs4dev.com/docs/zh/nginx/current/reference/dirindex.html

只编译不安装(也就是只make,但是不要make install)
跟已安装nginx版本要一致,我这ubuntu18.04默认安装的是nginx 1.14.0
编辑configure命令时的参数也要和原来的一样,后面再增加需要的新增模块

下载nginx源码
# 把源码丢到/usr/local/src好找
cd /usr/local/src
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
下载rtmp-module源码
cd /usr/local/src
wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
tar -zxvf v1.2.1.tar.gz

可以通过
nginx -V(大写V)
获取版本号和已配置的参数,复制下来找个txt存下备用。
类似这样的,出来一大堆英文

–with-cc-opt=’-g -O2 -fdebug-prefix-map=/build/nginx-DUghaW/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2′ –with-ld-opt=’-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC’ –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 –modules-path=/usr/lib/nginx/modules –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-http_ssl_module –with-http_stub_status_module –with-http_realip_module –with-http_auth_request_module –with-http_v2_module –with-http_dav_module –with-http_slice_module –with-threads –with-http_addition_module –with-http_geoip_module=dynamic –with-http_gunzip_module –with-http_gzip_static_module –with-http_image_filter_module=dynamic –with-http_sub_module –with-http_xslt_module=dynamic –with-stream=dynamic –with-stream_ssl_module –with-mail=dynamic –with-mail_ssl_module

删除线部分是不需要的,从–prefix=到结尾的部分复制起来,在后面编译时要用。

新配置格式(小提示,\ 这符号表示换行,为了好看)
./configure \
原有编译配置项目替换这里 \

--with-http_mp4_module \
--with-http_flv_module \
--with-http_hls_module \
--with-http_limit_conn_module \
--add-dynamic-module=/usr/local/src/nginx-rtmp-module-1.2.1

我的是这样的,

./configure \
--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 --modules-path=/usr/lib/nginx/modules --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-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module \
--with-http_mp4_module \
--with-http_flv_module \
--with-http_hls_module \
--with-http_limit_conn_module \
--add-dynamic-module=/usr/local/src/nginx-rtmp-module-1.2.1

注意上面扩展版本号nginx-rtmp-module-1.2.1根据源码官方最新为准。
注意填写绝对路径/usr/local/src/nginx-rtmp-module-1.2.1

在使用上面配置命令前,请先确认下编译环境是不是齐全了
编译环境必备acc是肯定需要的
# 查看zlib是否安装
dpkg -l | grep zlib
sudo apt-get install openssl libssl-dev 已经有
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install libxml2 libxml2-dev 已经有
sudo apt-get install libxslt-dev
# GD gd安装需要需要一点时间
sudo apt-get install libgd-dev
sudo apt-get install zlib1g zlib1g-dev 已经有
apt-get install libgeoip-dev

进入nginx源码目录
cd /usr/local/src/nginx-1.14.0
如果只有add-module和add-dynamic-module那么使用如下命令编译
make -f objs/Makefile modules
如果需要--with或--without 新增或删除原有模块,那么就要全编译
make
这里我们不需要 make install.

编译完成后nginx二进制和so文件会出现在objs文件夹中
替换nginx方法
查看nginx在哪个sbin目录里
vim /lib/systemd/system/nginx.service
文档显示,我的是在这里,你们有可能在/usr/local/nginx/sbin
ExecStartPre=/usr/sbin/nginx
备份旧的
service nginx stop
cp /usr/sbin/nginx /usr/sbin/nginx.bak
替换新的
cp ./objs/nginx /usr/sbin/nginx

新增模块的配置
# 将ngx_rtmp_module.so拷到/usr/lib/nginx/modules/模块总目录
cp ./objs/ngx_rtmp_module.so /usr/lib/nginx/modules/ngx_rtmp_module.so
# 新增nginx模块加载文档mod-rtmp.conf
echo "load_module modules/ngx_rtmp_module.so;" >> /usr/share/nginx/modules-available/mod-rtmp.conf
# unix软链接
ln -s /usr/share/nginx/modules-available/mod-rtmp.conf /etc/nginx/modules-enabled/mod-rtmp.conf

编译完了nginx也拷到相应目录了,模拟也配好了。重启
service nginx restart

下一章节讲到播放如何配置nginx

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注