前言
因为工作需要,需要对Nginx进行源代码级的研究,研究第一步就是编译其代码;
编译流程
编译准备
- nginx 源码包下载:http://nginx.org/en/download.html
- PCRE 下载:http://www.pcre.org (rewrite 模块依赖)
- zlib 库下载:http://zlib.net (gzip 模块依赖)
- openssl库下载 :https://www.openssl.org/source/
下载好源代码后解压到对应的目录,例如:document/nginx;
nginx从github下载的源代码不能直接编译,需要从上述网站下载,有时间研究下github的源代码,看看看是不是经过了二次打包的工作;
安装编译环境
brew install pcre
brew install zlib
brew install openssl
编译方法一
在nginx目录新建build.sh文件
全源代码编译;
#!/bin/bash
# 显示执行该指令及所下的参数。
set -x
# clean 整个工程
make clean
OUTPUT=$(pwd)/bin
# 编译函数
function build
{
# 编译前清理数据
make clean
./configure \
--prefix=$OUTPUT/ \
--sbin-path=$OUTPUT/nginx \
--conf-path=$OUTPUT/nginx.conf \
--pid-path=$OUTPUT/nginx.pid \
--with-http_ssl_module \
--with-pcre=pcre-8.38 \
--with-zlib=zlib-1.2.8 \
--with-openssl=openssl-1.1.1c
# 编译。
make -j8
#安装
make install
echo -e "\033[32m build successful \033[0m"
}
#执行编译命令
build
编译方法二
#!/bin/bash
# 显示执行该指令及所下的参数。
set -x
# clean 整个工程
make clean
OUTPUT=$(pwd)/bin
# 编译函数
function build
{
# 编译前清理数据
make clean
./configure \
--prefix=$OUTPUT/ \
--sbin-path=$OUTPUT/nginx \
--conf-path=$OUTPUT/nginx.conf \
--pid-path=$OUTPUT/nginx.pid \
--with-http_ssl_module \
--with-cc-opt="-I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/Cellar/pcre/8.43/include -I/usr/local/opt/zlib/include" \
--with-ld-opt="-L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/Cellar/pcre/8.43/lib -L/usr/local/opt/zlib/lib"
# 编译。
make -j8
#安装
make install
echo -e "\033[32m build successful \033[0m"
}
#执行编译命令
build
编译结果
./build #执行编译命令
#编译成功后去到bin生成的目录,可以看到对应的生成文件
./bin/nginx
*编译结果
*生成nginx执行文件
*运行结果
编译参数
--prefix #nginx安装目录,默认在/usr/local/nginx
--pid-path #pid问件位置,默认在logs目录
--lock-path #lock问件位置,默认在logs目录
--with-http_ssl_module #开启HTTP SSL模块,以支持HTTPS请求。
--with-http_dav_module #开启WebDAV扩展动作模块,可为文件和目录指定权限
--with-http_flv_module #支持对FLV文件的拖动播放
--with-http_realip_module #支持显示真实来源IP地址
--with-http_gzip_static_module #预压缩文件传前检查,防止文件被重复压缩
--with-http_stub_status_module #取得一些nginx的运行状态
--with-mail #允许POP3/IMAP4/SMTP代理模块
--with-mail_ssl_module #允许POP3/IMAP/SMTP可以使用SSL/TLS
--with-pcre=../pcre-8.11 #注意是未安装的pcre路径
--with-zlib=../zlib-1.2.5 #注意是未安装的zlib路径
--with-debug #允许调试日志
--http-client-body-temp-path #客户端请求临时文件路径
--http-proxy-temp-path #设置http proxy临时文件路径
--http-fastcgi-temp-path #设置http fastcgi临时文件路径
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #设置uwsgi 临时文件路径
--http-scgi-temp-path=/var/tmp/nginx/scgi #设置scgi 临时文件路径
详细配置参数:http://nginx.org/en/docs/configure.html
参考文章
http://nginx.org/en/docs/install.html
https://www.nginx.com/resources/wiki/start/topics/tutorials/installoptions/
https://www.vultr.com/docs/how-to-compile-nginx-from-source-on-ubuntu-16-04
总结&思考
- 编译失败,整个过程中因为依赖组件版本不一致导致链接失败,可通过查找相关资料解决,切记要对失败原因分析透彻才能解决问题,不要过早放弃。
- 多咨询有经验的同学,可快速解决问题。
- 多做失败记录,积累经验。