Ubuntu 下编译 nginx

1,608 阅读1分钟

Ubuntu / Debian 虽然可以直接使用 apt-get 下载安装 Nginx,但无法添加、自定义一些所需要的模块,如 Brotli、OpenSSL。所以自己编译安装 Nginx 才是最佳的选择。

环境

  • Ubuntu 16.04.4 LTS

nginx编译

安装依赖

ps: 配置和编译过程出现的 error 有很大部分是依赖不全导致的

安装 gcc g++ 的依赖库

sudo apt-get install build-essential
sudo apt-get install libtool

安装 pcre 依赖库(www.pcre.org/)

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

安装 zlib 依赖库(www.zlib.net

sudo apt-get install zlib1g-dev

安装 SSL 依赖库(16.04 默认已经安装了)

sudo apt-get install openssl

安装 nginx

下载源码
# 下载需要的版本
$ wget http://nginx.org/download/nginx-1.19.6.tar.gz
解压
$ tar -zxvf nginx-1.19.6.tar.gz

# 进入解压目录
$ cd nginx-1.19.6
配置
# --prefix=要安装的目录
$ ./configure --prefix=/usr/local/nginx  --with-http_ssl_module --with-openssl=../openssl-1.1.1k
没有添加 --with-openssl=../openssl-1.1.1k 时候遇到的问题
./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.

# 下载 openssl 源码 https://www.openssl.org/source/openssl-1.1.1d.tar.gz,解压到 nginx-1.19.6 的上级目录即可。

# 接着回到 nginx-1.19.6 目录下
$ cd /nginx-1.19.6
编译安装
# 编译
$ make

# 安装到 prefix 指定的目录下
$ make install
指定配置文件启动
$ sudo /nginx/sbin/nginx -c /nginx/conf/nginx.conf

# 注意,-c 后的配置文件要填绝对路径,或者使用 -p . 指定当前路径