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.
Related
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
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.
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
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
So I've got two apps I want to run on a server. One app I would like to be the "default" app--that is, all URLs should be sent this app by default, except for a certain path, lets call it /foo:
http://mydomain.com/ -> app1
http://mydomain.com/apples -> app1
http://mydomain.com/foo -> app2
My two rack apps are installed like so:
/var
/www
/apps
/app1
app.rb
config.ru
/public
/app2
app.rb
config.ru
/public
app1 -> apps/app1/public
app2 -> apps/app2/public
(app1 and app2 are symlinks to their respective apps' public directories). This is the Passenger setup for sub URIs described here: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rack_to_sub_uri
With the following config I've got /foo going to app2:
server {
listen 80;
server_name mydomain.com;
root /var/www;
passenger_enabled on;
passenger_base_uri /app1;
passenger_base_uri /app2;
location /foo {
rewrite ^.*$ /app2 last;
}
}
Now, how do I get app1 to pick up everything else? I've tried the following (placed after the location /foo directive), but I get a 500 with an infinite internal redirect:
location / {
rewrite ^(.*)$ /app1$1 last;
}
I hoped that the last directive would prevent that infinite redirect, but I guess not. My /foo rewrite still works. And I can still go to http://mydomain.com/app1.
Any ideas? Thanks!
One way is to create a new config.ru file that dispatches the request to the correct app.
# /var/www/apps/config.ru
require './app1/app1'
require './app2/app2'
map ('/') { run App1 }
map ('/foo') { run App2 }
Of course, this means your apps have to be made in a way that they can live on the same execution space.