官网:http://nginx.org/,官方安装参考文档。
GCC官网:https://gcc.gnu.org/。编译时,可以参考 GCC 在线文档。
版本选择
Nginx 有两种开源版本:Mainline 和 Stable,生产环境一般都选择稳定版(Stable)。
安装方式选择
Nginx 官方提供了两种安装方式:二进制包(rpm)安装和源码(source)编译安装,生产环境一般都选择源码编译安装。
二进制包安装
二进制包安装比较简单,配置好 YUM 仓库后,直接使用 yum 命令安装即可。
PS:
可以选择 Nginx 官方的 repo,epel.repo 中也包含了 nginx 包。推荐选择 Nginx 官方的 repo,版本多。
Nginx 官方 repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
epel.repo
#可以选择阿里云的epel.repo,官方的repo比较慢
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum 安装 Nginx
#查看yum仓库中nginx的所有版本
yum -v list nginx --show-duplicates
#安装1.9.15版本
yum -y install nginx-1.9.15
#查看安装nginx所生成的文件路径
rpm -ql nginx-1.9.15
源码编译安装
首先安装 gcc
yum -y install gcc gcc-c++
创建运行用户和组
groupadd -g 800 nginx
useradd -u 800 -g 800 -s /sbin/nologin nginx
安装依赖项的库
PCRE 库
官网:http://pcre.org/,下载源码包进行编译安装。
PCRE 是一个 perl 库,包括 perl 兼容的正则表达式库。供 Nginx 的 core 和 rewrite 模块使用。PCRE 从 8.20 版本开始,提供了JIT。可使用--enable-jit参数构建。
当使用 --with-pcre=DIR 构建 PCRE 库时,可通过 --with-pcre-jit 配置参数启用 JIT 支持。PCRE JIT 可以显着加快正则表达式的处理速度。
tar -zxf pcre-8.38.tar.gz
cd pcre-8.38
./configure --prefix=/usr/local/pcre-8.38 --enable-jit
make && make install
ZLIB 库
官网:https://www.zlib.net/,下载源码包进行编译安装。
支持头压缩。供 Nginx 的 gzip 模块使用。
tar -zxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib-1.2.11
make && make install
OpenSSL
官网:https://www.openssl.org/,下载源码包进行编译安装。
支持 HTTPS 协议。供 Nginx 的 ssl 模块使用。
tar -zxf openssl-1.0.1h.tar.gz
cd openssl-1.0.1h
./config --prefix=/usr/local/openssl-1.0.1h
make -j 4
make install
jemalloc
官网:http://jemalloc.net/,下载源码包进行编译安装。
jemalloc 是通用的 malloc3 实现,它强调避免碎片和可扩展的并发支持。可有效地优化内存。
yum -y install bzip2
tar -xvf jemalloc-3.6.0.tar.bz2
cd jemalloc-3.6.0
./configure --prefix=/usr/local/jemalloc --libdir=/usr/local/lib
make -j 4
make install
#使jemalloc的库可被系统找到
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
gperftools
官方文档:https://gperftools.github.io/gperftools/cpuprofile.html
Google 的性能分析工具,下载源码包进行编译安装。
PS:64 位系统需要先安装 libunwind,下载源码包进行编译安装。
tar -xvf libunwind-1.3.1.tar.gz
cd libunwind-1.3.1
./configure --prefix=/usr/local/libunwind --libdir=/usr/local/lib
make -j 4
make install
#使libunwind的库可被系统找到
#上面已经将/usr/local/lib添加到配置文件中,只需刷新即可
ldconfig
tar -xvf gperftools-2.7.tar.gz
cd gperftools-2.7
./configure --prefix=/usr/local/gperftools --libdir=/usr/local/lib --enable-frame-pointers
make -j 4
make install
#使libunwind的库可被系统找到
#上面已经将/usr/local/lib添加到配置文件中,只需刷新即可
ldconfig
编译安装 Nginx
注意一:
"./configure"编译时,指定的库路径为源码包路径。例如:pcre、openssl、zlib 等。
注意二:
需要修改 nginx 源代码中关于 openssl 路径的配置,不然 make 的时候会报错。
vim nginx-1.14.2/auto/lib/openssl/conf
# 1 删除CORE_INCS和CORE_DEPS中的.openssl
# 2 删除CORE_LIBS中的.openssl/lib
CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
#更改后的结果
CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
注意三:
nginx 版本大于 1.14.0 时,添加 nginx-upstream-fair 插件需要修改源代码。
vim ngx_http_upstream_fair_module.c
# 修改 543 和 553 行
/* an upstream implicitly defined by proxy_pass, etc. */
543 /* if (us->port == 0 && us->default_port == 0) { */
if (us->port == 0) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"no port in upstream \"%V\" in %s:%ui",
&us->host, us->file_name, us->line);
return NGX_ERROR;
}
ngx_memzero(&u, sizeof(ngx_url_t));
u.host = us->host;
553 /* u.port = (in_port_t) (us->port ? us->port : us->default_port); */
u.port = us->port;
if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
if (u.err) {
注意四:nginx 从 1.9.11 版本开始,才支持动态加载模块(dynamic)
tar -xvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-openssl=/usr/local/src/nginx/openssl-1.0.1h/ \
--with-openssl-opt=-fPIC \
--with-pcre=/usr/local/src/nginx/pcre-8.38/ \
--with-pcre-opt=-fPIC \
--with-pcre-jit \
--with-zlib=/usr/local/src/nginx/zlib-1.2.11/ \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-google_perftools_module \
--with-ld-opt=-ljemalloc \
--with-threads \
--add-module=/usr/local/src/nginx/headers-more-nginx-module-master \
--add-module=/usr/local/src/nginx/nginx-http-concat-master \
--add-module=/usr/local/src/nginx/nginx_upstream_check_module-master \
--add-module=/usr/local/src/nginx/nginx-upstream-fair-master \
--add-module=/usr/local/src/nginx/ngx_cache_purge-master \
--modules-path=/usr/local/nginx/conf/modules \
--with-stream=dynamic \
--with-stream_ssl_module
make -j 4
make install
可以参考之前的文章 “Nginx 编译参数详解”,来了解编译参数的含义。
#复制nginx运行文件,方便使用
cp -p /usr/local/nginx/sbin/nginx /usr/local/bin
#检查配置文件语法是否正确
nginx -t
#启动
nginx
#自启
vim /etc/rc.local
#添加
/usr/local/nginx/sbin/nginx
chmod +x /etc/rc.local