Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Rails in da Cloud
Почему?• Сатанисты хотят в облака. WTF?• Это модно.• Это удобно, выгодно, отказоустойчиво и дёшево.                       ...
ЦельЗапуск rails в Amazon EC2                            3
План•   Запуск инстанса•   Подключение к нему•   Настройка окружения•   Деплой•   ??????•   PROFIT!                       ...
5
6
7
8
9
10
11
12
13
14
15
16
17
Connect!                      ~ $   mv Downloads/igas.pem .ssh/igas.pem                      ~ $   cd .ssh                ...
nginx upstream unicorn {   server unix:/tmp/testaws.socket fail_timeout=0; } server {   listen 80 default;   server_name 1...
Capfile load deploy load deploy/assets load config/deploy # remove this line to skip loading any of the default tasks      ...
Gemfile source https://rubygems.org gem rails, 3.2.9 gem pg gem libv8, ~> 3.11.8.3 group :assets do   gem sass-rails,   ~> ...
config/deploy.rb require bundler/capistrano require "rvm/capistrano" set :rvm_ruby_string, 1.9.3 set :application,     test...
config/unicorn.rb env = ENV["RAILS_ENV"] || "development" user = "ubuntu" app = "testaws" root_path = "/home/#{user}/www/#{...
unicorn #! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/rvm/gems/ree-1.8.7-...
unicorn sudo chmod +x /etc/init.d/unicorn sudo /usr/sbin/update-rc.d -f unicorn defaults /etc/init.d/unicorn start        ...
Вопросы?           Igas, Ruby Ninja
Upcoming SlideShare
Loading in …5
×

EC2

482 views

Published on

  • Be the first to comment

EC2

  1. 1. Rails in da Cloud
  2. 2. Почему?• Сатанисты хотят в облака. WTF?• Это модно.• Это удобно, выгодно, отказоустойчиво и дёшево. 2
  3. 3. ЦельЗапуск rails в Amazon EC2 3
  4. 4. План• Запуск инстанса• Подключение к нему• Настройка окружения• Деплой• ??????• PROFIT! 4
  5. 5. 5
  6. 6. 6
  7. 7. 7
  8. 8. 8
  9. 9. 9
  10. 10. 10
  11. 11. 11
  12. 12. 12
  13. 13. 13
  14. 14. 14
  15. 15. 15
  16. 16. 16
  17. 17. 17
  18. 18. Connect! ~ $ mv Downloads/igas.pem .ssh/igas.pem ~ $ cd .ssh ~/.ssh $ chmod 400 igas.pem ~/.ssh $ ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.comubuntu@ip-10-203-42-73:~$ exit ~ $ cat ~/.ssh/id_rsa.pub| pbcopy ~/.ssh $ ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.comubuntu@ip-10-203-42-73:~$ echo ssh-rsa AAAAB3NEww== igasgeek@me.com >> .ssh/authorized_keysubuntu@ip-10-203-42-73:~$ sudo apt-get updateubuntu@ip-10-203-42-73:~$ sudo apt-get -y upgradeubuntu@ip-10-203-42-73:~$ sudo apt-get -y install git-core build-essential zlib1g-dev libssl-dev libreadline-gplv2-dev libyaml-dev python-software-properties libffi-dev nginx-light postgresql-server-dev-9.1ubuntu@ip-10-203-42-73:~$ sudo bash -c echo "RAILS_ENV=production" >> /etc/environmentubuntu@ip-10-203-42-73:~$ echo "gem: --no-ri --no-rdoc" > ~/.gemrcubuntu@ip-10-203-42-73:~$ curl -L https://get.rvm.io | bash -s stable --rubyubuntu@ip-10-203-42-73:~$ source $HOME/.rvm/scripts/rvmubuntu@ip-10-203-42-73:~$ sudo rm /etc/nginx/sites-enabled/defaultubuntu@ip-10-203-42-73:~$ sudo vi /etc/nginx/sites-enabled/testubuntu@ip-10-203-42-73:~$ sudo service nginx start 18
  19. 19. nginx upstream unicorn { server unix:/tmp/testaws.socket fail_timeout=0; } server { listen 80 default; server_name 184.72.181.253; root /home/ubuntu/www/testaws/current/public; access_log /var/log/nginx/testaws.log; rewrite_log on; location / { proxy_pass http://unicorn; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ ^/assets/ { expires max; break; } } 19
  20. 20. Capfile load deploy load deploy/assets load config/deploy # remove this line to skip loading any of the default tasks 20
  21. 21. Gemfile source https://rubygems.org gem rails, 3.2.9 gem pg gem libv8, ~> 3.11.8.3 group :assets do gem sass-rails, ~> 3.2.3 gem coffee-rails, ~> 3.2.1 gem uglifier, >= 1.0.3 gem therubyracer, ~> 0.11.0beta5 end gem jquery-rails gem rvm-capistrano gem unicorn gem capistrano 21
  22. 22. config/deploy.rb require bundler/capistrano require "rvm/capistrano" set :rvm_ruby_string, 1.9.3 set :application, testaws set :user, ubuntu set :scm, git set :repository, git@github.com:igas/testaws.git set :branch, master set :deploy_to, "/home/#{user}/www/#{application}" set :use_sudo, false set :domain, 184.72.181.253 default_run_options[:pty] = true ssh_options[:forward_agent] = true server domain, :web, :app, :db, primary: true namespace :deploy do task :start, except: { no_release: true } do run "cd #{current_path} && bundle exec unicorn -c config/unicorn.rb -D" end task :stop, except: { no_release: true } do run "cd #{current_path} && kill `cat tmp/pids/unicorn.#{application}.pid`" end end 22
  23. 23. config/unicorn.rb env = ENV["RAILS_ENV"] || "development" user = "ubuntu" app = "testaws" root_path = "/home/#{user}/www/#{app}" current_path = "#{root_path}/current" shared_path = "#{root_path}/shared" log_path = "#{shared_path}/log" worker_processes 2 preload_app true timeout 240 listen "/tmp/#{app}.socket", backlog: 64 pid "#{shared_path}/pids/unicorn.#{app}.pid" working_directory current_path stderr_path "#{shared_path}/log/unicorn.stderr.log" stdout_path "#{shared_path}/log/unicorn.stdout.log" before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end 23
  24. 24. unicorn #! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/rvm/gems/ree-1.8.7-2010.02/bin/unicorn DAEMON_OPTS="-c /web_apps/example_rack_app/unicorn.rb -E production -D" NAME=unicorn DESC="Unicorn app for example_rack_app" PID=/web_apps/example_rack_app/pids/unicorn.pid case "$1" in start) ! echo -n "Starting $DESC: " ! $DAEMON $DAEMON_OPTS ! echo "$NAME." ! ;; stop) ! echo -n "Stopping $DESC: " kill -QUIT `cat $PID` ! echo "$NAME." ! ;; restart) ! echo -n "Restarting $DESC: " kill -QUIT `cat $PID` ! sleep 1 ! $DAEMON $DAEMON_OPTS ! echo "$NAME." 24
  25. 25. unicorn sudo chmod +x /etc/init.d/unicorn sudo /usr/sbin/update-rc.d -f unicorn defaults /etc/init.d/unicorn start 25
  26. 26. Вопросы? Igas, Ruby Ninja

×