LoginSignup
40
39

More than 5 years have passed since last update.

nginxのコンパイルメモ

Last updated at Posted at 2016-04-04

nginxをソースからコンパイルする際に、できるだけパッケージに合わせたコンパイルオプションを指定したいと思ったのでメモ。
個人的なメモとして記録していますので、誤字脱字はご容赦。改善点やツッコミは歓迎。
むしろ勉強のために、他にもよい方法があればぜひ教えてください!おねがいします!

お急ぎの方は、以下のyum3つでパッケージは揃うはずなのでお試しを

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

GeoIP-develは、epelから取得するため、事前にepel-releaseをインストールしてリポジトリを追加します。

環境

CentOS 7
最小インストール
追加で入れたのは、vimとscreenのみ(笑)

追加パッケージのインストール

gccとか面倒なので、DevelopmentToolsで一括インストールする。
それでも足りないライブラリは、逐次インストールしていく。

# yum groupinstall "Development Tools"

Pre-Built Packagesのインストール

configureオプションを確認するため、一旦Pre-Built Packagesをインストールする。

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: download
http://nginx.org/en/download.html

今回は、Mainline Versionの「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)がdynamic moduleとしてロードされるため、事前にnjsも準備する
※nginxのビルドバージョンによっては、njsのバージョンも違うため、コンパイルしたいnginxが必要しているnjsを準備する必要がある
(これで一瞬ハマったw)

# 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リポジトリからcloneするのが筋なんだろうけど、わざわざこのためにhgコマンドとか入れるのも面倒だったので、tar.gzを落としてごまかしたw

configureする

# ./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

# ./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関連のライブラリが見つからないとな。
OpenSSLのライブラリをyumでインストールする。

# yum install openssl-devel

再トライ

# ./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.

終わりが見えない気がするけど、気を取り直してGeoIPをyumでインストール。

# yum install GeoIP-devel

再々々々々トライ

# ./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
# make install

完了!

と、これでは不通なので、nginxのキャッシュを個別に削除できるようになるngx_cache_purgeを追加する。

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の再configure

# 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とインストール

make済みなので、一度cleanupを行なう。

# 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

--add-module=../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

ちっと書きかけで保存(笑)

40
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
39