3

我GOOGLE了很多,但仍然无法找到一种方法将我的网站部署到EC2。 任何人都可以向我解释更多关于ec2的信息吗?我使用Ubuntu11.04进行开发。 我想使用乘客+ nginx部署,谢谢部署rails 3.1应用程序到亚马逊的Ec2

+0

http://www.heroku.com/部署到ec2,但使用heroku的中间件的溢价。他们确实有免费的服务和很多有用的服务 – Hortinstein

+0

因为我正在尝试使用AWS的免费服务,所以暂时还不是我的选择......难道要部署rails到AWS吗?感谢 –

+0

部署到EC2就像部署到任何其他服务器一样。 – NARKOZ

回答

7

我正在使用capistrano将Rails 3.1应用程序部署到EC2微型实例。我还使用rvm在我的EC2上设置了Ruby。我一直在使用瘦身,但是这个周末我转而使用Unicorn来测试它。我会分享我在做什么,也许你可以想出如何改变它相应地使用乘客(或其他人可以评论)。如果有人对此有任何建议,我也欢迎任何意见,因为我绝不是专家。 :)

我想指出,我在部署的“rake资产:预编译”阶段仍然存在问题。它进入第三阶段,资产:预编译:nodigest,并失败,没有退出代码。我认为它可能会用完内存。这不会回滚部署。如果我运行“rake资产:预编译:nodigest”,那么它完成后找到,一切都很好。当我部署到我的虚拟机进行测试时,这种情况不会发生,只有当我部署到我的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' 

Capfile:

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

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

load 'config/deploy' 

配置/ 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的/网站可用创建该文件一个符号链接,使符号链接指向一个的/ etc/nginx的/网站-enabled/foobar