之前在使用 nginx 和 nginx-rtmp-module 搭建流媒体服务器的时候遇到一个很尴尬的问题, 就是在把 nginx-rtmp-module 模块添加到 nginx 中去的时候, 我最开始采取的做法是先卸载原来的 nginx, 再下载 nginx 和 nginx-rtmp-module 的源码重新编译并安装. 重装完之后我测试了一下流媒体服务器是正常的, 但是接下来问题来了, 由于我有一些 web 工程是部署在之前搭建好的 LUMP 环境下的, 现在重装了 nginx, 这些工程需要重新部署. 于是, 我又开始重新部署这些 WEB 工程, 但是结果让我哭晕在了厕所, 因为 nginx 的目录结构发生了很大改变, 导致我没办法配置原来的 WEB 工程了. 此后我陷入了很长时间的挣扎和徘徊, 网上搜寻了很多解决方案, 但是这些方案大多都是侧重讲如何编译安装 nginx 以及进行推拉流测试, 没有涉及到我这方面的问题. 后来, 我在浏览帖子的时候看到有人说把自己编译的 nginx 可执行文件替换掉原来的 nginx 共享库能解决问题, 于是立马试了一把, 结果发现真的可以! 现在的 nginx 能同时运行流媒体服务和部署 WEB 工程, 鱼与熊掌兼得!
下面简单介绍一下我的操作过程, 希望能为遇到同样问题的你带来一些帮助 (我的配置环境: Ubuntu Server 16.04 + nginx1.10.0 + nginx-rtmp-module-master).
1. 先采用 apt-get 的方式安装 nginx. 目前用此方式安装的 nginx 的版本号为 1.10.0
- 1sudo apt-get update
- 2sudo apt-get install nginx
2. 到你喜欢的目录下用你喜欢的名字创建一个目录, 用于存放 nginx 和 nginx-rtmp-module 的源码, 例如: 我在根目录 / softwares(softwares 也是我自己创建的) 下面创建了 nginx 目录, 接下来我会把 nginx 和 nginx-rtmp-module 下载到 nginx 目录下.
- 1cd softwares/2sudo mkdir nginx
3. 进入 nginx 目录.
- 1cd nginx/
4. 下载 nginx 源码, 注意: 下载的源码版本需要与步骤 1 安装的 nginx 的版本保持一致, 以避免不必要的问题. 获取 nginx 源码的途径很多, 此处推荐两种方式.
方式 a: 在终端执行 apt-get source nginx 命令, 直接获取到对应版本的源码.
- 1sudo apt-get source nginx
此方式下载完之后会自动解压, nginx-1.10.0 目录即 nginx 源码目录.
方式 b: 在 nginx 的官网上面找到对应版本然后下载.
- 1 sudo wget http: //nginx.org/download/nginx-1.10.0.tar.gz
此方式下载完之后需要自己手动解压. 解码命令:
- 1sudo tar zxvf nginx-1.10.0.tar.gz
5. 下载 nginx-rtmp-module 的源码.
- 1 sudo wget https: //github.com/arut/nginx-rtmp-module/archive/master.zip
因为 nginx-rtmp-module 已在 GitHub 上开源, 所以也可以直接从 GitHub 上获取. GitHub 地址: https://github.com/arut/nginx-rtmp-module.
6. 解压 nginx-rtmp-module 源码的压缩包.
- 1sudo unzip master.zip
7. 进入 nginx 源码目录.
- 1cd nginx-1.10.0/
8. 查看当前 nginx 的配置信息, 并将当前配置信息完整保存到一个地方, 后面在编译 nginx 源码的时候需要依据当前的配置信息来进行配置.
- 1nginx -V
注意命令行中的 V 是大写哦, 小写只能看到 nginx 的版本号. 我当前的 nginx 配置信息如下:
- --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
9. 配置 nginx 源码编译信息, 将 nginx-rtmp-module 添加到 nginx 中.
- 1sudo ./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 --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_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-master
注意, 这一行命令其实是这样组成的: sudo ./configure <原来的配置信息> --add-module=../nginx-rtmp-module-master. 这样就将 nginx-rtmp-module 添加到 nginx 配置里面了, 同时将之前保存的 nginx 配置信息添加到了用于此次编译的配置信息里面, 尽可能地保证了编译出来的 nginx 与原来的 nginx 功能一致. 细心的同学会发现, 其实我在配置 nginx 信息的时候并没有将原来所有的配置信息都写进去, 原因是如果全部复制粘贴过去, 在编译的时候会出现一些不太好处理的错误, 这些错误一时半会儿还没找到好的解决方案, 所以就缩减了一些配置信息, 缩减之后在功能模块上并无太大差异, 所以可放心使用.
10. 配置完成之后, 执行 make 命令开始编译 nginx 源码. 编译完成之后, 会在 nginx 源码目录的 objs 目录下生成 nginx 可执行文件.
- 1sudo make
11. 将生成的 nginx 可执行文件拷贝到 / usr/sbin 目录下, 替换原来的 nginx 共享库文件. 注意: 原来 / usr/sbin 目录有一个 nginx 共享库文件, 我们使用编译出来的 nginx 可执行文件对其进行替换.
- 1sudo nginx /usr/sbin
12. 重启 nginx.
- 1sudo service nginx restart
13. 再次查看 nginx 配置信息.
- 1nginx -V
可以看到 nginx-rtmp-module 模块已经添加到了 nginx 里面.
- 1--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_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-master
试了试推流拉流, 功能正常, 再运行了一下原来的 WEB 工程, 也正常!
最后, 我说明一下为什么要先通过 apt-get 的方式安装 nginx, 再进行编译替换. 原因是为了方便在 LUMP 环境下部署 WEB 工程. 如果不先通过 apt-get 的方式安装 nginx, 而是直接下载源码编译安装, 则 nginx 的配置目录会不齐全, 很难去部署 WEB 项目 (也许通过某些操作也能实现部署, 但是具体怎么操作还是要花时间去研究 nginx 的). 如果不搭建流媒体服务, 我建议都通过 apt-get 的方式安装 nginx, 步骤简单又省心!
来源: http://www.cnblogs.com/1992monkey/p/7089830.html