在中国人的母语中,对以下内容进行释义:Nginx编译备忘录.

在从源代码编译Nginx时,我希望尽可能指定与软件包相适应的编译选项,所以进行了一些笔记。
这是我个人的记录,如果有错别字请谅解。欢迎提出改进意见或讽刺。
我更希望能有其他更好的方法,如果有的话,请务必告诉我!谢谢!

如果您急需,请尝试以下三个yum,应该可以获取所需的软件包。

# yum groupinstall "Development Tools"
# yum install pcre-devel openssl-devel libxslt-devel gd-devel perl-ExtUtils-Embed epel-release
# yum install GeoIP-devel

由于需要从epel获取GeoIP-devel,因此需要提前安装epel-release并添加仓库。

环境 –

CentOS 7
最小安装
我只安装了vim和screen(哈哈)

安装额外的软件包

由于gcc等很麻烦,我选择使用DevelopmentTools进行批量安装。
对于仍然缺少的库,我会逐个进行安装。

# yum groupinstall "Development Tools"

安装预构建软件包

为了确认configure选项,先安装预编译软件包。

添加nginx存储库(此次为CentOS7版本)

# cat > /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

使用yum进行安装

# yum install nginx

确认configure选项

# nginx -V
nginx version: nginx/1.9.13
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --add-dynamic-module=njs-91543c86f412/nginx --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --with-ld-opt=-Wl,-E

准备nginx的源代码文件

请下载与“Pre-Built Packages中可安装的版本一致”的tar.gz版本。

nginx: 下载
http://nginx.org/en/download.html

本次获取Mainline版本的「nginx-1.9.13」,并将其放置在/usr/local/src目录下。

# cd /usr/local/src/
# curl -L -O http://nginx.org/download/nginx-1.9.13.tar.gz

配置完成後即可展開。

# cd /usr/local/src/
# tar zxvf ./nginx-1.9.13.tar.gz

由于包装版是以动态模块的形式加载nginScript(njs),所以提前准备njs也是必要的。
※根据nginx的构建版本,njs的版本也可能不同,因此需要准备所需的njs来编译希望的nginx。(我曾被这个问题卡住过一下,哈哈)

# curl -L -O http://hg.nginx.org/njs/archive/tip.tar.gz
# tar zxvf ./tip.tar.gz
# mv ./njs-91543c86f412 ./nginx-1.9.13
# cd ./nginx-1.9.13/njs-91543c86f412
# ./configure 

其实,符合常规的做法应该是从Mercurial仓库进行克隆,不过为了避免麻烦,我选择直接下载tar.gz文件来应付哈哈。

进行配置

# ./configure                                                                                                               
configuring for Linux 3.10.0-327.13.1.el7.x86_64 x86_64
checking for C compiler: cc
 + using GNU C compiler
 + gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
checking for posix_memalign() ... found
checking for getrandom() ... not found

./auto/configure: error: no PCRE library found.

由于缺少PCRE,我决定使用yum进行安装。

# yum install pcre-devel

重新配置

# ./configure                                                                                                          
configuring for Linux 3.10.0-327.13.1.el7.x86_64 x86_64
checking for C compiler: cc
 + using GNU C compiler
 + gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
checking for posix_memalign() ... found
checking for getrandom() ... not found
checking for PCRE library ... found
 + PCRE version: 8.32

确认没有错误后进行make操作。

# make
(省略)

确认make没有错误,然后返回到nginx的目录中。

# cd ../

nginx的第一个configure选项

# ./configure \
 --prefix=/etc/nginx \
 --sbin-path=/usr/sbin/nginx \
 --modules-path=/usr/lib64/nginx/modules \
 --conf-path=/etc/nginx/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log \
 --pid-path=/var/run/nginx.pid \
 --lock-path=/var/run/nginx.lock \
 --http-client-body-temp-path=/var/cache/nginx/client_temp \
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
 --user=nginx \
 --group=nginx \
 --with-http_ssl_module \
 --with-http_realip_module \
 --with-http_addition_module \
 --with-http_sub_module \
 --with-http_dav_module \
 --with-http_flv_module \
 --with-http_mp4_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_random_index_module \
 --with-http_secure_link_module \
 --with-http_stub_status_module \
 --with-http_auth_request_module \
 --with-http_xslt_module=dynamic \
 --with-http_image_filter_module=dynamic \
 --with-http_geoip_module=dynamic \
 --with-http_perl_module=dynamic \
 --add-dynamic-module=njs-91543c86f412/nginx \
 --with-threads \
 --with-stream \
 --with-stream_ssl_module \
 --with-http_slice_module \
 --with-mail \
 --with-mail_ssl_module \
 --with-file-aio \
 --with-ipv6 \
 --with-http_v2_module \
 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' \
 --with-ld-opt=-Wl,-E

(中略)
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... not found
checking for OpenSSL library in /usr/local/ ... not found
checking for OpenSSL library in /usr/pkg/ ... not found
checking for OpenSSL library in /opt/local/ ... not found

./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相关的库。
使用yum安装OpenSSL库。

# yum install openssl-devel

再试一次 shì

# ./configure \ (以下略)
(中略)
checking for libxslt ... not found
checking for libxslt in /usr/local/ ... not found
checking for libxslt in /usr/pkg/ ... not found
checking for libxslt in /opt/local/ ... not found

./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.

因为被告知没有libxslt,所以使用yum进行安装。

# yum install libxslt-devel

再三尝试

# ./configure \ (以下略)
(中略)
checking for GD library ... not found
checking for GD library in /usr/local/ ... not found
checking for GD library in /usr/pkg/ ... not found
checking for GD library in /opt/local/ ... not found

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

接下来是GD啊… 在yum上快速安装。

# yum install gd-devel

再试一次

# ./configure \ (以下略)
(中略)
checking for perl
 + perl version: This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

./configure: error: perl module ExtUtils::Embed is required

如果缺少perl模块…这也可以通过yum进行安装。

# yum install perl-ExtUtils-Embed

再试一次

# ./configure \ (以下略)
(中略)
checking for GeoIP library ... not found
checking for GeoIP library in /usr/local/ ... not found
checking for GeoIP library in /usr/pkg/ ... not found
checking for GeoIP library in /opt/local/ ... not found

./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

虽然感觉看不到尽头,但还是重新集中精力,在yum中安装GeoIP。

# yum install GeoIP-devel

再次尝试 cì

# ./configure \ (以下略)
(中略)
creating objs/Makefile

Configuration summary
  + using threads
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/etc/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/lib64/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

通過了!

使用Nginx进行编译和安装

轻松地

# make
# make install

完了! !) -> 噢不好了! (ō bù le!)

为了解决这个问题,我们需要添加ngx_cache_purge模块,这样就可以单独删除nginx缓存。

展开 ngx_cache_purge 的源代码。

# cd /usr/local/src/
# curl -L -O http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
# tar zxvf ./ngx_cache_purge-2.3.tar.gz 

重新配置nginx

# cd ./nginx-1.9.13
# ./configure \
 --prefix=/etc/nginx \
 --sbin-path=/usr/sbin/nginx \
 --modules-path=/usr/lib64/nginx/modules \
 --conf-path=/etc/nginx/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log \
 --pid-path=/var/run/nginx.pid \
 --lock-path=/var/run/nginx.lock \
 --http-client-body-temp-path=/var/cache/nginx/client_temp \
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
 --user=nginx \
 --group=nginx \
 --with-http_ssl_module \
 --with-http_realip_module \
 --with-http_addition_module \
 --with-http_sub_module \
 --with-http_dav_module \
 --with-http_flv_module \
 --with-http_mp4_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_random_index_module \
 --with-http_secure_link_module \
 --with-http_stub_status_module \
 --with-http_auth_request_module \
 --with-http_xslt_module=dynamic \
 --with-http_image_filter_module=dynamic \
 --with-http_geoip_module=dynamic \
 --with-http_perl_module=dynamic \
 --add-dynamic-module=njs-91543c86f412/nginx \
 --with-threads \
 --with-stream \
 --with-stream_ssl_module \
 --with-http_slice_module \
 --with-mail \
 --with-mail_ssl_module \
 --with-file-aio \
 --with-ipv6 \
 --with-http_v2_module \
 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' \
 --with-ld-opt=-Wl,-E \
 --add-module=../ngx_cache_purge-2.3

请确保没有出现错误。

重新编译和安装nginx。

由于已经完成了,因此需要进行一次清理工作。

# make clean
# make
# make install

确认是否编译了nginx

# nginx -V
nginx version: nginx/1.9.13
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --add-dynamic-module=njs-91543c86f412/nginx --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --with-ld-opt=-Wl,-E --add-module=../ngx_cache_purge-2.3

–新增了../ngx_cache_purge-2.3模块!

待日可见

使用SRPM进行重新构建和打包。

从源代码安装,相比于固定二进制包版本,更容易定制,但不适合大规模部署。
嗯,虽然也可以做,但编译会浪费很多时间。
相比之下,将定制的内容打包更快捷且更容易管理。
但是,考虑到后续版本升级的问题,必须妥善保存spec文件等,否则可能会被后人抱怨。

准备酱油

由于nginx官方提供了SRPM包,因此可以下载该包。
http://nginx.org/packages/mainline/centos/7/SRPMS/nginx-1.9.13-1.el7.ngx.src.rpm

# cd /usr/local/src/
# curl -L -O http://nginx.org/packages/mainline/centos/7/SRPMS/nginx-1.9.13-1.el7.ngx.src.rpm

写了一点点就保存了(笑)

bannerAds