Deploy Sinatra app on Heroku

2019-01-31 02:37发布

问题:

I have simple Sinatra app.

web.rb:

require 'sinatra'

get '/' do 
    "Hello" 
end

Gemfile:*

source :rubygems

gem 'sinatra', '1.1.0'
gem 'thin', '1.2.7'

config.ru:

require './web'
run Sinatra::Application

But when I deploy my app on Heroku I'll get the error on logs:

2012-03-27T19:17:48+00:00 heroku[router]: Error H14 (No web processes running) -> GET furious-waterfall-6586.herokuapp.com/ dyno= queue= wait= service= status=503 bytes=

How can I fix it?

回答1:

You need a Procfile file alongside your config.ru to tell Heroku how to run your app. Here is the content of an example Procfile:

web: bundle exec ruby web.rb -p $PORT

Heroku Ruby docs on Procfiles

EDIT: Here's a sample config.ru from one of my sinatra/Heroku apps:

$:.unshift File.expand_path("../", __FILE__)
require 'rubygems'
require 'sinatra'
require './web'
run Sinatra::Application

You may need to require sinatra and rubygems for it to work.



回答2:

Here's how to create a minimal sinatra app that deploys to heroku:

app.rb:

require 'sinatra'

get '/' do
  "hello world"
end

Gemfile:

source 'https://rubygems.org'

gem 'heroku'
gem 'sinatra'
gem 'thin'

config.ru:

require './app'
run Sinatra::Application

Type these commands in your command line to deploy (without the $ signs):

$ bundle install
$ git init
$ git add -f app.rb Gemfile Gemfile.lock config.ru
$ git commit -am "initial commit"
$ heroku create <my-app-name>
$ git push heroku master

Then test your app:

$ curl <my-app-name>.heroku.com

and you should see:

hello world


回答3:

I have had this problem a number of times in the past, and it was all because I didn't include my config.ru file with the requiring of [app].rb & then pushing to Heroku. Even if I added it afterwards and restarted, Heroku would never pick it up.

  • Destroy your small app on Heroku's site (http://www.heroku.com)
  • Then remove the remote from your project folder

    $ git remote rm heroku
    
  • Then recreate the app



回答4:

try to restart heroku

heroku restart

here more discussion: unknown heroku error



回答5:

By adding the gem 'heroku' to the Gemfile, I got it working. No Procfile needed.



回答6:

As an update, here is a slightly more minimal app that I created, and confirmed to be working as of today. The thin gem was not needed, and a Procfile was not needed to get an initial working app.

Gemfile

source 'https://rubygems.org'
gem 'sinatra'

config.ru

require './app'

run Sinatra::Application

Note: The require line uses './app' instead of 'app'.

app.rb

require 'sinatra'

get '/' do
  'Hello, World! Find me in app.rb'
end

If you want to use this template, you can copy it, bundle, and push the Git repo.

$ git init
$ git add .
$ git commit -m "initial sinatra app"
$ bundle
$ git add Gemfile.lock
$ git commit -m "bundle install"
$ heroku create
$ git push heroku master
$ heroku open