简体   繁体   中英

deploying rails 3.1 apps to Amazon Ec2

I've googled a lot, but still can't find a way to deploy my site to ec2. can anyone explain more about ec2 to me? I'm using Ubuntu11.04 for developement. I would like to use passenger + nginx to deploy, thanks

I'm using capistrano to deploy a Rails 3.1 app to an EC2 micro instance. I've also setup Ruby on my EC2 using rvm. I've been using thin, but this weekend I switched to Unicorn to test it out. I'll share what I'm doing, and maybe you can figure out how to change it accordingly to use Passenger (or someone else can comment on that). I'd also welcome any comments if people have some suggestions for any of this, as I'm in no way an expert. :)

I would like to point out that I still have a problem during the "rake assets:precompile" stage of my deploy. It gets to the third stage, assets:precompile:nodigest, and fails with no exit code. I think it might be running out of memory. It's not rolling back the deploy. If I run "rake assets:precompile:nodigest" then it finishes just find and everything is good to go. This doesn't happen when I deploy to my VM for testing, only when I deploy to my EC2 micro instance (which makes me think it could be an OOM error since EC2 micro is tiny and I've seen OOM errors in the past).

Despite that, here's what I've got. Maybe it'll help you get up and running.

Some relevant things from my Gemfile:

gem 'rails', '3.1.1'

group :assets do
  gem 'jquery-rails',
  gem 'sass-rails', "~> 3.1.4"
  gem 'coffee-rails', "~> 3.1.1"
  gem 'uglifier', ">= 1.0.3"
  gem 'compass', :git => 'git://github.com/chriseppstein/compass.git', :branch => 'master'
end

group :production do
  gem 'therubyracer'
end

gem 'unicorn'

Capfile:

load 'deploy' if respond_to?(:namespace)
load 'deploy/assets'

Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }

load 'config/deploy'

config/deploy.rb:

$:.unshift(File.expand_path('./lib', ENV['rvm_path']))

require "rvm/capistrano"
require "bundler/capistrano"

role :app, "your-ec2-domain"
role :db,  "your-ec2-domain", :primary => true
set :user, "your-login-username"

set :application, "your-app-name"

set :scm, :git
set :repository, "."
set :branch, "deploy"   # or whatever git branch you deploy from

set :deploy_via, :copy
set :deploy_to, "/home/#{user}/rails/#{application}"
set :use_sudo, false

set :rails_env, "production"

set :rvm_ruby_string, "ruby-1.9.2-p290"
set :rvm_type, :user

set :unicorn_pid do
  "#{shared_path}/pids/unicorn.pid"
end

before "deploy:assets:precompile", "bundle:install"

namespace :deploy do
  task :start do
    top.unicorn.start
  end

  task :stop do
    top.unicorn.stop
  end

  task :restart do
    top.unicorn.reload
  end
end

namespace :unicorn do
  desc "start unicorn server"
  task :start, :roles => :app do
    run "cd #{current_path} && bundle exec unicorn -E #{rails_env} -D -P #{unicorn_pid}"
  end

  desc "stop unicorn server"
  task :stop do
    run "kill -s QUIT `cat #{unicorn_pid}`"
  end

  desc "restart unicorn"
  task :restart do
    top.unicorn.stop
    top.unicorn.start
  end

  desc "reload unicorn (gracefully restart workers)"
  task :reload do
    run "kill -s USR2 `cat #{unicorn_pid}`"
  end

  desc "reconfigure unicorn (reload config and gracefully restart workers)"
  task :reconfigure, :roles => :app do
    run "kill -s HUP `cat #{unicorn_pid}`"
  end
end

My nginx.conf:

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events {
  worker_connections 768;
  accept_mutex off;
}

http {
  include /etc/nginx/mime.types;

  access_log /var/log/nginx/access.log combined;
  error_log /var/log/nginx/error.log;

  sendfile on;
  tcp_nopush on;
  keepalive_timeout 65;
  tcp_nodelay off;

  gzip on;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

Then under /etc/nginx/sites-available you should create a file for your site. We'll call it foobar.conf:

upstream rails {
  server unix:/tmp/.sock fail_timeout=0;
  server 127.0.0.1:8080 fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name foobar.com

  access_log /var/log/nginx/rails.access.log main;

  # foobar is your project. current is a symlink setup by capistrano
  root /home/username/rails/foobar/current/public;

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

  location ~ ^/assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.html
  location = /500.html
    root /home/username/rails/foobar/current/public;
  }
}

Then you should create a symlink from the file you just created in /etc/nginx/sites-available and make the symlink point to a /etc/nginx/sites-enabled/foobar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM