deploying rails 3.1 apps to Amazon Ec2 - 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

Related

How to run Sinatra App in Passenger with Nginx?

I'm trying to run a very simple Sinatra app using my existing Nginx & Passenger setup. I'm familiar with running Rails on Passenger but this is my first time setting up Sinatra.
I've installed Sinatra through bundler and RVM. Take a look at my configuration and tell me what I'm doing wrong.
Nginx conf:
server {
listen 80;
server_name demo.my-example.com;
root /home/user/demo.my-example.com/sinatra;
passenger_ruby /usr/local/rvm/wrappers/ruby-2.3.1#my_gemset/ruby;
passenger_enabled on;
}
/home/user/demo.my-example.com/sinatra/config.ru
require 'rubygems'
Gem.clear_paths
disable :run, :reload
set :environment, :production
require "./stripe"
run StripeApp
/home/user/demo.my-example.com/sinatra/stripe.rb
require 'sinatra/base'
class StripeApp < Sinatra::Base
get '/' do
"Hello world"
end
end
/home/user/demo.my-example.com/sinatra/config.ru
require 'rubygems'
Gem.clear_paths
require "./stripe"
run StripeApp

Deploy Grape API with Nginx/Passenger

I have Nginx and Passenger installed on my server. Trying to run a Grape (Rack) API off it.
When I deploy Rails applications I have this server block in Nginx conf;
server {
listen 80;
server_name yourserver.com;
# Tell Nginx and Passenger where your app's 'public' directory is
root /path-to-app/public;
# Turn on Passenger
passenger_enabled on;
passenger_ruby /path-to-ruby;
}
The instructions on Passenger's tutorial are;
The server block's root must point to your application's public
subdirectory.
What would this root be in case of my Grape API?
In case of grape, you need to create an empty public folder and point to this folder in the sever block.

How to make passenger and rvm work together?

I've had rvm installed beforehand. And I decided to install passenger from a package (nginx-full and passenger) and would like to use the ruby installed with rvm. But somehow it doesn't work. Here's the test sinatra app I'm using (~yuri/a1/app.rb):
require 'rubygems'
require 'sinatra'
get '/' do
"Hello and Goodbye"
end
~yuri/a1/config.ru:
require 'rubygems'
require 'sinatra'
require './app.rb'
run Sinatra::Application
nginx.conf:
http {
...
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# the paths in the above file point out to debian repository's ruby version
server {
server_name a1;
root /home/yuri/a1;
access_log /var/log/nginx/a1-access.log;
error_log /var/log/nginx/a1-error.log;
passenger_enabled on;
passenger_ruby /home/yuri/.rvm/wrappers/ruby-1.9.3-p385#a1/ruby;
}
}
But when I do w3m http://a1 access.log says:
127.0.0.1 - - [12/Sep/2013:21:14:58 +0300] "GET / HTTP/1.0" 403 168 "-" "w3m/0.5.2+cvs-1.1027"
and error.log:
2013/09/12 21:14:58 [error] 27622#0: *1 directory index of "/home/yuri/tr/" is forbidden, client: 127.0.0.1, server: tr, request: "GET / HTTP/1.0", host: "a1"
The app works if I run it as follows: rvm ruby-1.9.3-p385#a1 && ruby app.rb.
Is there a way to track down what's happening there? Or how to make it work?
passenger expects application directories to have a certain layout. Particularly, config.ru must reside up one level relative to the document root. That is:
/etc/nignx/nginx.conf:
...
http {
...
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
server {
server_name a1;
root /home/yuri/a1/public;
access_log /var/log/nginx/a1-access.log;
error_log /var/log/nginx/a1-error.log;
passenger_ruby /home/yuri/.rvm/wrappers/ruby-1.9.3-p385#a1/ruby;
passenger_enabled on;
}
}
~yuri/a1/app.rb:
require 'sinatra'
get '/' do
"Hello World!"
end
~yuri/a1/config.ru:
require './app'
run Sinatra::Application
And:
$ rvm install 1.9.3-p385
...
$ rvm --create use 1.9.3-p385#a1
...
$ curl http://a1
Hello World!
I just went through setting up passenger 4.0.17 on Ubuntu Precise using the apt approach.
Here is what worked for me.
In the /etc/nginx/nginx.conf
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/rvm/wrappers/ruby-2.0.0-p247/ruby;
Try moving the passenger_ruby to outside of the server block.
Good Luck

passenger_base_uri not working on passenger 4.0.0rc4

We are running ruby 2.0.0-p0 with passenger 4.0.0rc (nginx/1.2.7) on ubuntu 12.04 server in production (rails 3.2.12). Just notice that the passenger_base_uri in our nginx.conf is not pointing to the base subdir. Instead it return nothing. Here is passenger_base_uri in nginx.conf:
server {
listen 80;
server_name 154.49.55.6;
root /ebs/www/;
passenger_enabled on;
rails_env production;
passenger_base_uri /nbhy;
#for rails >= 3.1, assets pipeline
location ~ ^/assets/ {
expires max;
add_header Cache-control public;
add_header ETag "";
break;
}
}
A symlink nbhy was created on the server under /ebs/www and point to /ebs/www/nbhyop/current/public. The /nbhy is the base uri for rails app.
Here is the nginx error log,
[ 2013-05-06 17:47:00.5469 718/7f5097fc0700 Pool2/Implementation.cpp:1098 ]: [App 838 stdout]
2013/05/06 17:47:26 [error] 735#0: *104 open() "/ebs/www/authentify/session" failed (2: No such file or directory), client: 6.5.5.94, server: 154.49.55.6, request: "POST /authentify/session HTTP/1.1", host: "154.49.55.6", referrer: "http://154.49.55.6/nbhy/signin"
The path above should be: /ebs/www/nbhy/authentify/session instead of /ebs/www/authentify/session.
The same config is working on passenger 3.x. How to fix this problem? Thanks for help
I believe you were using passenger_base_uri for the wrong purpose. By specifying passenger_base_uri, you are saying:
"I have another web app, living under /nbhy"
I see in the Nginx output that you were doing a POST /authentify/session. Since you are not accessing any URL under /nbhy, the passenger_base_uri option has no effect. If you POST /nbhy/authentify/session, then it'll have effect.
Perhaps you want to set the virtual host's root to /ebs/www/nbhyop/current/public?

Sinatra on Nginx configuration - what's wrong?

I followed this tutorial more or less... I installed the passenger gem, executed passenger-install-ginx-module, sucessfully installed nginx and inserted this into the config:
server {
listen 80;
server_name localhost;
root /home/admin/sintest/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
In /home/admin/sintest I have: an empty public folder,
the config.ru:
require 'sinatra'
set :env, :production
disable :run
require './app.rb' #the app itself
run Sinatra::Application
and a test sinatra app.rb:
require 'sinatra'
get '/' do
"hello world!"
end
Now when I run nginx and open up http://localhost what I get is: 403 Forbidden
What am I doing wrong? Have I missed something?
Make sure that the user nginx is running as (in most cases 'nobody' or 'www-data') has permission to read the contents of your home directory /home/admin.
Also you can look into the nginx logs and read exactly what the error was.
I had the same error until I added passenger_root and passenger_ruby directives in the http block.

Resources