Skip Navigation
Show nav
Dev Center
  • Get Started
  • ドキュメント
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
    • .NET
  • ドキュメント
  • 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 のアーキテクチャ
    • Dyno (アプリコンテナ)
      • Dyno Management
      • Dyno Concepts
      • Dyno Behavior
      • Dyno Reference
      • Dyno Troubleshooting
    • スタック (オペレーティングシステムイメージ)
    • ネットワーキングと DNS
    • プラットフォームポリシー
    • プラットフォームの原則
  • Developer Tools
    • コマンドライン
    • Heroku VS Code Extension
  • デプロイ
    • Git を使用したデプロイ
    • Docker によるデプロイ
    • デプロイ統合
  • 継続的デリバリーとインテグレーション
    • 継続的統合
  • 言語サポート
    • Node.js
      • Working with Node.js
      • Node.js Behavior in Heroku
      • Troubleshooting Node.js Apps
    • Ruby
      • Rails のサポート
      • Bundler の使用
      • Working with Ruby
      • Ruby Behavior in Heroku
      • Troubleshooting Ruby Apps
    • Python
      • Working with Python
      • Python でのバックグランドジョブ
      • Python Behavior in Heroku
      • Django の使用
    • Java
      • Java Behavior in Heroku
      • Working with Java
      • Maven の使用
      • Spring Boot の使用
      • Troubleshooting Java Apps
    • PHP
      • PHP Behavior in Heroku
      • Working with PHP
    • Go
      • Go の依存関係管理
    • Scala
    • Clojure
    • .NET
      • Working with .NET
  • データベースとデータ管理
    • Heroku Postgres
      • Postgres の基礎
      • Postgres スターターガイド
      • Postgres のパフォーマンス
      • Postgres のデータ転送と保持
      • Postgres の可用性
      • Postgres の特別なトピック
      • Migrating to Heroku Postgres
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • その他のデータストア
  • AI
    • Working with AI
    • Heroku Inference
      • Inference API
      • Quick Start Guides
      • AI Models
      • Inference Essentials
    • Vector Database
    • Model Context Protocol
  • モニタリングとメトリクス
    • ログ記録
  • アプリのパフォーマンス
  • アドオン
    • すべてのアドオン
  • 共同作業
  • セキュリティ
    • アプリのセキュリティ
    • ID と認証
      • シングルサインオン (SSO)
    • Private Space
      • インフラストラクチャネットワーキング
    • コンプライアンス
  • Heroku Enterprise
    • Enterprise Accounts
    • Enterprise Team
    • Heroku Connect (Salesforce 同期)
      • Heroku Connect の管理
      • Heroku Connect のリファレンス
      • Heroku Connect のトラブルシューティング
  • パターンとベストプラクティス
  • Heroku の拡張
    • Platform API
    • アプリの Webhook
    • Heroku Labs
    • アドオンのビルド
      • アドオン開発のタスク
      • アドオン API
      • アドオンのガイドラインと要件
    • CLI プラグインのビルド
    • 開発ビルドパック
    • Dev Center
  • アカウントと請求
  • トラブルシューティングとサポート
  • Salesforce とのインテグレーション
  • 言語サポート
  • Ruby
  • Working with Ruby
  • Rack ベースのアプリのデプロイ

Rack ベースのアプリのデプロイ

日本語 — Switch to English

最終更新日 2021年01月04日(月)

Table of Contents

  • 純粋な Rack アプリ
  • フレームワーク
  • データベースアクセス
  • Rack::Sendfile

Heroku では Rack​ をサポートしており、Sinatra​、Ramaze​、Camping などの Rack ベースの Web フレームワークもサポートしています。

Rack ベースのアプリを実行するには、Gemfile​ に加えて、config.ru​ という名前のラックアップファイルをアプリのルートディレクトリに 含めます。config.ru​ ファイル規約は一般的になっているため、既存の Rack アプリケーションのほとんどは変更なしで Heroku にデプロイできます。

純粋な Rack アプリ

まず、新しいディレクトリを作成し、単純な config.ru​ ファイルを記述します。

$ 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

ローカルでテストします。

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

Heroku にデプロイします。

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

アプリが Heroku にデプロイされました。heroku open​ を実行するか、ブラウザでアプリの URL にアクセスしてテストします。「Hello, World!​」と表示されます。

フレームワーク

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 では、Rack アダプターは必要ありません。代わりに run Hello​ を使用してください。

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'

データベースアクセス

ActiveRecord の使用

ActiveRecord をスタンドアロンで使用する非 Rails アプリの場合、DATABASE_URL​ にアクセスするには、次のコードをアプリケーションに配置します。

require 'active_record'

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

上記のコードでは、mydb​ という名前のデフォルトのローカル PostgreSQL データベースを使用しますが、この値を変更して任意の場所を指定したり、シェルで DATABASE_URL​ 環境変数を設定してアプリを実行することによって上書きしたりできます。

DataMapper または Sequel の使用

DataMapper と Sequel はどちらもネイティブでデータベース URL を使用するため、設定は簡単です。

DataMapper の場合:

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

Sequel の場合:

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

Rack::Sendfile

Rack::Sendfile​ は通常、Ruby アプリケーションを経由せず Web サーバーから直接、静的ファイルを提供するために使用されます。

Heroku では Rack::Sendfile​ の使用をサポートしていません。Rack::Sendfile​ は通常、nginx や apache などのフロントエンド Web サーバーがアプリと同じマシンで実行されていることを要求します。 Heroku はこのように設計されていません。Rack::Sendfile​ ミドルウェアを使用すると、Content-Length​ が 0 の本体が送信されるため、ファイルのダウンロードが失敗します​。

デフォルトでは、Rails はこれを nil​ に設定しますが、config.action_dispatch.x_sendfile_header​ が config/enviroments/production.rb​ で設定されていないことを確認してください。

関連カテゴリー

  • Working with Ruby
Windows で生成された Ruby プロジェクトのデプロイ Rack を使用した Ruby での静的サイトの作成

Information & Support

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

Language Reference

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

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing
  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Github
  • LinkedIn
  • © 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
  • heroku.com
  • Legal
  • Terms of Service
  • Privacy Information
  • Responsible Disclosure
  • Trust
  • Contact
  • Cookie Preferences
  • Your Privacy Choices