2012-06-22

Rails3.2.6アプリケーションにCoolなデザインを適用してApache+Unicornで起動!

Railsアプリケーションに簡単にCoolなデザインを適用できるWeb App Themeなのですが、Rails3.2.6だとassetsに対応したバージョンが入ってくれず、苦労したのでメモしておきます。
まずは、アプリケーション作成
cd /var/www
rails new web-app-theme -d mysql
そして、説明通り
echo "gem 'web-app-theme', '~> 0.8.0'" >> Gemfile
bundle install
とやってみたところ
Bundler could not find compatible versions for gem "rails":
  In Gemfile:
    web-app-theme (~> 0.8.0) ruby depends on
      rails (~> 3.1.0.rc6) ruby

    rails (3.2.6)
な感じで怒られてしまい、Rails3.2.6は3.1.0.rc6より新しいと認識してくれないようです(ノ゚⊿゚)ノ
仕方なくGemfileから, '~> 0.8.0'の記述を削除してbundle installすると0.7.0が入ってくれました。
とりあえず、サンプルでscaffold実行
rails g scaffold task name:string memo:string  

>>/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
        from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs.rb:5:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs.rb:4:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.1.4/lib/bundler/runtime.rb:68:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.1.4/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.1.4/lib/bundler/runtime.rb:66:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.1.4/lib/bundler/runtime.rb:66:in `block in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.1.4/lib/bundler/runtime.rb:55:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.1.4/lib/bundler/runtime.rb:55:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.1.4/lib/bundler.rb:119:in `require'
        from /var/www/web-app-theme/config/application.rb:7:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:24:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:24:in `'
        from script/rails:6:in `require'
        from script/rails:6:in `
'
何か足りないようです(´・ω・`)
てな訳で足りないものをGemfileに追記してbundle install再実行
cat <<"EOF" >> Gemfile
gem 'execjs'
gem 'therubyracer'
EOF

bundle install
scaffoldリトライ!
rails g scaffold task name:string memo:string
今度はうまく行ってくれましたヾ(*・ω・)シ
あとは、静的ファイルの配信用としてApacheをインストールしておきます
yum -y install httpd
そして、バーチャルホストの設定ファイルをつくっておきます
cat <<"EOF" > /etc/httpd/conf.d/rails.conf

  ServerName www.exapmle.com
  DocumentRoot /var/www/web-app-theme/public
  CustomLog logs/access.log combined
  ErrorLog  logs/error.log

  ProxyRequests Off
  
    Order deny,allow
    Allow from all
  

  ProxyPass /images !
  ProxyPass /assets !
  ProxyPass /stylesheets !
  ProxyPass /javascripts !
  ProxyPass /robots.txt !
  ProxyPass /favicon.ico !

  ProxyPass / http://localhost:3000/
  ProxyPassReverse / http://localhost:3000/

EOF
あとは、Apacheを実行してApache側の設定はおしまい!
service httpd start
Rails側の処理をunicornでさせるためにインストールしておきます
Gefileに予め記述があるのでこれをコメントアウトしてbundle install
sed -i "s/# gem 'unicorn'/gem 'unicorn'/" Gemfile
bundle install
で、unicorn用にはproduction用の設定を作成しておきます
cat <<"EOF" > config/production.conf
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# See also http://unicorn.bogomips.org/examples/unicorn.conf.rb for
rails_env = 'production'
worker_processes 5
working_directory '/var/www/web-app-theme'
port = 3000
listen port, :tcp_nopush => true
timeout 30
pid 'tmp/pids/unicorn.pid'
preload_app  true
stderr_path 'log/unicorn_err.log'
stdout_path 'log/unicorn.log'

before_fork do |server, worker|
  # この設定はpreload_app trueの場合に必須
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  # この設定はpreload_app trueの場合に必須
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end
EOF
あとは、DBの設定とassetsのprecompile
rake db:create RAILS_ENV=production
rake db:migrate RAILS_ENV=production
rake assets:precompile RAILS_ENV=production
あとはunicornにこの設定ファイルを読み込ませてデーモンモードで起動
unicorn_rails -D -c /var/www/web-app-theme/config/production.conf -E production
あとはブラウザでアクセスしてみれば起動してますねヾ(*・ω・)シ

お次は、ようやくWeb App Themeを使ってデザイン適用です
rails g web_app_theme:theme --app-name="web-app-theme" --theme=amro
そして、assetsのrecompileをしてunicornの再起動
rake assets:precompile RAILS_ENV=production
kill `cat /var/www/web-app-theme/tmp/pids/unicorn.pid`
unicorn_rails -D -c /var/www/web-app-theme/config/production.conf -E production
いざブラウザでアクセスしてみるとエラーが・・・
/var/www/web-app-theme/log/production.logにはこんなのがでてました
ActionView::Template::Error (web-app-theme/base.css isn't precompiled):
    2: 
    3: 
    4:   web-app-theme
    5:   <%= stylesheet_link_tag "web-app-theme/base", "web-app-theme/themes/amro/style", "web-app-theme/override", :cache => true %>
    6:   <%= javascript_include_tag :defaults, :cache => true %>
    7:   <%= csrf_meta_tag %>
    8: 
  app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb__2208883353270545151_259086380'
  app/controllers/tasks_controller.rb:7:in `index'
これだとassetsに対応していないので書き換え
sed -i "s/<%= stylesheet_link_tag.*web-app-theme.*>/<%= stylesheet_link_tag \"application\" %>/" /var/www/web-app-theme/app/views/layouts/application.html.erb
sed -i "s/<%= javascript_include_tag.*defaults.*>/<%= javascript_include_tag \"application\" %>/" /var/www/web-app-theme/app/views/layouts/application.html.erb
cssの方も書き換え
cat <<"EOF" >> /var/www/web-app-theme/app/assets/stylesheets/application.css
//=web-app-theme/base
//=web-app-theme/themes/Amro/style.css
//=web-app-theme/override.css
EOF
そして、一式をassetsに移動
mv /var/www/web-app-theme/public/stylesheets/web-app-theme/ /var/www/web-app-theme/app/assets/stylesheets/
あとは、もう一度compileしてunicorn再起動
rake assets:precompile RAILS_ENV=production
kill `cat /var/www/web-app-theme/tmp/pids/unicorn.pid`
unicorn_rails -D -c /var/www/web-app-theme/config/production.conf -E production
これで、動きましたヾ(*・ω・)シ

0 件のコメント:

コメントを投稿