debian vpsにnginx+sqlite3+unicorn+Redmine

友人とシェアするRedmine鯖を立てる事になったので、その記録です。 ちなみに利用したvpsは3月末に契約した米国のvps。なんとメモリ512MB保証、最大1GBで通信量は1TBまでという内容で年$20という価格。通信量の制限を考えると妥当な価格ではありますが、ここまで安いvpsは国内では見つかりませんよね。さすがIT先進国。

内容は殆ど下記のページで記述されていることと変わりありません。右も左もわからない自分はかなり参考にさせて頂きました!

さくらVPSで nginx + MySQL + Unicorn + Redmine の運用 - コードを舐める日々

Mysqlの代わりにsqlite3を使ってるあたりが違ったり、一部不足していたパッケージのインストールが必要だったので 上記ページに対する追加情報として記録させて頂きました。

構築作業

必要なパッケージのインストール
コンパイラやライブラリを予めインストールしておきます。

下記のライブラリが必要でした。

# apt-get -y install libssl-dev libreadline5-dev zlib1g-dev libyaml-dev
# apt-get -y install libreadline6-dev libsqlite3-dev sqlite3 libxslt1-dev 
# apt-get -y autoconf libgdbm-dev automake bison libffi-dev

以下は必要に合わせて取捨選択して下さい。

# apt-get -y install git git-core make g++
# apt-get -y install build-essential curl
# apt-get -y nginx

rvmのインストール

# \curl -L https://get.rvm.io | bash -s stable
# source /etc/profile.d/rvm.sh
# rvm install 1.9.3

ライブラリの追加インストール

# apt-get install -y imagemagick libmagickcore-dev libmagickwand-dev

bundlerのインストール

# apt-get -y install rubygems
# gem install bundler --no-rdoc --no-ri

redmineインストール

# git clone https://github.com/redmine/redmine.git  
# cd redmine
# gem install bundler
# bundle install --path vendor/bundle --without development test rmagick postgresql sqlite

redmineのDB接続情報を修正 sqlite3を使うように修正します。

# cp config/database.yaml.example confing/database.yaml
# vim config/database.yml

修正したconfing/database.yamlの中身は以下のとおり。

# Default setup is given for MySQL with ruby1.9. If you're running Redmine
# with MySQL and ruby1.8, replace the adapter name with `mysql`.
# Examples for PostgreSQL, SQLite3 and SQL Server can be found at the end.
# Line indentation must be 2 spaces (no tabs).

# production:
#   adapter: mysql2
#   database: redmine
#   host: localhost
#   username: root
#   password: ""
#   encoding: utf8
#
# development:
#   adapter: mysql2
#   database: redmine_development
#   host: localhost
#   username: root
#   password: ""
#   encoding: utf8
#
# # Warning: The database defined as "test" will be erased and
# # re-generated from your development database when you run "rake".
# # Do not set this db to the same as development or production.
# test:
#   adapter: mysql2
#   database: redmine_test
#   host: localhost
#   username: root
#   password: ""
#   encoding: utf8

# PostgreSQL configuration example
#production:
#  adapter: postgresql
#  database: redmine
#  host: localhost
#  username: postgres
#  password: "postgres"

# SQLite3 configuration example
production:
  adapter: sqlite3
  database: db/redmine.sqlite3

# SQL Server configuration example
#production:
#  adapter: sqlserver
#  database: redmine
#  host: localhost
#  username: jenkins
#  password: jenkins

DBを作成

# cd db
# sqlite3 redmine.sqlite3
# cd ../

webrick簡易サーバーで動作確認
下記コマンド実行後ポート3000のアドレスにアクセスして動作しているか確認。

# ruby script/rails server webrick -e production

unicornのインストールと設定

unicornRubyRails サーバで Ruby 版のHTTPサーバ + daemontools のようなもので、落ちても自動再起動してくれるものです。

via さくらVPSで nginx + MySQL + Unicorn + Redmine の運用 - コードを舐める日々

# gem install unicorn

config/unicorn.rbを作成。

#vim config/unicorn.rb

中身は以下のとおり。

worker_processes 2
#working_directory /home/www/rails/charag

listen File.expand_path("tmp/unicorn.sock", ENV['RAILS_ROOT'])
pid File.expand_path("tmp/unicorn.pid", ENV['RAILS_ROOT'])

timeout 60

preload_app true # ダウンタイムをなくす

stdout_path File.expand_path("log/unicorn.stdout.log", ENV['RAILS_ROOT'])
stderr_path File.expand_path("log/unicorn.stderr.log", ENV['RAILS_ROOT'])

GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!

  old_pid = "#{server.config[:pid]}.oldbin"
    if old_pid != server.pid
      begin
        sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
        Process.kill(sig, File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
      end 
    end 

    sleep 1
  end 

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end

redmineunicornを利用して起動。

# bundle exec unicorn_rails -c config/unicorn.rb -E production -D -p 5001

nginx のプロキシ、バーチャルホスト設定、 nginx の再起動
以下、さくらVPSで nginx + MySQL + Unicorn + Redmine の運用 - コードを舐める日々と同じ内容です。

# vim /etc/nginx/nginx.conf

「 http { 」の下に下記のコードを入れます。

upstream redmine{
  server 127.0.0.1:5001;
}

virtual.confの編集。

# vim /etc/nginx/conf.d/virtual.conf

中身は以下のとおり。

server {
  listen 80;
  server_name redmine.example.com;

  access_log /var/log/nginx_redmine_access.log;
  error_log /var/log/nginx_redmine_error.log;

  proxy_connect_timeout 60;
  proxy_read_timeout    60;
  proxy_send_timeout    60;

  auth_basic "Secret Area";
  auth_basic_user_file "/home/www/rails/redmine/.htpasswd";

  location / {

    root /home/www/rails/redmine/public;
    if (-f $request_filename){
      break;
    }

    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_pass http://redmine;
    proxy_redirect off;
  }
}

nginxの再起動。

#service nginx restart

virtual.conf で指定した server_name にアクセスして Redmine が開けば成功!お疲れ様!

最後に

すでに2週間ほど運用していますが、今のところ問題なくさくさく動いています。 次はもう一歩進んでredmine+gitに挑戦したいと考えています。 今日はここまで〜。