環境
- Ruby 2.1
- Rails 4.2.0
- MySQL
- nginx and Unicorn
参考
- Deploying Ruby on Rails Applications to AWS OpsWorks
- Chef デプロイフックの使用 - AWS OpsWorks
- Rails 4.2 写経用サンプルアプリ
サンプル
Railsアプリの準備
opsworksブランチの作成
RailsアプリにOpsWorks用のブランチを作成します。
例:私はRails 4.2 写経用サンプルアプリをクローンし、OpsWorks用のブランチを作成しました。
git checkout -b opsworks
Gemの追加
GemfileにMySQL接続用のライブラリを追加します。
gem 'mysql2', '~> 0.3.18'
therubyracerとunicorn行のコメントを外し、Gemを有効にします。
gem 'therubyracer', platforms: :ruby
gem 'unicorn'
アセットのプリコンパイル
deploy/before_symlink.rbファイルを追加し、シンボリックリンクステージの前にプリコンパイルを実行します。
mkdir deploy
vi deploy/before_symlink.rb
deploy/before_symlink.rb
run "cd #{release_path} && RAILS_ENV=production bundle exec rake assets:precompile"
GitHubリポジトリに反映
bundle install
git add -A
git commit -m OpsWorks
git push origin opsworks
OpsWorksの設定
Stack
デフォルト値のままで、Nameだけ入力し、「Add Stack」ボタンを押下します。
- Amazon Linux 2014.09
Layers
Rails App Server
- Ruby version
2.1
- Rails stack
nginx and Unicorn
- RubyGems version
2.2.2
- Bundler version
1.7.4
MySQL
- デフォルト設定
Instances
Rails App Server, MySQL
- t2.small などに変更します。
Apps
Application Source
- Repository URL
https://github.com/usutani/store_rails420.git
- Branch/Revision
opsworks
Environment Variables
- SECRET_KEY_BASE Protected value
(参考)Mac:bundle exec rake secret | pbcopy
でクリップボードにコピーします。
Deployments
- Migrate database YES
- Advanced >> Custom Chef JSON
アプリ名store_rails420
を置き換えてください。
{
"deploy":
{
"store_rails420":
{
"database":
{
"adapter": "mysql2"
}
}
}
}