家計簿さな太郎のインストールメモ(nginx+サブディレクトリ)

Web家計簿を自宅サーバで運用するための作業メモ。サービスとしても利用できるが、OSSとして公開されているので自分のサーバにインストールし、運用することも可能。

基本的な手順等は下記を参考にさせていただいた。

Ruby on Rails で動く家計簿の導入メモ

適当なディレクトリで以下を実行。

$ git clone git://github.com/kaznum/sanataro.git
$ sudo chowon -R nginx:nginx sanataro
$ cd sanataro

事前にdatabaseを作成しておき、database名、ユーザ名、パスワードを設定しておく。 以下、mysql2を利用する場合の一例。

sanataro/config/database.yml
 production:
   adapter: mysql2
   database: sanataro
   username: sanataro
   password: xxxxxxxx
   encoding: utf8
   socket: /var/lib/mysql/mysql.sock

さきほど作ったdataベース名、ユーザ名、パスワードを指定。これは、あくまでもDB接続用のパスワードのため、利用時のログインパスワードとは別である。利用時のログインパスワードは、このDB上に保管される。socketのファイルパスは、mysqlの設定とあわせること。

msqlのソケットファイルのパス確認
$ sudo ps auxf | grep mysql
root 1274 0.0 0.0 11468 936 ? S 2017 0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 1493 0.2 11.8 2165900 243836 ? Sl 2017 153:54 \_ /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock

--socketの後のパスがmysqlが使っているsocketのパスとなる。

sanataro/config.ru
# This file is used by Rack-based servers to start the application.

RAILS_RELATIVE_URL_ROOT="/sanataro"

require ::File.expand_path('../config/environment',  __FILE__)
run Sanataro::Application

if RAILS_RELATIVE_URL_ROOT then
        map RAILS_RELATIVE_URL_ROOT do
                run Rails.application
        end
else
        run Rails.application
end

サブディレクトリで運用する為には、RAILS_RELATIVE_URL_ROOTが重要。

sanararo/Gemfile
group :production do
  gem 'unicorn'
end

productionにunicornを追加し、bundle install時に追加するよう変更。

sanataro/confing/unicorn.rb
# Rails_root_path
rails_root = "/www/html/sanataro"

worker_processes 2
working_directory rails_root

# Unicorn Socket
listen "#{rails_root}/tmp/unicorn.sock"

# Unicorn PID file location
pid "#{rails_root}/tmp/unicorn.pid"

# Path to logs
stderr_path "#{rails_root}/log/unicorn_error.log"
stdout_path "#{rails_root}/log/unicorn.log"

# Time-out
timeout 300

unicorn用設定ファイルを作成する。

$ gem install bundler
$ bundle install --path vendor/bundle --without test development

利用するだけであれば、productionだけのgemでよい。 特にtestのみで使うgemのインストールでいくつかハマりポイントがあるので、回避。

$ bundle exec rake db:migrate RAILS_ENV=production

必要なテーブルの作成等。

/etc/nginx/conf.d/ssl.conf
#Sanataro(Rails)

upstream unicorn {
  server unix:/www/html/sanataro/tmp/development_unicorn.sock fail_timeout=0;
}

location /sanataro {
    alias /www/html/sanataro;
    try_files $uri/index.html $uri.html $uri @unicorn;
  }

  location @unicorn {
    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://unicorn;
  }

nginxで動かす方の設定ファイル。nginxのserverディレクティブの中に書く。

$ sudo service nginx restasrt

nginxの再起動。

さな太郎(Rasils App)の起動
bundle exec unicorn_rails -c config/unicorn.rb -E production -D --path /sanataro

ここでも --path設定。

他にやること

・自分に不要な機能をテンプレート(/app)のファイル、または該当部分をテンプレートから削除する等して無効化(ユーザー追加等)しておく。

・別の家計簿アプリからデータのインポート(特にインポート機能はないので、適当にコンバートかけておく。集計は別テーブルがあるので、そこもコンバートしておく。)、詳細は割愛。