22

I installed Sinatra and it works but it uses port 4567 by default. I want it to run on port 80.

In an effort to get it to work on port 80, I tried this:

require 'rubygems'
require 'rack/handler/webrick'
require 'sinatra'

Sinatra::Application.default_options.merge!(
  :run => false,
  :env => :production,
  :port => 80
)

get '/' do
  "Hello World"
end

But I get this error:

$ ruby -rubygems index.rb
index.rb:5:in `<main>': undefined method `default_options' for Sinatra::Application:Class (NoMethodError)

Any idea what's going on?

Jason Swett
  • 43,526
  • 67
  • 220
  • 351
  • 2
    Since you are running Ruby 1.9, note that you no longer need to `require 'rubygems'`; they're included in 1.9 for you. You also do not need to require 'webrick', Sinatra will do that as a fallback if a better server (like Thin) isn't installed. – Phrogz Jan 27 '11 at 20:36

4 Answers4

44

Can't you just use (http://www.sinatrarb.com/configuration.html):

set :port, 80

Note that in order to bind a socket to port 80, you'll need to have superuser privileges.


And, by the way,

Using Sinatra.default_options to set base configuration items is obsolete

From: http://www.sinatrarb.com/one-oh-faq

miku
  • 181,842
  • 47
  • 306
  • 310
  • When I try to do that: `/home/jason/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/utils.rb:73:in `initialize': Permission denied - bind(2) (Errno::EACCES)` – Jason Swett Jan 27 '11 at 20:34
  • 7
    To bind to anything below port 1024, you need to run as root. – Dylan Markow Jan 27 '11 at 20:34
  • 1
    @JasonSwett That either means that you already have a web server running on port 80, or you need to run the script as a super user to be allowed to set up on port 80. – Phrogz Jan 27 '11 at 20:35
  • I have Apache already running on port 80. I guess I'll just try stupid `mod_ruby` some more. – Jason Swett Jan 27 '11 at 20:45
  • @Jason: Or just stop apache (if you can). Or, use apache rewrites. I once used them on a subdomain (ruby ran on port 18080): `.htaccess` looked like `RewriteRule ^(.*)$ http://127.0.0.1:18080/$1 [P] [QSA]`. – miku Jan 27 '11 at 20:46
  • @Jason: Passenger might be a better option, it works with Sinatra. Their documentation includes instructions on using Sinatra: http://www.modrails.com/documentation/Users%20guide%20Apache.html – Dylan Markow Jan 27 '11 at 20:49
17

An alternate way to accepted answer

rvmsudo rackup -p 80

In case one is using RVM to manage Ruby versions, you may not be able to use sudo that easily (or else would need to setup ruby in path).

ch4nd4n
  • 4,110
  • 2
  • 21
  • 43
2

Any port below 1024 is for privileged processes only. You'd have to run as root to run the sinatra app directly on 80. You could reverse proxy - http://sinatra-book.gittr.com/#deployment.

Nick
  • 6,967
  • 2
  • 34
  • 56
0

Yes, running anything other than Apache, Nginx, Varnish or HAProxy or port 80 is in my opiniona dangerous game. Those tools are very good at what they do. A reverse proxy setup is the way to go.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
MikeB
  • 577
  • 5
  • 15