How to run Sinatra App in Passenger with Nginx? - ruby

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

Related

Can't access sinatra service outside of container

I am getting ERR_EMPTY_RESPONSE in the browser after lunching the Sinatra container in docker. If I connect to the docker container and run curl localhost:4567 it works, but outside of the container - doesn't
web.rb
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
'Hello world!'
end
Dockerfile
FROM ruby:3.0.2
EXPOSE 4567
COPY ./web.rb .
RUN gem install sinatra
RUN gem install puma
CMD ["ruby", "web.rb"]
The solution was in binding application to 0.0.0.0 host
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
'Hello world!'
end

How to make Sinatra listen on two ports with socat

I had a classical Sinatra application which was accessible on two ports. After migrating it to modular style the second port is not working anymore.
My initial implementation was:
require 'sinatra'
set :port, 8080
set :bind, '0.0.0.0'
----some routes-----
...
The resulting implementation was:
require 'sinatra/base'
require_rel 'lib'
class MyApp < Sinatra::Base
register Sinatra::SomeRegister
helpers Sinatra::SomeHelper
set :port, 8080
set :bind, '0.0.0.0'
----some routes-----
...
run!
end
The application is run using:
socat tcp-l:8181,fork,reuseaddr tcp:localhost:8080 &
ruby /path/my_app.rb
The application doesn't respond on port 8181 anymore.
The fix was to install socat first:
apt-get update && apt-get --allow-unauthenticated -y install socat

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

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

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