kakakakakku blog

Weekly Tech Blog: Keep on Learning!

Chef で nginx-build を使って nginx をインストールしてみた

前から気になってた nginx-build を少し使ってみて,非常に良さそうだったので,Chef から実行できるようにしてみた.レシピはもっと改善して書けそう.nginx-build に関しては以下を参照で良いかと.

github.com

tech.mercari.com

レシピ

以下に nginx 1.9.15 (Mainline) をインストールするレシピを載せた.ビルドオプション,モジュール,サードパーティモジュールは参考として見てもらえればと思う.nginx-build ではサードパーティモジュールを modules3rd.ini に定義することができて -m でビルドできる.これは凄く便利だなと思った.今回は例として headers-more-nginx-module を含めた.

modules3rd.ini

[headers-more-nginx-module]
form=git
url=https://github.com/openresty/headers-more-nginx-module.git
rev=v0.30

default.rb

#
# Cookbook Name:: nginx
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

remote_file '/tmp/nginx-build.tar.gz' do
  source 'https://github.com/cubicdaiya/nginx-build/releases/download/v0.9.3/nginx-build-linux-amd64-0.9.3.tar.gz'
  action :create_if_missing
end

cookbook_file '/tmp/modules3rd.ini' do
  source 'modules3rd.ini'
end

%w(
  /etc/nginx
  /etc/nginx/conf.d
).each do |d|
  directory d do
    mode '0755'
    owner 'root'
    group 'root'
    action :create
  end
end

execute 'unpack .tar.gz' do
  cwd '/tmp'
  command <<-EOF
    tar xvfz nginx-build.tar.gz
  EOF
  creates '/tmp/nginx-build'
end

nginx_version = '1.9.15'

execute 'install nginx' do
  cwd '/tmp'
  command <<-EOF
    ./nginx-build \
      -d work \
      -v #{nginx_version} \
      -m modules3rd.ini \
      --sbin-path=/usr/local/nginx/sbin/nginx \
      --conf-path=/etc/nginx/nginx.conf \
      --pid-path=/var/run/nginx/nginx.pid \
      --lock-path=/var/lock/subsys/nginx.lock \
      --http-log-path=/var/log/nginx/access.log \
      --error-log-path=/var/log/nginx/error.log \
      --with-http_ssl_module \
      --with-http_realip_module \
      --with-http_addition_module \
      --with-http_xslt_module \
      --with-http_sub_module \
      --with-http_dav_module \
      --with-http_flv_module \
      --with-http_gzip_static_module \
      --with-http_random_index_module \
      --with-http_secure_link_module \
      --with-http_stub_status_module

    cd /tmp/work/nginx/#{nginx_version}/nginx-#{nginx_version}
    sudo make install
  EOF
  creates '/usr/local/nginx/sbin/nginx'
end

インストール確認

$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.9.15
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --add-module=../headers-more-nginx-module --pid-path=/var/run/nginx/nginx.pid --sbin-path=/usr/local/nginx/sbin/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --conf-path=/etc/nginx/nginx.conf --lock-path=/var/lock/subsys/nginx.lock --with-http_dav_module --with-http_realip_module --with-http_secure_link_module --with-http_sub_module --with-http_stub_status_module --with-http_random_index_module --with-http_addition_module --with-http_xslt_module --with-http_gzip_static_module --with-http_ssl_module --with-http_flv_module

バージョン指定

nginx-build は -v でバージョンを指定することができるため,例えば Stable の 1.10.0 をインストールすることもできる.今回は変数 nginx_version に定義しているけど,attributes に書いた方が良いとは思う.

$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.10.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --add-module=../headers-more-nginx-module --http-log-path=/var/log/nginx/access.log --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx/nginx.pid --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/subsys/nginx.lock --with-http_flv_module --with-http_gzip_static_module --with-http_dav_module --with-http_secure_link_module --with-http_addition_module --with-http_xslt_module --with-http_stub_status_module --with-http_random_index_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module

Mainline と Stable

nginx には主に Mainline と Stable バージョンがあり,現在は以下のようになっている.

  • Mainline : 1.9.15
  • Stable : 1.10.0

名前だけ見ると Stable を選びたくなるけど,基本的には最新の Mainline を選ぶことが推奨されている.この話は実は今まで知らなくて「nginx 実践入門」を読んでて初めて知った.

nginx実践入門 (WEB+DB PRESS plus)

nginx実践入門 (WEB+DB PRESS plus)

まとめ

nginx-build は Golang 製だから,remote_file でバイナリを落としてくるだけですぐ使えるっていう手軽さが素晴らしいなと思った.