---
title: "Nginx源码构建、编译并安装"
author: "bert82503"
date: "2015-05-27T19:25:26Z"
tags: []
categories: ["互联网"]
slug: "nginx"
draft: false
lastmod: "2015-05-29T00:06:18Z"
id: 2214904
url: "https://bert82503.iteye.com/blog/2214904"
---

本文主要记录自己在“从Nginx源码构建、编译并安装”过程中遇到的一些问题及详细步骤，通过以下方式构建的Nginx集成了 SSL、[Lua](http://www.lua.org/)、[Nginx Upstream Health Check](http://openresty.org/#LuaRestyUpstreamHealthcheckLibrary)、解压缩 功能。

 

**# 1. 下载安装包**（[http://nginx.org/en/download.html](http://nginx.org/en/download.html)）

**wget** [http://nginx.org/download/**nginx**-1.7.10.tar.gz](http://nginx.org/en/download.html)  
wget [ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/**pcre**-8.36.tar.gz](http://pcre.org/)  
wget [http://openssl.org/source/**openssl**-1.0.2a.tar.gz](http://openssl.org/source/)  
wget [http://zlib.net/**zlib**-1.2.8.tar.gz](http://zlib.net/)  
wget [https://github.com/simpl/**ngx\_devel\_kit**/archive/v0.2.19.tar.gz](https://github.com/simpl/ngx_devel_kit/releases) -O ngx\_devel\_kit-0.2.19.tar.gz  
wget [https://github.com/openresty/**lua-nginx-module**/archive/v0.9.15.tar.gz](https://github.com/openresty/lua-nginx-module/releases) -O lua-nginx-module-0.9.15.tar.gz

wget [https://github.com/openresty/**lua-upstream-nginx-module**/archive/v0.02.tar.gz](https://github.com/openresty/lua-upstream-nginx-module/archive/v0.02.tar.gz) -O lua-upstream-nginx-module-0.02.tar.gz

 

 

**# 2. 解压缩安装包，并创建“软链接”**  
**tar -xzvf** **nginx**-1.7.10.tar.gz

tar -xzvf **pcre**-8.36.tar.gz  
mv pcre-8.36 nginx-1.7.10  
tar -xzvf **openssl**-1.0.2a.tar.gz  
mv openssl-1.0.2a nginx-1.7.10  
tar -xzvf **zlib**-1.2.8.tar.gz  
mv zlib-1.2.8 nginx-1.7.10  
tar -xzvf **ngx\_devel\_kit**-0.2.19.tar.gz  
mv ngx\_devel\_kit-0.2.19 nginx-1.7.10  
tar -xzvf **lua-nginx-module**-0.9.15.tar.gz  
mv lua-nginx-module-0.9.15 nginx-1.7.10

tar -xzvf **lua-upstream-nginx-module**-0.02.tar.gz  
mv lua-upstream-nginx-module-0.02 nginx-1.7.10  
**mv nginx-1.7.10 /usr/apps/nginx-1.7.10**

cd **/usr/apps**  
**ln -s** nginx-1.7.10 **nginx**  
cd nginx

 

 

**# 3. 修改Nginx的openssl编译配置信息并编译OpenSSL**

\# 3.1 修改 **nginx/auto/lib/openssl/conf**

\# 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\_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**"

 

\# 3.2 编译并构建OpenSSL

cd openssl-1.0.2a/

./config  
make

cd ..

 

【问题】

1\. make\[1]: \*\** \[/apps/lib/openssl-1.0.2a/**.openssl/include/openssl/ssl.h**] Error 2

[http://blog.csdn.net/signmem/article/details/18974587](http://blog.csdn.net/signmem/article/details/18974587)

2. cc: error: openssl-1.0.2a/**libssl.a**: 未找到相关文件或目录、cc: error: openssl-1.0.2a/**libcrypto.a**: 未找到相关文件或目录

原因是没有先对OpenSSL进行编译构建，需要先构建生成这两个包。

 

**# 4. build（构建）**

**./configure** --prefix=/usr/apps/nginx \\  
\--with-ld-opt="-Wl,-rpath,**$LUAJIT\_LIB**" \\

--with-http\_ssl\_module \\

--with-pcre=pcre-8.36 \\  
\--with-openssl=openssl-1.0.2a \\  
\--with-zlib=zlib-1.2.8 \\  
\--add-module=ngx\_devel\_kit-0.2.19 \\  
\--add-module=lua-nginx-module-0.9.15 \\  
\--add-module=lua-upstream-nginx-module-0.02

 

==== 构建日志 ====  
checking for OS  
\+ Linux 3.13.0-24-generic x86\_64  
checking for **C compiler** ... found  
\+ using **GNU C compiler**  
+ **gcc version: 4.8.2** (Ubuntu 4.8.2-19ubuntu1)   
...  
**configuring additional modules**  
adding module in **ngx\_devel\_kit**-0.2.19  
+ **ngx\_devel\_kit** was configured  
adding module in **lua-nginx-module**-0.9.15  
**checking for LuaJIT library** in /usr/apps/luajit/lib and /usr/apps/luajit/include/luajit-2.0 (**specified by the LUAJIT\_LIB and LUAJIT\_INC env**) ... found  
checking for export symbols by default (-E) ... found  
checking for export symbols by default (--export-all-symbols) ... not found  
checking for SO\_PASSCRED ... found  
+ **ngx\_http\_lua\_module** was configured

adding module in **lua-upstream-nginx-module**-0.02  
+ **ngx\_http\_lua\_upstream\_module** was configured  
creating objs/Makefile

Configuration summary (配置摘要)

\+ using PCRE library: pcre-8.36  
\+ using OpenSSL library: openssl-1.0.2a  
+ **md5**: using **OpenSSL** library  
+ **sha1**: using OpenSSL library  
\+ using zlib library: zlib-1.2.8

**nginx path prefix**: "/usr/apps/**nginx**"  
nginx **binary file**: "/usr/apps/nginx/**sbin/nginx**"  
nginx configuration prefix: "/usr/apps/nginx/conf"  
nginx **configuration file**: "/usr/apps/nginx/**conf/nginx.conf**"  
nginx **pid file**: "/usr/apps/nginx/**logs/nginx.pid**"  
nginx **error log file**: "/usr/apps/nginx/**logs/error.log**"  
nginx **http access log file**: "/usr/apps/nginx/**logs/access.log**"  
nginx **http client request body temporary files**: "client\_body\_temp"  
nginx **http proxy temporary files**: "proxy\_temp"  
nginx http fastcgi temporary files: "fastcgi\_temp"  
nginx http uwsgi temporary files: "uwsgi\_temp"  
nginx http scgi temporary files: "scgi\_temp"

 

 

**# 5. make（编译）**  
**make -j2**

==== 编译日志 ====  
...  
cc -o objs/nginx \\  
objs/src/core/nginx.o \\  
objs/src/core/ngx\_**log**.o \\  
objs/src/core/ngx\_palloc.o \\  
objs/src/core/ngx\_array.o \\  
objs/src/core/ngx\_list.o \\  
objs/src/core/ngx\_hash.o \\  
objs/src/core/ngx\_buf.o \\  
objs/src/core/ngx\_queue.o \\  
objs/src/core/ngx\_**output\_chain**.o \\  
objs/src/core/ngx\_string.o \\  
objs/src/core/ngx\_parse.o \\  
objs/src/core/ngx\_inet.o \\  
objs/src/core/ngx\_file.o \\  
objs/src/core/ngx\_crc32.o \\  
objs/src/core/ngx\_murmurhash.o \\  
objs/src/core/ngx\_md5.o \\  
objs/src/core/ngx\_rbtree.o \\  
objs/src/core/ngx\_radix\_tree.o \\  
objs/src/core/ngx\_slab.o \\  
objs/src/core/ngx\_times.o \\  
objs/src/core/ngx\_shmtx.o \\  
objs/src/core/ngx\_**connection**.o \\  
objs/src/core/ngx\_cycle.o \\  
objs/src/core/ngx\_spinlock.o \\  
objs/src/core/ngx\_cpuinfo.o \\  
objs/src/core/ngx\_conf\_file.o \\  
objs/src/core/ngx\_resolver.o \\  
objs/src/core/ngx\_open\_file\_cache.o \\  
objs/src/core/ngx\_crypt.o \\  
objs/src/core/ngx\_proxy\_protocol.o \\  
objs/src/core/ngx\_syslog.o \\  
objs/src/event/ngx\_**event**.o \\  
objs/src/event/ngx\_event\_timer.o \\  
objs/src/event/ngx\_event\_posted.o \\  
objs/src/event/ngx\_event\_busy\_lock.o \\  
objs/src/event/ngx\_**event\_accept**.o \\  
objs/src/event/ngx\_**event\_connect**.o \\  
objs/src/event/ngx\_**event\_pipe**.o \\  
objs/src/os/unix/ngx\_**time**.o \\  
objs/src/os/unix/ngx\_errno.o \\  
objs/src/os/unix/ngx\_alloc.o \\  
objs/src/os/unix/ngx\_files.o \\  
objs/src/os/unix/ngx\_**socket**.o \\  
objs/src/os/unix/ngx\_**recv**.o \\  
objs/src/os/unix/ngx\_**readv\_chain**.o \\  
objs/src/os/unix/ngx\_udp\_recv.o \\  
objs/src/os/unix/ngx\_**send**.o \\  
objs/src/os/unix/ngx\_**writev\_chain**.o \\  
objs/src/os/unix/ngx\_**channel**.o \\  
objs/src/os/unix/ngx\_shmem.o \\  
objs/src/os/unix/ngx\_**process**.o \\  
objs/src/os/unix/ngx\_daemon.o \\  
objs/src/os/unix/ngx\_setaffinity.o \\  
objs/src/os/unix/ngx\_setproctitle.o \\  
objs/src/os/unix/ngx\_**posix\_init**.o \\  
objs/src/os/unix/ngx\_user.o \\  
objs/src/os/unix/ngx\_**process\_cycle**.o \\  
objs/src/os/unix/ngx\_linux\_init.o \\  
objs/src/event/modules/ngx\_**epoll**\_module.o \\  
objs/src/os/unix/ngx\_linux\_sendfile\_chain.o \\  
objs/src/core/ngx\_regex.o \\  
objs/src/http/ngx\_**http**.o \\  
objs/src/http/ngx\_**http\_core**\_module.o \\  
objs/src/http/ngx\_http\_special\_response.o \\  
objs/src/http/ngx\_**http\_request**.o \\  
objs/src/http/ngx\_http\_parse.o \\  
objs/src/http/ngx\_http\_header\_filter\_module.o \\  
objs/src/http/ngx\_http\_write\_filter\_module.o \\  
objs/src/http/ngx\_http\_copy\_filter\_module.o \\  
objs/src/http/modules/ngx\_**http\_log**\_module.o \\  
objs/src/http/ngx\_**http\_request\_body**.o \\  
objs/src/http/ngx\_http\_variables.o \\  
objs/src/http/ngx\_http\_script.o \\  
objs/src/http/ngx\_**http\_upstream**.o \\  
objs/src/http/ngx\_**http\_upstream\_round\_robin**.o \\  
objs/src/http/ngx\_http\_parse\_time.o \\  
objs/src/http/modules/ngx\_http\_static\_module.o \\  
objs/src/http/modules/ngx\_http\_index\_module.o \\  
objs/src/http/modules/ngx\_http\_chunked\_filter\_module.o \\  
objs/src/http/modules/ngx\_http\_range\_filter\_module.o \\  
objs/src/http/modules/ngx\_http\_headers\_filter\_module.o \\  
objs/src/http/modules/ngx\_http\_not\_modified\_filter\_module.o \\  
objs/src/http/ngx\_http\_busy\_lock.o \\  
objs/src/http/ngx\_http\_file\_cache.o \\  
objs/src/http/modules/ngx\_http\_gzip\_filter\_module.o \\  
objs/src/http/ngx\_http\_postpone\_filter\_module.o \\  
objs/src/http/modules/ngx\_http\_ssi\_filter\_module.o \\  
objs/src/http/modules/ngx\_http\_charset\_filter\_module.o \\  
objs/src/http/modules/ngx\_http\_userid\_filter\_module.o \\  
objs/src/http/modules/ngx\_http\_autoindex\_module.o \\  
objs/src/http/modules/ngx\_http\_auth\_basic\_module.o \\  
objs/src/http/modules/ngx\_**http\_access**\_module.o \\  
objs/src/http/modules/ngx\_http\_limit\_conn\_module.o \\  
objs/src/http/modules/ngx\_**http\_limit\_req**\_module.o \\  
objs/src/http/modules/ngx\_http\_geo\_module.o \\  
objs/src/http/modules/ngx\_http\_map\_module.o \\  
objs/src/http/modules/ngx\_http\_split\_clients\_module.o \\  
objs/src/http/modules/ngx\_http\_referer\_module.o \\  
objs/src/http/modules/ngx\_**http\_rewrite**\_module.o \\  
objs/src/http/modules/ngx\_**http\_proxy**\_module.o \\  
objs/src/http/modules/ngx\_http\_fastcgi\_module.o \\  
objs/src/http/modules/ngx\_http\_uwsgi\_module.o \\  
objs/src/http/modules/ngx\_http\_scgi\_module.o \\  
objs/src/http/modules/ngx\_http\_memcached\_module.o \\  
objs/src/http/modules/ngx\_http\_empty\_gif\_module.o \\  
objs/src/http/modules/ngx\_http\_browser\_module.o \\  
objs/src/http/modules/ngx\_**http\_upstream\_hash**\_module.o \\  
objs/src/http/modules/ngx\_http\_upstream\_ip\_hash\_module.o \\  
objs/src/http/modules/ngx\_**http\_upstream\_least\_conn**\_module.o \\  
objs/src/http/modules/ngx\_**http\_upstream\_keepalive**\_module.o \\  
objs/addon/src/ndk.o \\  
objs/addon/src/ngx\_http\_**lua\_script**.o \\  
objs/addon/src/ngx\_http\_lua\_log.o \\  
objs/addon/src/ngx\_http\_lua\_subrequest.o \\  
objs/addon/src/ngx\_http\_lua\_ndk.o \\  
objs/addon/src/ngx\_http\_lua\_control.o \\  
objs/addon/src/ngx\_http\_lua\_**time**.o \\  
objs/addon/src/ngx\_http\_lua\_misc.o \\  
objs/addon/src/ngx\_http\_lua\_variable.o \\  
objs/addon/src/ngx\_http\_lua\_string.o \\  
objs/addon/src/ngx\_http\_lua\_output.o \\  
objs/addon/src/ngx\_http\_lua\_headers.o \\  
objs/addon/src/ngx\_http\_lua\_req\_body.o \\  
objs/addon/src/ngx\_http\_lua\_uri.o \\  
objs/addon/src/ngx\_http\_lua\_**args**.o \\  
objs/addon/src/ngx\_http\_lua\_ctx.o \\  
objs/addon/src/ngx\_http\_lua\_regex.o \\  
objs/addon/src/ngx\_http\_lua\_module.o \\  
objs/addon/src/ngx\_http\_lua\_headers\_out.o \\  
objs/addon/src/ngx\_http\_lua\_headers\_in.o \\  
objs/addon/src/ngx\_http\_lua\_directive.o \\  
objs/addon/src/ngx\_http\_lua\_consts.o \\  
objs/addon/src/ngx\_http\_lua\_exception.o \\  
objs/addon/src/ngx\_http\_lua\_util.o \\  
objs/addon/src/ngx\_http\_lua\_cache.o \\  
objs/addon/src/ngx\_http\_**lua\_contentby**.o \\  
objs/addon/src/ngx\_http\_**lua\_rewriteby**.o \\  
objs/addon/src/ngx\_http\_**lua\_accessby**.o \\  
objs/addon/src/ngx\_http\_**lua\_setby**.o \\  
objs/addon/src/ngx\_http\_lua\_capturefilter.o \\  
objs/addon/src/ngx\_http\_lua\_clfactory.o \\  
objs/addon/src/ngx\_http\_lua\_pcrefix.o \\  
objs/addon/src/ngx\_http\_lua\_headerfilterby.o \\  
objs/addon/src/ngx\_http\_lua\_shdict.o \\  
objs/addon/src/ngx\_http\_**lua\_socket\_tcp**.o \\  
objs/addon/src/ngx\_http\_lua\_api.o \\  
objs/addon/src/ngx\_http\_lua\_logby.o \\  
objs/addon/src/ngx\_http\_lua\_sleep.o \\  
objs/addon/src/ngx\_http\_lua\_coroutine.o \\  
objs/addon/src/ngx\_http\_lua\_bodyfilterby.o \\  
objs/addon/src/ngx\_http\_**lua\_initby**.o \\  
objs/addon/src/ngx\_http\_**lua\_initworkerby**.o \\  
objs/addon/src/ngx\_http\_lua\_socket\_udp.o \\  
objs/addon/src/ngx\_http\_lua\_req\_method.o \\  
objs/addon/src/ngx\_http\_lua\_phase.o \\  
objs/addon/src/ngx\_http\_**lua\_uthread**.o \\  
objs/addon/src/ngx\_http\_lua\_timer.o \\  
objs/addon/src/ngx\_http\_lua\_config.o \\  
objs/addon/src/ngx\_http\_lua\_worker.o \\  
objs/ngx\_modules.o \\  
\-Wl,-rpath,/usr/apps/luajit/lib -Wl,-E -lpthread -lcrypt -L/usr/apps/luajit/lib -lluajit-5.1 -lm pcre-8.36/.libs/libpcre.a zlib-1.2.8/libz.a  
make\[1]:正在离开目录 \`/usr/apps/nginx-1.7.10'  
make -f objs/Makefile manpage  
make\[1]: 正在进入目录 \`/usr/apps/nginx-1.7.10'  
sed -e "s|%%PREFIX%%|/usr/apps/nginx|" \\  
\-e "s|%%**PID\_PATH**%%|/usr/apps/nginx/**logs/nginx.pid**|" \\  
\-e "s|%%**CONF\_PATH**%%|/usr/apps/nginx/**conf/nginx.conf**|" \\  
\-e "s|%%**ERROR\_LOG\_PATH**%%|/usr/apps/nginx/**logs/error.log**|" \\  
&lt; man/nginx.8 &gt; objs/nginx.8  
make\[1]:正在离开目录 \`/usr/apps/nginx-1.7.10'

 

 

**# 6. install（安装）**  
**make install**