LoginSignup
12
13

More than 5 years have passed since last update.

Herokuの使い方

Posted at

03 Herokuの仕組み

Herokuの送るもの(gitによる)
Source Code:
Dependencies: ライブラリの依存関係を示したファイル(railsの場合gemfile)
Procfile: Heroku上で動かしたいコマンド
slug: ソースコード(Source Code)と依存関係のあるライブラリ(Dependencies)をまとめたファイル
Dyno: slugを読み込む仮装サーバー
その他: アドオンにより提供されている(Databaseなど)

04 Heroku Toolbelt (Herokuコマンドを利用できるようにする為のもの)

①(linuxの場合)

ターミナル
wget -O- https://toolbelt.heroku.com/install.sh | sh

②パスを通す

ターミナル
echo 'PATH="/usr/local/heroku/bin:$PATH"' >> ~/.bash_profile

③vagrantを再起動する

④公開鍵と秘密鍵があるか確認

ターミナル
cd .ssh
ls

id_rsa(秘密鍵)・id_rsa.pub(公開鍵)があるか確認

⑤Herokuアカウントを入力し認証する

ターミナル
heroku login

05

ターミナル
git init
git add .
git commit -m "initial commit"

06 Heroku側でアプリケーションを作成

GemfileにHerokuでrailsを動かすためのgemを追加

ターミナル
gem 'rails_12factor', group: :production

Procfileを作成

ターミナル
vi Procfile
procfile
web: bundle exec rails server -p $PORT
ターミナル
git add .
git commit -m "Gemfile updated, Procfile added"

Herokuアプリケーションの作成(リモートリポジトリを作成)

ターミナル
heroku create

07 Heroku上で確認

ローカルのファイルをHerokuにpush

ターミナル
git push heroku master

(うまくいかなかった場合)

ターミナル
bundle update

Herokuにpostgresをアドオンする

ターミナル
heroku addons:add heroku-postgresql

マイグレーションを実行する

ターミナル
heroku run rake db:migrate

(エラー PG::ConnectionBad: FATAL: remaining connection slots are reserved for non-replication superuser connectionsが出た場合)

database.ymlの修正

production:
-  adapter: sqlite3
-  database: db/production.sqlite3
+  adapter: pg
+  database: db/production.pg
ターミナル
git add .
git commit -m "production db changed"

Herokuのurl等を表示

ターミナル
heroku apps:info

08 heroku ps/logs/maintenance

今動いているプロセスを表示

ターミナル
heroku ps

Dynoを増設(クレジット情報が必要)

ターミナル
heroku ps:scale web=2

Herokuで走っているログを表示

ターミナル
heroku logs

リアルタイムでログを表示

ターミナル
heroku logs --tail

Herokuをメンテナンスモードにする

ターミナル
heroku maintenance:on

Herokuをのメンテナンスモードを解除する

ターミナル
heroku maintenance:off

09 One-off Dynos(heroku run)

Herokuに対してコマンドラインでアクセスする

ターミナル
heroku run bash

Herokuでrailsのコンソールを使う

ターミナル
heroku run rails console

10 アプリの更新とリリース

リリース(Herokuにpushした履歴)を表示

ターミナル
heroku releases

ひとつ前のリリースのバージョンに戻る

ターミナル
heroku releases:rollback

ホスト名を変更

ターミナル
heroku rename 

*2回目

1. Herokuにログインする

ターミナル
$ heroku login

2. railsアプリ作成

ターミナル
rails new (appname) -d postgresql
cd (appname)
vi Gemfile
Gemfile
gem 'therubyracer'

3. モデル作成

4. DB作成

ターミナル
rake db:create
rake db:migrate

ブラウザで確認

5. Gitに登録

ターミナル
git init
git add .
git commit -m "(initial commit)"

6. Herokuでrailsを動かすためのgemを追加

(postgreSQLで開発した場合)

ターミナル
vi Gemfile
gem 'rails_12factor', group: :production

(sqliteで開発した場合)

/config/database.yml
-  <<: *default
-  database: db/production.sqlite3
+  adapter: postgresql
+  encoding: unicode
+  database:
+  pool: 5
+  username:
+  password:
Gemfile
-gem 'sqlite3'
+group :development do
+  gem 'sqlite3'
+end
+group :production do
+  gem 'pg'
+  gem 'rails_12factor'
+end

(参考)http://morizyun.github.io/blog/beginner-rails-heroku-tutorial/#1

7. Procfileを作成

ターミナル
vi Procfile
web: bundle exec rails server -p $PORT

8. 変更をGitで更新

ターミナル
$ git add .
$ git commit -m "Gemfile updated, Procfile added"

9. Herokuに新規のアプリケーションを作成

ターミナル
$ heroku create

10. ローカルのファイルをHerokuにpush

ターミナル
$ git push heroku master

11. Herokuにpostgresをアドオンを追加

ターミナル
$ heroku addons:add heroku-postgresql

<確認>

Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pgbackups:restore
Use `heroku addons:docs heroku-postgresql` to view documentation.

12. HerokuにDBを作成

ターミナル
heroku run rake db:migrate
<確認>
== 20************ CreateMemos: migrating ======================================
-- create_table(tablename)
   -> *.****s
== 20************ CreateMemos: migrated (*.****s) =============================

13. アプリにアクセス

ターミナル
$ heroku apps:info

その他のコマンド

Herokuの再起動

ターミナル
$ heroku restart

アセットのプリコンパイル

ターミナル
$ rake assets:precompile
12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13