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
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
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で開発した場合)
- <<: *default
- database: db/production.sqlite3
+ adapter: postgresql
+ encoding: unicode
+ database:
+ pool: 5
+ username:
+ password:
-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