[server] apache + passengerを捨ててnginx + thinにしてみた

nginx と thinの組み合わせが速いらしいので変更してみることにした。

現在運用しているのは

webdavについては別途書くとしてとりあえずrailsアプリとSinatraアプリをthinに。リバースプロキシをnginxにしてみる。

ちなみに環境はCentOS 4.6

以下作業ログ

nginxのインストール

$ sudo su -
# cd /usr/local/src
# wget http://nginx.org/download/nginx-0.7.65.tar.gz
# yum install pcre-devel zlib-devel openssl-devel
# tar -zxvf nginx-0.7.65.tar.gz
# cd nginx-0.7.65
// configure の参考 http://wiki.nginx.org/NginxInstallOptions
// 上記のExample2に--with-sha1あたりを足してconfigureしてみる
# ./configure \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --http-log-path=/var/log/nginx/access.log \
    --with-http_dav_module \
    --http-client-body-temp-path=/var/lib/nginx/body \
    --with-http_ssl_module \
    --http-proxy-temp-path=/var/lib/nginx/proxy \
    --with-http_stub_status_module \
    --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
    --with-debug \
    --with-http_flv_module \
    --with-sha1-asm \
    --with-sha1=/usr/include
# make
# make install

とりあえずポート8080番で動かしてみる

# vi /etc/nginx/nginx.conf
server {
    listen       8080; # 8080番にする
    server_name  localhost;
    #access_log  logs/host.access.log  main;
# /usr/local/nginx/sbin/nginx -t
the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
// OKが出たので起動
# /usr/local/nginx/sbin/nginx
# lsof -i :8080
COMMAND  PID   USER   FD   TYPE    DEVICE SIZE NODE NAME
nginx   6886   root    6u  IPv4 580333295       TCP *:webcache (LISTEN)
nginx   6887 nobody    6u  IPv4 580333295       TCP *:webcache (LISTEN)

thinのインストール

# gem install thin

redmineをthin(でしかもunix socket)で動かす

# mkdir -p /tmp/thin
# chmod 777 /tmp/thin
# su - redmine
$ cd /var/www/redmine
$ thin start -e production -s -4 --socket /tmp/think/redmine.sock
$ ls /tmp/thin/
redmine.0.sock  redmine.1.sock  redmine.2.sock  redmine.3.sock

unix socketなthinにnginxからアクセスできるようにする

# vi /etc/nginx/nginx.conf
// httpセクションの一番最後に下記のincludeを入れる
    include  /etc/nginx/sites-enable/*;
}
# mkdir /etc/nginx/sites-{available,enable}
// このあたりのディレクトリ構成はDebianのapacheをパクっている
# vi /etc/nginx/sites/available/redmine
server{
  listen 8080;
  server_name redmine.ya-lab.org;
  location / {
    proxy_pass http://redmine;
  }
}
upstream redmine{
  server unix:/tmp/thin/redmine.0.sock;
  server unix:/tmp/thin/redmine.1.sock;
  server unix:/tmp/thin/redmine.2.sock;
  server unix:/tmp/thin/redmine.3.sock;
}

# cd /etc/nginx/sites-enable/
# ln -s ../sites-available/redmine 001-redmine
# /usr/local/nginx/sbin/nginx -t
the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
# /usr/local/nginx/sbin/nginx -s reload

この状態でredmine.ya-lab.org:8080にブラウザからアクセスしてみると見事redmineの画面が表示された。
しかもわりと速いように感じる。

引き続きsinatraアプリケーション

$ cd /path/to/sinatra_app
$ mkdir -p log tmp/pids public
$ thin -s 4 -R config.ru --socket /tmp/thin/sinatra.sock start
$ sudo su -
# vi /etc/nginx/sites-available/sinatra
server{
  listen 8080;
  server_name sinatra.ya-lab.org;
  location / {
    proxy_pass http://sinatra;
  }
}

upstream sinatra{
  server unix:/tmp/thin/sinatra.0.sock;
  server unix:/tmp/thin/sinatra.1.sock;
  server unix:/tmp/thin/sinatra.2.sock;
  server unix:/tmp/thin/sinatra.3.sock;
}
# cd /etc/nginx/sites-enable
# ln -s ../sites-available/sinatra 002-sinatra
# /usr/local/nginx/sbin/nginx -t
the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
# /usr/local/nginx/sbin/nginx -s reload

とりあえず動かすことには成功。
initスクリプトの整備やWebdavについてはまた後日まとめる。

[ruby][twitter] TwitterStreamAPIで遊んでみた

先日アンカンファレンス関西とはこぶ忘年会で使用したTwitterStreamAPIのLTの資料とコードをアップしました。

http://github.com/yalab/shooting_twitter/
http://www.slideshare.net/yalab/twitter-2795668

サンプルアプリケーションはまだ複数人が同時にアクセスできるような状態ではないので、
コードだけとりあえず晒しておきます。

[emacs][haskell] anything.elでhrefを引けるようにした

id:rubikitchさんのRuby リファレンスマニュアルを Emacs で参照・ anything.el との連携(改訂版) - http://rubikitch.com/に移転しましたを参考にして、Haskellの日本語マニュアルhrefをanything.elで使えるようにしてみました。

.emacsに以下のように書いて

(defun href (kw)
  (interactive "shref: ")
  (with-current-buffer (get-buffer-create (concat "*href:" kw "*"))
    (when (zerop (buffer-size))
      (let ((kws (split-string kw)))
        (call-process "href" nil t t
                      (car kws)
                      (concat (cadr kws) ""))))
    (setq minibuffer-scroll-window (get-buffer-window (current-buffer) t))
    (goto-char (point-min))
    (display-buffer (current-buffer))))

(setq anything-c-source-href
      (anything-c-source-static-escript
       'anything-c-href-candidates "href"
       "/$HOME_DIR/.emacs.d/href.e" ;$HOME_DIRは自分の環境に合わせてください
       '(delayed)
       '(requires-pattern . 3)))
(setq anything-sources (cons anything-c-source-href anything-sources))

以下のスクリプトを走らせてindexを作成すれば

#!/bin/ruby                                                                     
require 'uri'
index_file = ENV['HOME'] + "/.emacs.d/href.e"
href_datadir = ENV['HREF_DATADIR']

products = []
Dir.glob(href_datadir + '/**/*') do |file|
  file.gsub!(/#{href_datadir}\//, '')
  (module_name, function) = URI.decode(file).split('/')
  products << %|(href "#{module_name} #{function}")| if function
end

File.open(index_file, 'w') do |f|
  f.write(products.join("\n"))
end

こんな感じになった。

[ruby] anything.el + refe2 のDBとindexを自動生成するようにした

id:rubikitchRuby リファレンスマニュアル(通称るりま)と ReFe2 をインストール・ Emacs で参照する・ anything.el との連携 - http://rubikitch.com/に移転しましたを参考にanything.elとrefe2を使えるようにした。
ただ気がつけば(とっくに)安定版は1.8.7だし開発版も1.9.2devなのでDBとかindexを作るあたりを自動化しておいた。


bitclusthttp://doc.loveruby.net/wiki/SubversionRepository.html:るりまSVN HEADを同じディレクトリにチェックアウトしてさらに以下のRakefileを同じディレクトリに置く。

require 'bitclust/bin/bitclust'
task :default do
  ['1.9.1', '1.8.7'].each do |version|
    BASE_DIR = File.dirname(File.expand_path(__FILE__))
    db_path = BASE_DIR + '/db-' + version.gsub(/\./, '_')
    db = BitClust::MethodDatabase.new(db_path)
    unless File.exists? db_path
      cmd = InitCommand.new
      cmd.exec db, ["version=#{version}", "encoding=euc-jp"]
    end
    cmd = UpdateCommand.new
    cmd.instance_eval do
      @root = BASE_DIR + '/doctree/refm/api/src'
    end
    cmd.exec db, []
    list = db.classes.map {|c| c.name }.sort
    db.classes.sort_by {|c| c.name }.each do |c|
      c.entries.sort_by {|m| m.id }.each do |m|
        list << m.label
      end
    end
    list.map!{|line| "# (refe2 \"#{line}\")"}
    File.open(BASE_DIR + "/refe_#{version}.e", 'w') do |f|
      f.write(list.join("\n"))
    end
  end
end

それからbitclustのコマンドラインツールをライブラリとして扱えるようにちょっとだけ手を加える

diff --git a/bin/bitclust.rb b/bin/bitclust.rb
index c21a4aa..642e160 100755
--- a/bin/bitclust.rb
+++ b/bin/bitclust.rb
@@ -504,5 +504,6 @@ class PropertyCommand < Subcommand

 end

-
-main
+if $0 == __FILE__
+  main
+end

で実行するとこんな感じ

$ ls 
Rakefile  bitclust  doctree
$ rake
// なんかいろいろ出てくる
$ ls 
Rakefile  bitclust  db-1_8_7  db-1_9_1  doctree  refe_1.8.7.e  refe_1.9.1.e

canvas要素で遊んでいる

canvas要素を使ってみたくてとりあえず画像がウロウロするスクリプトを書いてみた
実行してみる

window.onload = function(){
  var canvas = document.createElement('canvas');
  canvas.width = '1000', canvas.height = '250';
  document.getElementById('content').appendChild(canvas);
  var ctx = canvas.getContext('2d');
  var img = new Image();
  img.src = 'http://ec3.images-amazon.com/images/I/416FBSMR24L._SL500_SS75_.jpg';
  var x = 0, y = 0;
  var y_range = 20;
  var count = 0, max_count = 15;

  img.onload = function(){
    ctx.drawImage(img, 0, 0);
    var increment = {x: 5, y: 10};
    (function(){
       if(count > max_count){
         return;
       }
       if(y < -y_range || y_range < y){
         increment['y'] = (-increment['y']);
       }
       ['x', 'y'].map(function(k){ eval(k + " += increment['" + k + "']"); });
       ctx.clearRect(0, 0, img.width, img.height);
       ctx.translate(x, y);
       ctx.drawImage(img, 0, 0);
       count += 1;
       setTimeout(arguments.callee, 100);
     })();
  };
};

firefox 3.5.5でしか検証してないのでその他のブラウザで動くかは不明。
動いたブラウザをコメントで報告していただけるとうれしいです。

githubに間違えたタグをつけちゃったので削除

満を持してgitでタグをつけてgithubにpushしたのだけれど実は誤ってブランチのほうにタグをつけてた!!という時の対処方

$ git tag -d version-0.3
$ git push git@github.com:yalab/shichimi.git :version-0.3

git pushの第一引数には該当のプロジェクトのYour Clone URL(ログイン時のみに表示される)を指定する