Rails:アプリケーションをHerokuにデプロイする手順

前提:Herokuでアカウントを作成しているとする。

1. Herokuにログインする

$ heroku login
Enter your Heroku credentials.
Email: youremail@gmail.com
Password (typing will be hidden):
Authentication successful.

2. 公開鍵を生成する

herokuとSSH通信するために公開鍵を生成する。 id_rsaとは別にheroku_rsaというファイルに保存することにする。

$ cd ~/.ssh/
$ ssh-keygen -t rsa -C "youremail@gmail.com" -f heroku_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in heroku_rsa.
Your public key has been saved in heroku_rsa.pub.
The key fingerprint is:
16:ed:31:28:25:92:2f:d9:f0:b9:5f:86:15:13:5e:19 youremail@gmail.com
The key's randomart image is:
+--[ RSA 2048]----+
|    ... . ..Eo   |
|    o. o +o..    |
|     *..o =o     |
|    o =. o.o     |
|     . .So.      |
|      ... o      |
|       . o       |
|        .        |
|                 |
+-----------------+

今回はパスワードは設定しなかったです。忘れるし...
heroku_rsa.pubというファイルも生成されてます。

$ ls
config    heroku_rsa    heroku_rsa.pub    id_rsa    id_rsa.pub    known_hosts  

3.~/.ssh/configに設定を記述する

Githubなど他の公開鍵の設定もしている場合は分けるために~/.ssh/configでHostを指定する。

$ vi ~/.ssh/config

#Heroku用
Host heroku
  User git
  Port 22
  HostName heroku.com
  IdentityFile ~/.ssh/heroku_rsa
  TCPKeepAlive yes
  IdentitiesOnly yes

#Github用
Host github
  User git
  Port 22
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
  TCPKeepAlive yes
  IdentitiesOnly yes

4. Herokuにkeyを登録する

$ heroku keys:add /Users/yourname/.ssh/heroku_rsa.pub
Uploading SSH public key /Users/yourname/.ssh/heroku_rsa.pub… done

確認してみると、ちゃんと通信できている模様。

$ ssh -T heroku.com
Warning: Permanently added the RSA host key for IP address '50.20.20.200' to the list of known hosts.
shell request failed on channel 0

5. Herokuにpushするためにコミットする

$ cd your_app_name
$ git init
$ git add .
$ git commit -m "initial commit"

6. Herokuにアプリを作成する

GUIの方でもできます。

$ heroku create your_app_name
Creating your_app_name... done, stack is cedar
http://your_app_name.herokuapp.com/ | git@heroku.com:your_app_name.git

7. pushする

$ git push heroku master
Initializing repository, done.
Counting objects: 225, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (203/203), done.
Writing objects: 100% (225/225), 87.15 KiB | 0 bytes/s, done.
Total 225 (delta 23), reused 0 (delta 0)

………………..


To git@heroku.com:your_app_name.git
 * [new branch]      master -> master

できた!

8. DBを作成する

必要な方は。

$ heroku rake db:migrate

9. ブラウザで確認する

$ heroku open

表示されていれば成功です。

こんな記事も書きました!
1台のパソコンでGithubの複数(2つ)のアカウントを使い分ける - Hello world, I am kgmx.