2012年3月13日火曜日

アプリケーションをサーバに上げる手順

Railsアプリケーションを完成させたのでそれを上げるまでの一連の流れをメモ!

ドメインを作成

レポジトリを作る
$ mkdir hoge
$ hg init
$ ls -a
.  .. .hg
「hg init」でレポジトリが出来るので確認してコミットする
$ hg push ssh://xxx@hg.xxxx.jp/directory/directory/main
.hg/hgrcを編集する
[paths]
default = ssh://xxx@hg.xxxx.jp/directory/directory/main
とすれば以降プッシュが楽になる。

デプロイ先を決める

deploy.rbを書く
require 'bundler/capistrano'
load 'deploy/assets'

set :application, "application名"
set :scm, :mercurial
set :use_sudo, false
set :keep_releases, 3

desc "説明文"
task :production do
  set :deploy_to, "/var/rails/#{application}"
  set :repository, "ssh://xxx@hg.xxxx.jp/directory/directory/main"
  set :user, 'app'
  set :runner, "app"
  set :staging_server, false
  set :use_passenger, true
  set :rails_env, 'production'
  set :resettable, false  role :app, "サーバー"
  role :web, "サーバー"
  role :db,  "サーバー", :primary => true
  set :default_environment, {
    'PATH' => "/usr/local/bin:$PATH",
  }
end

desc "説明文"
after "deploy:update_code", :roles => :app do
  run "cp #{shared_path}/config/database.yml #{release_path}/config/"
end

namespace :deploy do
  # deploy:restart の上書き
  desc "説明文"
  task :restart do
    run "mkdir -p #{shared_path}/tmp"
    run "touch #{shared_path}/tmp/restart.txt"
  end
end
↓ デプロイ先にフォルダを作る
$ cap production deploy:setup
database.ymlを作る
通常のサーバー上であれば
/var/rails/application名/shared/config/database.ymlに書く
production:
  adapter: mysql2
  encoding: 文字コード
  database: application名
  pool: 5
  username: ユーザー名
  password: パスワード
  timeout: 5000

デプロイする
$ cap production deploy

サーバー上で手動でマイグレーションする(deploy.rbに書かない場合)
サーバー上で
$ export RAILS_ENV=production
$ bundle exec rake db:migrate

サーバー上で手動でrake db:assets:precompileをする(deploy.rbに書かない場合)
$ export RAILS_ENV=production
$ bundle exec rake db:assets:precompile

apacheのconfを書く
標準であればこんな感じ?
<VirtualHost *:80>
  ServerName ドメイン
  ErrorLog "/var/log/httpd/ドメイン.error_log"
  CustomLog "/var/log/httpd/ドメイン.access_log"
  DocumentRoot /var/rails/linkage/current/public
  RailsEnv production
  RackEnv production
  PassengerRestartDir /var/rails/linkage/shared/tmp
</VirtualHost>

apacheのconfが間違っていないか確認
$ sudo ./apachectl configtest
apacheの再起動
$ sudo ./apachectl graceful

0 件のコメント:

コメントを投稿