nginx系列基础(2)---nginx安装
nginx安装的方式
- 通过源码的方式(推荐使用)
- 使用操作系统自带的安装工具yum、apt等
使用系统自带工具安装存在一个问题。
nginx的二进制文件会把模块直接编译进来的,而nginx的官方模块并不是默认都是开启的,如果使用其它的第三方模块就必须使用编译的方式,才能添加第三方模块的功能。
编译nginx
下载nginx
nginx下载地址:http://nginx.org/en/download.html
自行下载
wget http://nginx.org/download/nginx-1.20.2.tar.gz
# 解压到安装位置
tar xf nginx-1.20.2.tar.gz -C /usr/local/src/
介绍各个目录
# 进入解压后的目录
cd /usr/local/src/nginx-1.20.2/
# 具体目录
drwxr-xr-x. 6 1001 1001 4096 11月 27 13:21 auto
-rw-r--r--. 1 1001 1001 312251 11月 16 22:44 CHANGES
-rw-r--r--. 1 1001 1001 476577 11月 16 22:44 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 11月 27 13:21 conf
-rwxr-xr-x. 1 1001 1001 2590 11月 16 22:44 configure
drwxr-xr-x. 4 1001 1001 72 11月 27 13:21 contrib
drwxr-xr-x. 2 1001 1001 40 11月 27 13:21 html
-rw-r--r--. 1 1001 1001 1397 11月 16 22:44 LICENSE
drwxr-xr-x. 2 1001 1001 21 11月 27 13:21 man
-rw-r--r--. 1 1001 1001 49 11月 16 22:44 README
drwxr-xr-x. 9 1001 1001 91 11月 27 13:21 src
- auto目录
drwxr-xr-x. 2 1001 1001 133 11月 27 13:21 cc
-rw-r--r--. 1 1001 1001 141 11月 16 22:44 define
-rw-r--r--. 1 1001 1001 889 11月 16 22:44 endianness
-rw-r--r--. 1 1001 1001 2812 11月 16 22:44 feature
-rw-r--r--. 1 1001 1001 136 11月 16 22:44 have
-rw-r--r--. 1 1001 1001 137 11月 16 22:44 have_headers
-rw-r--r--. 1 1001 1001 411 11月 16 22:44 headers
-rw-r--r--. 1 1001 1001 1020 11月 16 22:44 include
-rw-r--r--. 1 1001 1001 768 11月 16 22:44 init
-rw-r--r--. 1 1001 1001 4875 11月 16 22:44 install
drwxr-xr-x. 11 1001 1001 163 11月 27 13:21 lib
-rw-r--r--. 1 1001 1001 18324 11月 16 22:44 make
-rw-r--r--. 1 1001 1001 3934 11月 16 22:44 module
-rw-r--r--. 1 1001 1001 38640 11月 16 22:44 modules
-rw-r--r--. 1 1001 1001 136 11月 16 22:44 nohave
-rw-r--r--. 1 1001 1001 25469 11月 16 22:44 options
drwxr-xr-x. 2 1001 1001 88 11月 27 13:21 os
-rw-r--r--. 1 1001 1001 8752 11月 16 22:44 sources
-rw-r--r--. 1 1001 1001 120 11月 16 22:44 stubs
-rw-r--r--. 1 1001 1001 2014 11月 16 22:44 summary
-rw-r--r--. 1 1001 1001 394 11月 16 22:44 threads
drwxr-xr-x. 2 1001 1001 65 11月 27 13:21 types
-rw-r--r--. 1 1001 1001 26166 11月 16 22:44 unix
# cc 编译目录
# lib 库文件
# os 对操作系统判断
其它文件都是在Configure用来判断nginx支持的模块
auto就是nginx的Configure这步提供支持的
- CHANGES文件 CHANGES.ru文件
就是nginx当前版本做了那些修改,如更新那些功能,修改了那些bug之类的更新说明
CHANGES.ru 因为作者是俄罗斯人,这个是俄罗斯语言的更新说明
- conf目录
实例nginx配置文件目录,当我们安装后这个目录也会拷贝到,我们的安装目录中作为nginx默认启动配置文件
- configure脚本
是一个生成中间文件,执行编译前的必备动作的一个脚本文件
- contrib目录
提供了两个pl脚本,和vim的工具。
就是我们没有使用这个vim工具的时候打开nginx配置文件的时候色彩是没有变化的。使用了这个vim工具nginx配置文件中关键字的色彩会发生变化。
# 没有,创建一个.vim的目录
mkdir ~/.vim/
# 拷贝vim目录下的文件,到当前用户下的vim目录下
cp -a contrib/vim/* ~/.vim/
- html文件目录
这个目录提供了提供了两个标准的HTML文件,一个是标准的500错误文件,一个是默认的index文件
- man目录
提供了nginx的帮助文件
man ./man/nginx.8
- src目录
nginx的源代码目录
Configure
查看configure文件支持那些参数
./configure --help | more
可以分为以下几块内容
- 指定安装目录、生成文件的参数
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
--user=USER set non-privileged user for
worker processes
--group=GROUP set non-privileged group for
worker processes
--build=NAME set build name
--builddir=DIR set build directory
- 使用那些模块或关闭那些模块
通常是使用--with后--without开头的参数
--with:表示开启那些模块
--without:表示关闭那些模块
- 其它参数
--with-cc=PATH set C compiler pathname
--with-cpp=PATH set C preprocessor pathname
--with-cc-opt=OPTIONS set additional C compiler options
--with-ld-opt=OPTIONS set additional linker options
--with-cpu-opt=CPU build for the specified CPU, valid val
ues:
pentium, pentiumpro, pentium3, pentium
4,
athlon, opteron, sparc32, sparc64, ppc
64
--without-pcre disable PCRE library usage
--with-pcre force PCRE library usage
--with-pcre=DIR set path to PCRE library sources
--with-pcre-opt=OPTIONS set additional build options for PCRE
--with-pcre-jit build PCRE with JIT compilation suppor
t
--with-zlib=DIR set path to zlib library sources
--with-zlib-opt=OPTIONS set additional build options for zlib
--with-zlib-asm=CPU use zlib assembler sources optimized
for the specified CPU, valid values:
pentium, pentiumpro
--with-libatomic force libatomic_ops library usage
--with-libatomic=DIR set path to libatomic_ops library sour
ces
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenS
SL
--with-debug enable debug logging
# 这些就是当我在是gcc编译的时候可能需要添加以下gcc相关的参数,还有就是使用第三方模块是要指定位置
- 使用默认参数编译nginx
# 先安装必要的依赖,会在后面的专栏中逐步补充
yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
# gcc gcc-c++ make nginx底层使用c++编译需要使用gcc的环境
# pcre 一个Perl库,包括perl兼容的正则表达式,nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre, pcre-devel是使用pcre开发的一个二次开发库,nginx也需要此库
# lib库提供了多种压缩和解压缩方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库
# openssl是一个强大的安全套接字层密码库,囊括了主要的密码算法、常用的秘钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
# nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux上安装openssl库
# 我们这里只指定了nginx的默认安装目录位置/usr/local/nginx
./configure --prefix=/usr/local/nginx
中间文件介绍
configure执行完成以后会生产一些中间文件
会生成一个
ngx_modules.c
的文件,这个文件决定了,我们编译的时候会有那些模块安装到nginx中去。每个extern
都是我们安装的模块,最后统一放到了*ngx_modules[]
的数组中去了。
编译
# 编译
make -j 2
# -j 2表示使用2核CPU进行编译,指定核心数
编译完成后会在objs文件夹中生成nginx的二进制文件,这个我们是需要知道的,当我们做平滑升级的时候是需要这个二进制文件的。
C语言编译过程中使用的中间文件都在objs/src目录下。
如果我们使用了动态模块,那动态模块会生成.SO
的文件,也会放到objs目录下
安装
make install
- 查看安装目录
cd /usr/local/nginx/
[root@cng-web1 nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 11月 27 14:34 conf
drwxr-xr-x. 2 root root 40 11月 27 14:34 html
drwxr-xr-x. 2 root root 6 11月 27 14:34 logs
drwxr-xr-x. 2 root root 19 11月 27 14:34 sbin