Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Rails Support
      • Working with Bundler
    • Python
      • Background Jobs in Python
      • Working with Django
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with the Play Framework
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Language Support
  • Ruby
  • Deploying Rack-based Apps

Deploying Rack-based Apps

English — 日本語に切り替える

Last updated January 04, 2021

Table of Contents

  • Pure Rack apps
  • Frameworks
  • Database access
  • Rack::Sendfile

Heroku supports Rack and Rack-based web frameworks like Sinatra, Ramaze, and Camping.

To run a Rack-based app, include a Gemfile, as well as a rackup file named config.ru in the app’s root directory. The config.ru file convention has become common, so most existing Rack applications should not require changes to deploy to Heroku.

Pure Rack apps

First, create a new directory and write a simple config.ru file:

$ mkdir hello
$ cd hello
$ cat <<EOF> config.ru
run lambda { |env| [200, {'Content-Type'=>'text/plain'}, StringIO.new("Hello World!\n")] }
EOF
$ cat <<EOF> Gemfile
source 'https://rubygems.org'
gem 'rack'
EOF

Test it locally:

$ bundle install
$ bundle exec rackup -p 9292 config.ru &
$ curl http://localhost:9292
Hello World!
$ kill %1

Deploy to Heroku:

$ git init
$ git add .
$ git commit -m 'pure rack app'
$ heroku create
$ git push heroku main

The app is now deployed to Heroku. Test by executing heroku open or by visiting your app’s URL in your browser. You should see Hello, World!.

Frameworks

Sinatra

hello.rb:

require 'sinatra'

get '/' do
  "Hello World!"
end

config.ru:

require './hello'
run Sinatra::Application

Gemfile:

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

Ramaze

hello.rb:

require 'ramaze'

class MainController < Ramaze::Controller
  def index
     "Hello World!"
  end
end

config.ru:

require ::File.expand_path('./../hello', __FILE__)
Ramaze.start(:file => __FILE__, :started => true)
run Ramaze

Gemfile:

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

Camping

Camping 2.0 does not require the Rack adapter; use run Hello instead.

hello.rb:

require 'camping'

Camping.goes :Hello

module Hello::Controllers
  class Index < R '/'
     def get
        render :hello
     end
  end
end

module Hello::Views
  def hello
     p  "Hello World!"
  end
end

config.ru:

require './hello'
run Rack::Adapter::Camping.new(Hello)

Gemfile:

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

Database access

Using ActiveRecord

For non-Rails apps using ActiveRecord standalone, put this code into your application to access the DATABASE_URL:

require 'active_record'

ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')

The code above uses a default local PostgreSQL database named mydb, but you can change this value to point anywhere you like, or override by running your app with the DATABASE_URL environment variable set in your shell.

Using DataMapper or Sequel

DataMapper and Sequel both use database URLs natively, so configuration is a snap:

For DataMapper:

require 'data_mapper'
DataMapper.setup(:default, ENV['DATABASE_URL'] || 'postgres://localhost/mydb')

For Sequel:

require 'sequel'
Sequel.connect(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')

Rack::Sendfile

Rack::Sendfile is typically used to serve static files directly from a webserver instead of through your Ruby application.

Heroku does not support the use of Rack::Sendfile. Rack::Sendfile usually requires that a front-end webserver like nginx or apache is running on the same machine as your app. This is not how Heroku is architected. Using the Rack::Sendfile middleware will cause your file downloads to fail, because it sends a body with a Content-Length of 0.

By default, Rails sets this to nil, but make sure that config.action_dispatch.x_sendfile_header is not set in config/enviroments/production.rb.

Keep reading

  • Ruby

Feedback

Log in to submit feedback.

Using WebSockets on Heroku with Ruby Fixing a Segfault in Ruby Apps

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies
  • Cookie Preferences