unicorn & nginx で tdiary を使う
tdiary に付属した config.ru があるのでほとんど設定することはなかった。
tdiary 自体の設定
tdiary は
git clone https://github.com/tdiary/tdiary-core.git
でダウンロードした。tdiary の設定は http://transitive.info/2011/07/25/serversman-vps-tdiary/ のようになっている。
ruby 2.0
ruby は rvm で 2.0 をインストールした。
rvm install ruby-2.0.0-head
rvm use ruby-2.0.0-head
gem のインストール
cd /path/to/tdiary-core
therubyracer がないと unicorn 動かなかったので Gemfile に
gem 'therubyracer'
を追加する。
bundle update
で最新の gem をインストールする。unicorn もインストールする。
gem install unicorn
unicorn の設定
unicorn の設定を書いたファイルを作成する。ここでは、unicorn.rb というファイル名にした。 log というディレクトリ以下にログと PID を出力している。 preload_app と GC の項目は必要なのかは不明。
worker_processes 2
listen '/tmp/unicorn_tdiary.sock'
stderr_path File.expand_path(File.join(File.dirname(__FILE__), 'log/unicorn.log'))
stdout_path File.expand_path(File.join(File.dirname(__FILE__), 'log/unicorn.log'))
pid stdout_path File.expand_path(File.join(File.dirname(__FILE__), 'log/pid'))
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true
次のようにして unicorn を実行する。
unicorn -c /path/to/unicorn.rb -D
tdiary の .htpasswd
git でダウンロードした tdiary-core ディレクトリに .htpasswd を置けばよい。 私の環境では別のディレクトリに置いてあったので config.ru の方を変更した。
use TDiary::Rack::Auth::Basic, '.htpasswd'
となっているところを
use TDiary::Rack::Auth::Basic, '/path/to/.htpasswd'
のようにする。
nginx
unicorn 上の設定だと /tmp/unicorn_tdiary.sock で実行されているので nginx をリバースプロキシにして /tmp/unicorn_tdiary.sock にアクセスするようにする。 nginx の設定は前に使っていたファイルをコピーしたので不要な部分があるかもしれないので注意。
/etc/nginx/sites-available/example.com に
upstream tdiary_unicorn_server {
  server unix:/tmp/unicorn_tdiary.sock;
}
server {
        listen   80;
        server_name  example.com;
        error_log /var/log/nginx/example.com.error.log;
        access_log  /var/log/nginx/example.com.access.log;
        root /path/to/tdiary-core;
        gzip on;
        gzip_http_version 1.1;
        gzip_types text/plain
                   text/xml
                   text/css
                   application/xml
                   application/xhtml+xml
                   application/rss+xml
                   application/javascript
                   application/x-javascript;
        gzip_buffers 4 8k;
        gzip_min_length 1000;
        gzip_comp_level 1;
        gzip_proxied off;
        gzip_disable "MSIE [1-6]\."  "Mozilla/4";
        gzip_vary off;
        location / {
                proxy_http_version 1.1;
                proxy_set_header    X-Real-IP       $remote_addr;
                proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header    Host            $http_host;
                proxy_redirect      off;
                proxy_max_temp_file_size    0;
                proxy_pass http://tdiary_unicorn_server;
        }
}
のような設定を書き
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
で設定を有効にして
service nginx restart
で設定を読み込ませる。
unicorn の起動
起動するたびに
cd /path/to/tdiary-core
unicorn -c /path/to/unicorn.rb -D
を実行する必要がある。本当は自動で起動するようにすれば良いのだが、サーバで常時起動なのであまり困らない。