🗂 | 查看 NGINX 专题可浏览更多内容
应该选择预先构建的包还是源代码编译来安装 NGINX?
预先构建的包:开箱即用。
使用如 APT 一类的高级包管理系统有很多好处,包的兼容性测试、安装所需要的依赖、安装后的如权限等配置、日后的升级维护都一条龙的解决了。
源代码编译:提供更多的灵活性。
而从源代码编译 NGINX 可以添加特定的模块 (来自 NGINX 或第三方) ,并应用最新的安全补丁。
添加特定的模块是使用编译安装 NGINX 的主要原因,大多数构建在 NGINX 中的模块是静态链接的:它们在编译时构建在 NGINX 中并静态链接到 NGINX 二进制文件。这些模块需要通过编译 NGINX 来启用或禁用。
NGINX 模块也可以编译为共享对象 (*.so
文件) 。然后在运行时动态加载到 NGINX。这提供了更多的灵活性,因为通过在 NGINX 配置文件中添加或删除相关的load_module
指令并重新加载配置,可以在任何时候加载或卸载模块。但注意,这需要模块本身必须支持动态链接。
相关文章:
以 NGINX 1.17.10 为例进行编译安装:
# 进入通常用于存放编译源码的目录
$ cd /usr/local/src
# 下载 NGINX
$ sudo wget https://nginx.org/download/nginx-1.17.10.tar.gz
# 解压
$ sudo tar -xaf nginx-1.17.10.tar.gz
$ cd nginx-1.17.10/
/usr/local/src/nginx-1.17.10/
├── auto # 用于编译
├── CHANGES # 修改日志
├── CHANGES.ru # 俄语版修改日志
├── conf # 示例文件,会拷贝到安装目录
├── configure # 本,用于生成中间文件,执行编译前必备动作
├── contrib # 提供两个 Perl 脚本 geo2nginx.pl、unicode2nginx 和 vim 高亮配置
├── html # 提供标准 HTML 文件 (50x 错误和 index)
├── LICENSE # 版权协议
├── man # 帮助文件
├── README # 说明
└── src # 源码文件
💡 如果使用 VIM 编辑器可以使用
contrib
目录下的配置文件,以在打开 NGINX 配置文件时显示语法高亮:cp -r contrib/vim/* ~/.vim/
问题
在编译前可以先看看支持哪些选项
$ ./configure --help | more
--help print this message
--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-select_module enable select module
--without-select_module disable select module
--with-poll_module enable poll module
--without-poll_module disable poll module
--with-threads enable thread pool support
--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
如一般常用的 --prefix=PATH
指定安装目录,如 --user=USER
及 --group=GROUP
用于指定工作进程使用某个非特权用户和用户组。
第二部分为 --with
和 --without
开头,带有 --with
的模块默认不会编译进 NGINX(--without
相反),所以当需要的时候就加上该参数。
$ sudo ./configure
编译时可能会遇到一些报错,常见的有
问题 1:
./configure: error: C compiler cc is not found
这是因为缺少编译相关的工具,如使用 APT 解决:
$ sudo apt install build-essential
有些人会在安装
build-essential
后仍会看到C compiler cc is not found
的提示,这是缺少权限所致,使用sudo ./configure
或使用拥有足够权限的帐户再次运行即可。
问题 2:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
NGINX 编译安装 NGINX 需要 PCRE、zlib 和 OpenSSL 三个依赖库,如上示例就是缺少 PCRE。
构建
现在开始正式安装 NGINX。
依赖
NGINX 编译安装 NGINX 需要 PCRE、zlib 和 OpenSSL 三个依赖库,可以使用包管理系统或编译安装,个人的建议是能不编译就不要编译。
如使用 APT 安装 PCRE、zlib 和 OpenSSL:
$ sudo apt install libpcre3-dev zlib1g-dev libssl-dev
如果你的项目要求使用编译安装,可使用下列方法,但注意不要照搬所给出的示例,而是按照注释中的官方网站获取最新版本的下载链接:
# 回到本地源码目录
$ cd /usr/local/src
# 下载 PCRE https://pcre.org/
$ sudo wget https://versaweb.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz
# 解压
$ sudo tar -xaf pcre-8.45.tar.gz
# 下载 zlib https://zlib.net/
$ sudo wget https://zlib.net/zlib-1.2.12.tar.gz
# 解压
$ sudo tar -xaf zlib-1.2.12.tar.gz
# 下载 OpenSSL https://www.openssl.org/source/
$ sudo wget https://www.openssl.org/source/openssl-1.1.1n.tar.gz
# 解压
$ sudo tar -xaf openssl-1.1.1n.tar.gz
# 返回到 NGINX 安装目录
$ cd /usr/local/src/nginx-1.17.10
配置
为 NGINX 的工作进程新建一个非特权用户,如 www
:
$ sudo useradd -s /sbin/nologin www
如果是使用 APT 安装 PCRE、zlib 和 OpenSSL,则使用以下命令,下列多行命令为一条命令:
$ sudo ./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_ssl_module
如果是想要通过编译安装 PCRE、zlib 和 OpenSSL,已经通过前文步骤下载并解压了相关源码包,那么需要加上相关源码包的解压路径:
$ sudo ./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-pcre=/usr/local/src/pcre-8.45 \
--with-zlib=/usr/local/src/zlib-1.2.12 \
--with-openssl=/usr/local/src/openssl-1.1.1n \
--with-http_ssl_module
通过返回内容可以看到:
Configuration summary
+ using PCRE library: /usr/local/src/pcre-8.45
+ using OpenSSL library: /usr/local/src/openssl-1.1.1n
+ using zlib library: /usr/local/src/zlib-1.2.12
所需要的 3 个 library 都也配置上了
此时 nginx-1.17.10
目录里会新建一个名为 objs
的目录,里面存放着一些中间文件
/usr/local/src/nginx-1.17.10/objs/
├── autoconf.err
├── Makefile
├── ngx_auto_config.h
├── ngx_auto_headers.h
├── ngx_modules.c
└── src
其中最重要的是 ngx_modules.c
,它决定了稍后编译安装时有哪些模块会被编译进 NGINX。
编译
接下来开始编译
$ sudo make
如果看到 Command 'make' not found, but can be installed with
提示,如前文所述那样安装 build-essential
即可。
在成功编译后会生成更多的中间文件:
/usr/local/src/nginx-1.17.10/objs/
├── autoconf.err
├── Makefile
├── nginx
├── nginx.8
├── ngx_auto_config.h
├── ngx_auto_headers.h
├── ngx_modules.c
├── ngx_modules.o
└── src
其中 nginx
就是 NGINX 的二进制文件,如果有动态模块也会生成在 objs
目录中,而 gcc 编译的文件会在 src
目录中。
安装
首次安装使用
$ sudo make install
如果在配置时没有使用 --prefix
指定目录一般会默认安装到 /usr/local/nginx/
,而 NGINX binary 会在该目录下的 sbin
目录中 (/usr/local/nginx/sbin
)
/usr/local/nginx/
├── conf # 决定功能的配置文件
├── html
├── logs
└── sbin # 最主要的二进制文件
为什么强调首次安装时使用 make install
,因为日后进行 NGINX 升级时不能使用该命令,而是在编译后替换二进制文件,也就是将 /usr/local/src/nginx-1.17.10/objs/
下的 nginx
拷贝替换到安装目录。
最后,查看 NGINX 版本号以确认是否成功安装。
$ /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.17.10
成功。
其他
1.运行 nginx
提示 nginx: command not found
$ sudo ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx
未配置 --prefix
时默认安装到了 /usr/local/nginx
,如果你指定了安装目录则此处需要修改。如果你接手的系统并不知道装在了哪里可以使用命令如查找 /usr
下:find /usr -type f -name "nginx"
2.配置 NGINX 的 systemd 配置
$ sudo vim /lib/systemd/system/nginx.service
文件内容为:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
设置 NGINX 开机启动
$ sudo systemctl enable nginx