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