この前、Amazon LinuxNginxをインストールしたのだが、リバースプロキシをするにあたって[subs_filter_module]が必要なことがわかった。
それで、ソースコードからインストールすることが必要になったのでその際のメモ。

最初にAmazon LinuxにNginxをインストールしたのは、[yum]でインストールした。
インストールされたNginxのバージョンは1.8.1

目次

configureオプションについて

設定ファイルなどはそのまま使用できるものは使用したいのでパッケージでインストールされた状態のconfigureオプションについて確認した。

$ sudo nginx -V
nginx version: nginx/1.8.1
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_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_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=' -Wl,-E'

なんか、インストール方法の書かれているサイトのオプション以上に大量にあります。パッケージだからだろうけど、多い・・・
とりあえずこれをコピーして、とっておきます。

次に起動ファイルをコピーする

Nginxの起動ファイルはyumでインストールしているので[/etc/init.d]にあります。

$ sudo cp /etc/init.d/nginx /etc/init.d/nginx.bak

パッケージのnginxをアンインストール

パッケージインストールの情報がとれたのでアンインストールする。

$ sudo yum remove nginx

とりあえずここからはroot権限で操作していきます。

$ su -

コンパイラ、Gitなどをインストール

# yum install gcc
# yum install pcre-devel
# yum install git

zlibのダウンロード

Nginxに組み込むためダウンロードする

# cd /usr/local/src
# wget "http://zlib.net/zlib-1.2.8.tar.gz"
# tar zxvf zlib-1.2.8.tar.gz

OpneSSLのダウンロード

OpnesSSLも組み込むので最新のものをダウンロードする

# cd /usr/local/src
# wget "https://www.openssl.org/source/openssl-1.0.1t.tar.gz"
# tar zxvf openssl-1.0.1t.tar.gz

インストールに必要なものがインストールされているか確認

以下のものは、Amazon Linuxには標準でインストールされていなかったものです。
インストールされていなければ、configureの段階でエラーとなります。

# yum list installed | grep libxml2
# yum list installed | grep libxslt
# yum list installed | grep libgcrypt
# yum list installed | grep perl-ExtUtils-Embed
# yum list installed | grep gd

インストールされていないものをインストール

libxml2/libxsltをインストール

# yum install libxslt libxslt-devel
# yum install libxml2 libxml2-devel

gdのインストール

# yum install gd-devel

perl-ExtUtils-Embedのインストール

# yum install google-perftools google-perftools-devel

GeoIPのインストール

最新バージョンを下記URLにアクセスして確認
http://pkgs.repoforge.org/rpmforge-release/

# cd /usr/local/src
# wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
# rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
# vi /etc/yum.repos.d/rpmforge.repo
enabled = 1→0に変更(通常使用しないように設定)

# yum --enablerepo=rpmforge install geoip-devel

リバースプロキシに必要なsubs_filter_moduleモジュールのダウンロード

Githubからダウンロードします。

# mkdir /usr/local/src/subs_filter_module
# cd /usr/local/src/subs_filter_module
# git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git

Nginxのソースコードダウンロード

2016年5月現在のStable versionである、1.10.0をインストールするためダウンロード

# mkdir /usr/local/src/nginx
# cd /usr/local/src/nginx
# wget http://nginx.org/download/nginx-1.10.0.tar.gz
# tar zxvf nginx-1.10.0.tar.gz
# cd nginx-1.10.0

ソースコードをビルドする

1.8.1と1.10.0では、一部モジュールの変更があるので変更し、追加するものを付け足す。
変更点
–with-spdy_module

–with-http_v2_module
追加
–add-module=/usr/local/src/subs_filter_module/ngx_http_substitutions_filter_module
–with-openssl=/usr/local/src/openssl/openssl-1.0.1t
–with-zlib=/usr/local/src/zlib-1.2.8

# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_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_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=' -Wl,-E' --add-module=/usr/local/src/subs_filter_module/ngx_http_substitutions_filter_module --with-openssl=/usr/local/src/openssl/openssl-1.0.1t --with-zlib=/usr/local/src/zlib-1.2.8

エラーがなければmakeする

# make
# make install

これでインストールが完了。
confファイルは、「/etc/nginx/nginx.conf」にあるので必要な設定を行う。
起動ファイルを用意
パッケージインストール時にコピーしておいた起動ファイルを戻す。
# mv /etc/init.d/nginx.bak /etc/init.d/nginx
もしくは、デベロッパーサイトの下記ページより、インストール先サーバーのOSに合わせて起動ファイルをダウンロードして設置します。
http://wiki.nginx.org/InitScripts
この場合は、必要箇所をインストール環境に合わせて修正が必要となります。

Nginxの基本コマンド

起動ファイルを使用する場合

起動

/etc/init.d/nginx start

停止

/etc/init.d/nginx stop

再起動

/etc/init.d/nginx restart

設定ファイルの再読み込み(Apacheのgracefulに相当)

/etc/init.d/nginx reload

設定ファイル文法チェック

/etc/init.d/nginx configtest

状態確認

/etc/init.d/nginx status