amazon-ec2 - 将 Rails 3.1 应用程序部署到 Amazon Ec2

标签 amazon-ec2 nginx ruby-on-rails-3.1 amazon-web-services web-deployment

我在谷歌上搜索了很多,但仍然找不到将我的站点部署到 ec2 的方法。 任何人都可以向我解释更多关于 ec2 的信息吗?我正在使用 Ubuntu11.04 进行开发。 我想使用passenger + nginx来部署,谢谢

最佳答案

我正在使用 capistrano 将 Rails 3.1 应用程序部署到 EC2 微型实例。我还使用 rvm 在我的 EC2 上设置了 Ruby。我一直在使用 thin,但这个周末我改用 Unicorn 来测试一下。我将分享我正在做的事情,也许您可​​以弄清楚如何相应地更改它以使用 Passenger(或者其他人可以对此发表评论)。如果人们对此有任何建议,我也欢迎任何评论,因为我绝不是专家。 :)

我想指出,我在部署的“rake assets:precompile”阶段仍然遇到问题。它进入第三阶段,assets:precompile:nodigest,并且失败并且没有退出代码。我认为它可能内存不足。它不会回滚部署。如果我运行“rake assets:precompile:nodigest”,那么它就会完成查找,一切都很好。当我部署到我的 VM 进行测试时不会发生这种情况,只有当我部署到我的 EC2 微型实例时才会发生(这让我认为这可能是一个 OOM 错误,因为 EC2 微型实例很小而且我过去看到过 OOM 错误) .

尽管如此,这就是我所拥有的。也许它会帮助您启动并运行。

我的 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'

标题:

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

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

load '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

我的 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/*;
}

然后在/etc/nginx/sites-available 下你应该为你的站点创建一个文件。我们称之为 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;
  }
}

然后您应该从刚刚在/etc/nginx/sites-available 中创建的文件创建一个符号链接(symbolic link),并使符号链接(symbolic link)指向/etc/nginx/sites-enabled/foobar

关于amazon-ec2 - 将 Rails 3.1 应用程序部署到 Amazon Ec2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7871760/

相关文章:

ssh - Amazon EC2 Root 登录

amazon-web-services - AWS 针对任意数量的 EC2 实例在 CPU 上自动缩放

node.js - 您是否设法使 Heroku 上的 Node nginx 代理设置正常工作?

linux - 以非 root 用户身份运行 Nginx

regex - nginx不区分大小写的url重定向

ruby-on-rails - Heroku 不更新 Assets 修改

amazon-ec2 - 是否可以更改 AWS 控制台中显示的 EC2 实例 key 名称?

ubuntu - EC2 实例、装载卷 DOS/MBR 引导扇区

ruby-on-rails - ActionView::Template::Error,多个未知的正则表达式选项问题

ruby-on-rails-3.1 - Rails.3.1.1 : config. assets.digest = true 导致编译后的 css 被用作 Sprite 的文件名