编译Nginx主要分为以下六个部分:

极客时间Mysql资源 下载极客时间_极客时间Mysql资源

1. 下载Nginx:

从Nginx官方网站中下载压缩包即可。

也可使用 wget 命令的方式进行下载:

Unix > wget http://nginx.org/download/nginx-1.14.0.tar.gz

wget 命令:

wget是Linux中的一个下载文件的工具,支持通过HTTP、HTTPS、FTP三个最常见的 TCP/IP协议下载。




2. 介绍各目录:

解压下载的Nginx压缩包后,在Nginx的目录中可以看到主要包含以下几个部分:

root nginx > ls -l

auto			# auto文件夹中主要有4个目录:cc、lib、os、types,auto目录主要是为了辅助configure执行时Nginx要支持哪些模块、当前系统支持哪些特性来供Nginx使用;

GHNAGE			# 当前版本相较于前面版本有哪些变化,新增支持哪些特性(Feature)和修复了哪些bug(Bugfix)
CHANGE.ru		# 俄语版的CHANGE文件

conf			# 实例文件。在Nginx安装好后,会把configure的实例文件拷贝到conf文件中,方便运维使用

configure		# 配置脚本,用来生成中间文件,执行编译前的必备动作

contrib			# Perl脚本,使Nginx的语法在vim中显示

html			# 包括 50x.html, index.html 两个html文件

LICENSE

man				# Nginx的帮助文件

README

src				# Nginx的源代码(core, event, http, mail, misc, os, stream)

tree 命令:

Linux tree 命令用于以树状图列出目录的内容。
执行tree指令,它会列出指定目录下的所有文件,包括子目录里的文件。




3. Configure:

按照Unix的惯例,需要先执行 configure 文件,然后再编译。

configure 命令介绍:

./configure --help | more

//第一类参数:指定Nginx的安装路径:
--prefix=PATH			# set installation prefix
--sbin-path=PATH
--modules-path=PATH
--conf-path=PATH

//第二类参数:确定Nginx要使用哪些模块、不使用哪些模块:
//"--with" 开头的模块是默认 不 编译进Nginx的模块,使用with可指定其加入Nginx
//"--without" 开头的模块是默认 编译进Nginx的模块,使用without可指定其不加入Nginx
--with-select_module
--with-http_ssl_module

--without-http_gzip_module
--without-http_charset_module

//第三类参数:一些特殊选项:
--with-cc=PATH				# 指定C编译器路径
--with-cpp=PATH				# 指定CPP编译器路径
--with-ld-opt=OPTIONS



使用默认方式安装:

./configure --prefix=/home/nginx



more 命令:

Linux more 命令类似 cat,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空格键(space)就往下一页显示,按 b 键 就会往回(back)一页显示,而且还有搜索字符串的功能(与 vi 类似)。




4. 中间文件介绍:

objs目录:
./configure 执行后会生成一些中间文件,这些中间文件会放在 objs 目录中(与 auto、configure 等同级的目录)。
objs 目录中最重要的文件是 ngx_module.c,它决定了在后面编译时有哪些模块会被编译进Nginx。
其中是一个数组,包含了所有要编译进Nginx的模块。

ngx_module.c 内容:

#include <ngx_config.h>
#include <ngx_core.h>

extern  ngx_module_t  ngx_core_module;
extern  ngx_module_t  ngx_errorlog_module;
...


ngx_module_t *ngx_modules[] = {
	&ngx_core_module,
	&ngx_errorlog_module,
	...
};

char *ngx_module_names[] = {
	"ngx_core_module",
	"ngx_errlog_module",
	...
};




5. 编译:

执行 make 编译。



编译后,会生成大量的中间文件(例如 .o 文件) 和 最后的可执行二进制文件(nginx)。

objs 目录中可以看到 可执行二进制文件 nginx;
objs/src 目录下可以看到C语言编译时生成的所有中间文件(.o 文件)。




6. 安装:

执行 make install 安装。



(首次安装时可以通过执行 make install 安装)

随后,在 --prefix 指定的目录中就可以看到 安装好后的 Nginx。

其中,
最主要的nginx可执行二进制文件就在 sbin 目录下;
决定 nginx功能的配置文件 在 conf 目录下;
access.log 和 error.log 将会保存在 logs 目录下。

root / > cd /home/nginx/

root nginx > ls 
conf  html  logs  sbin

root nginx > tree sbin
sbin/
|—— nginx

root nginx > tree html
html/
|—— 50x.html
|—— index.html

root nginx > tree conf
conf/
|—— fastcgi.conf
|—— mime.types
|—— nginx.conf
...