首先,在官网下载 nginx 的源码包,有3种版本能够选择:html
我选择的是 Stable version 中的 1.12.2,是适用于生产环境的最新稳定版本。nginx
下载后能够解压移动到/usr/local/bin目录下:浏览器
mv nginx-1.12.2.tar.gz /usr/local/bin
在官方文档中能够找到 Installing nginx → Building nginx from Sources,这里介绍了不少编译时可配置的选项,大可能是各类路径的配置以及依赖的模块。最下面给出了一个配置示例:bash
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11
对于路径,由于我是新手,因此决定不作配置,用默认的就好;
对于模块,看起来这几个仍是要配置一下的。ui
在官网下载页下到最新稳定版 1.1.0g。.net
在 PCRE 官网能够找到下载地址,注意有两个大版本:8.x和10.x,我最开始下了一个10.x的版本,编译失败了,可能8.x和10.x的接口并不兼容,并且nginx依赖的是8.x。因此这里选择了 pcre-8.41.tar.bz2。是8.x的最高版本,也和示例中的版本一致。code
zlib 直接选择官网首页最新的 1.2.11 版本就能够,也是示例中的版本。htm
一样把这几个库解压,而后也移动到/usr/local/bin
(和 nginx 同目录):接口
mv openssl-1.1.0g pcre-8.41 zlib-1.2.11 /usr/local/bin
进入以前解压的 nginx 目录:ip
cd /usr/lcoal/bin/nginx-1.12.2
执行配置命令,几个依赖包的路径对就能够,官方文档提示要写到一行:
./configure --with-http_ssl_module --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.0g
一阵 checking 无报错信息以后配置成功——
其实我最开始没有装 OpenSSL,配置的报错提示仍是很友好的:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
因此后面我就配置了 --with-openssl
。
——而后就能够编译了:
make
一阵编译无报错信息以后安装:
sudo make install
赶忙试一试:
cd /usr/local/nginx sudo sbin/nginx
去浏览器打开 127.0.0.1,看到 nginx 的欢迎页就大功告成啦。