Zhibin's blog

always smile :-)

部署 Rails 程序到 Heroku

| Comments

什么是 Heroku ?

Heroku 是一个 Saas (云应用平台),用户可以将自己的 web 程序部署到 Heroku 云主机上。使用简单的命令就可以部署你的程序到 Heroku。 Heroku 使用 Git 作为版本控制工具。Heroku 目前支持 Ruby, Node.js,Clojure,Java,Python,Scala。默认数据库是 PostgreSQL。

Build. Deploy. Scale. Heroku brings them together in an experience built and designed for developers. – Larry Marburger, CloudApp

想要部署你的程序到 Heroku?

Let’s Go !

Step 1:创建账户

你需要先创建一个 Heroku的账户,如果你已经有了,直接看 Step 2.

点此创建 Heroku 账户

Step 2: 安装 Heroku Toolbelt

Linux 用户请查看 安装 Heroku toolbelt , 或直接执行下面命令:

1
wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

我在执行的时候发现非常的慢,就在浏览器打开了上面这个URL 然后,保存下来,手动执行了一下。

我还修改了一行, 将:

1
apt-get install -y heroku-toolbelt

改为:

1
apt-get install -y --force-yes heroku-toolbelt

Step 3: 登录

安装好之后,就可用命令行登录 heroku:

1
2
3
4
5
6
7
8
$ heroku login
Enter your Heroku credentials.
Email: adam@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub

Step 4: 准备好自己的程序

一切就绪,你需要准备好自己的程序。

由于 Heroku上 使用的是 PostgreSQL , 所以我们需要修改下Gemfile。

将:

1
gem 'sqlite3'

修改为:

1
2
3
4
5
6
group :development do
  gem 'sqlite3'
end
group :production do
  gem 'pg'
end

确保你的 app 已经纳入 Git 版本控制之中,如果没有请执行:

1
2
3
git init
git add -A
git commit -m "initial commit"

Step 5: 部署程序到 Heroku

下面该上传你的 app 到 Heroku 了:

创建 Heroku App:

1
2
3
4
$ heroku create
Creating stark-fog-398... done, stack is cedar
http://stark-fog-398.herokuapp.com/ | git@heroku.com:stark-fog-398.git
Git remote heroku added

push 代码到 Heroku:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ git push heroku master
Counting objects: 67, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (52/52), done.
Writing objects: 100% (67/67), 86.33 KiB, done.
Total 67 (delta 5), reused 0 (delta 0)

-----> Heroku receiving push
-----> Rails app detected
-----> Installing dependencies using Bundler version 1.1
       Checking for unresolved dependencies.
       Unresolved dependencies detected.
       Running: bundle install --without development:test --path vendor/bundle --deployment
       Fetching source index for https://rubygems.org/
       Installing rake (0.8.7)
       ...
       Installing rails (3.0.5)
       Your bundle is complete! It was installed into ./vendor/bundle
-----> Rails plugin injection
       Injecting rails_log_stdout
       Injecting rails3_serve_static_assets
-----> Discovering process types
       Procfile declares types -> (none)
       Default types for Rails -> console, rake, web, worker
-----> Compiled slug size is 8.3MB
-----> Launching... done, v5
       http://severe-mountain-793.herokuapp.com deployed to Heroku

To git@heroku.com:severe-mountain-793.git
 * [new branch]      master -> master

如果这一步时间很长,或者返回 Timeout , 而且你在 China ,那可能遇到 这个问题 了。

Note: 不要使用 rails new yourapp 新建一个 app 来测试 Heroku, 至少你也得建个 welcome#index 来测试吧。

这里 是我用来测试的 rails app.

Comments