LoginSignup
21

More than 5 years have passed since last update.

nginxソースビルドを自動化出来るnginx-buildをサクッと入れるItamaeプラグイン作った

Posted at

nginxは、普通に使うだけなら各ディストリビューションで提供されているパッケージをいれれば良いんですが、サードパーティのモジュールを使いたい時とかはビルドをしなくてはならないです。非常に面倒くさいです。

そんな面倒くさいビルドを自動化してくれるnginx-buildという神ツールがあったので、nginx-buildを導入するItamaeプラグインを作りました。Itamae使っている人は、使ってみてください。

nginx-buildとは

nginx-buildはメルカリの@cubicdaiyaさんがGoで書かれたnginxのビルドプロセスを自動化してくれるツールです。バイナリでも配布されており、Linux以外にもMacのHomebrewにも対応しています。

使い方

nginx-buildの使い方は非常に簡単です。バイナリをダウンロードして設置したら、すぐ使えます。

$ nginx-build -d work -v 1.8.0

これでv1.8.0のnginxがビルドされます。最後のmake installだけは手動で行います。

$ cd work/1.8.0/nginx-1.8.0
$ sudo make install

configureオプション

configureオプションを指定できます。

$ nginx-build -d work -v 1.8.0 -c configure.sh
configure.sh
#!/bin/sh

./configure \
  --user=nginx \
  --group=nginx \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --pid-path=/var/run/nginx.pid \
  --with-http_ssl_module

サードパーティモジュール

サードパーティモジュールも指定すると、ダウンロードから組み込みまで全部自動でやってくれます。すごすぎる。

$ nginx-build -d work -v 1.8.0 -m modules3rd.ini
modules3rd.ini
[ngx_cache_purge]
form=git
url=https://github.com/FRiCKLE/ngx_cache_purge.git
rev=2.3

他にもOpenRestyとかTengineのビルドをサポートしていたりと細かいところまで手の届く素晴らしいツールです。ありがとうございます。詳しいことについては@cubicdaiyaさんの解説記事を読むと良いです。

Itamaeプラグイン

紹介したnginx-buildの導入をItamaeで自動化するようにプラグインがこちらです。

使い方

gem 'itamae-plugin-recipe-nginx_build'
recipe.rb
include_recipe "nginx_build"
include_recipe "nginx_build::install"

これだけで、デフォルトv1.8.0のnginxがインストールされます(後述するオプションで細かく指定できます)。

なにをやってくれるのか

  • nginxに必要なパッケージのインストール
  • nginx-buildのインストール
  • nginx v1.8.0の make install
  • nginxの起動スクリプト設置
  • nginxの起動&自動起動設定

この5つを行います。nginxのホスト毎の設定ファイルは設置しないので、別途レシピに書く必要があります。

オプション

Itamaeのノードファイルで細かく指定できます。すべてオプショナルなので、指定しなかった場合は、下記の値がデフォルトで反映されます(modules / modules3rdsを除く)。

development.yml
nginx_build:
  platform: linux ( or darwin )
  version: 0.6.4
  bin: /usr/local/bin/
  nginx_version: 1.8.0
  nginx_user: nginx
  nginx_group: nginx
  nginx_sbin: /usr/sbin/nginx
  nginx_conf: /etc/nginx/nginx.conf
  nginx_pid: /var/run/nginx.pid
  modules:
    - http_ssl_module
  modules3rds:
    -
      name: ngx_cache_purge
      form: git
      url: https://github.com/FRiCKLE/ngx_cache_purge.git
      rev: 2.3
  configure_path: /usr/local/nginx_build/configure.sh
  modules3rd_path: /usr/local/nginx_build/modules3rd.ini

実際の使い方

必要最低限の設定ファイルしか設置されないので、サーバごとに必要な設定ファイルは別途レシピで書いてあげる感じになります。僕が実際に使っているレシピの一部を抜き出すとこんな感じになります。

nginx/default.rb
include_recipe "nginx_build"
include_recipe "nginx_build::install"

directory "/etc/nginx/conf.d/"
template "/etc/nginx/conf.d/virtual.conf" do
  owner "root"
  group "root"
  mode "644"
  source "./#{node[:role]}/templates/etc/nginx/conf.d/virtual.conf.erb"
  notifies :restart, "service[nginx]"
end

directory "/var/log/nginx/"
template "/etc/logrotate.d/nginx" do
  source "./#{node[:role]}/templates/etc/logrotate.d/nginx.erb"
end

プラグインが動かない環境がたぶんあると思うので、不具合あればIssueなりプルリクなり投げていただけると助かります。

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
21