LoginSignup
0
0

More than 3 years have passed since last update.

Webサービスをインターネットに公開する(cloud9からHerokuにデプロイ)

Last updated at Posted at 2020-01-20

Herokuについて

  • https://jp.heroku.com
  • Paas(Platform as s Service)と呼ばれるクラウドサービス
  • 元々、Rubyのアプリケーションをホスティングするサービス
  • PHP等、他の言語にも対応

デプロイの準備

設定箇所
1. Gemfile
2. config/database.yml
3. config/environments/production.rb
4. config/routes.rb

1.Gemfile

Gemfileにあるgem'sqlite3'を切り取って group :development, :test doに貼り付ける
gem 'pg', '~> 0.18.4'を追加する

Gemfile
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'sqlite3'
end
Gemfile
group :production do
  gem 'pg', '~> 0.18.4'
end

bundle install --without productionを実行する(production以外のgemをインストールする)

bundle install --without production

2.config/database.yml

sqliteはproduction環境では使わないので、database: db/production.sqlite3を削除し、下記のように記述する

config/database.yml
production:
  <<: *default
  adapter: postgresql
  encoding: unicode

3.config/environments/production.rb

config.assets.compile = falseをtrueに書き換える

config/environments/production.rb
# Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true

4.config/routes.rb
rootページ(例:root 'questions#index')が設定されているか確認する(設定されていないとエラーになってしまう)

global configの設定

個人の識別情報を登録する

$ git config --global user.name "名前"
$ git config --global user.email メールアドレス

gitの設定

ソースコードのバージョンを管理するツール

gitリポジトリの新規作成

$ git init

管理するファイルを選択する
※git ignoreファイルで書かれたものは登録しない
cloud9の設定アイコンからShow Hidden Filesにチェックが入っているか確認する

$ git add -A

commitという登録処理をする

$ git commit -m "Initial commit"

Heroku Cliのインストール

Heroku Cli:cloud9などからHerokuを操作するためのツール
cli(Command Line Interface)

ファイルをダウンロードする
https://devcenter.heroku.com/articles/heroku-cliのOther installation methodsを使う
(TarballsのLinux(x64)のリンクをコピー)

$ curl -OL https://cli-assets.heroku.com/heroku-linux-x64.tar.gz

圧縮ファイルなので、解凍する。

tar zxf heroku-linux-x64.tar.gz

herokuフォルダを/user/localフォルダに移動する
/usr/local:一般的にシステム管理者が自分でコンパイルしたアプリケーションをインストールする場所

sudo mv heroku /usr/local/

パスの設定を行う

echo 'PATH=/usr/local/heroku/bin:$PATH' >> $HOME/.bash_profile

PATH=/usr/local/heroku/bin:$PATHという文字列をユーザーのホームディレクトリにある、.bach_profileというファイルの内容に追記、という意味。
.bach_profile:シェルの設定ファイルのこと。
シェルというのは、人間からコンピュータに命令を伝えるための仕組み。

設定したパスを反映する

source $HOME/.bash_profile

インストール出来たか確認するために、herokuのバージョンを確認する

heroku -v

インストール用にダウンロードしたheroku-linux-x64.tar.gzを削除する

rm -f heroku-linux-x64.tar.gz

RailsアプリとHerokuの関連付け

アプリのフォルダまで移動し、heroku cliを使ってherokuにログインする
EmailとPasswordを入力したらログイン完了!

$ heroku login

もしheroku: Press any key to open up the browser to login or q to exit:と表示されてログインできない場合は、

$ heroku login --interactive

を実行する

Railsアプリとherokuの関連付けを行う
アプリを表示するためのURLは https://アプリ名.herokuapp.com/

$ heroku create アプリ名

デプロイ

ソースコードをサーバーにデプロイする

$ git push heroku master

実行した時に、"You must use bundle 2 or greater with this lockfile."
とエラーが表示されたら、https://programmingnavi.com/1685/サポートサイトの記事を参照する

データベースのマイグレーションを実行する

$ heroku run rails db:migrate

インターネットに公開されたwebサイトの情報を確認する

$ heroku apps:info

※アプリを削除する場合

heroku apps:destroy --app アプリ名

本当に削除して良いか確認を求められるので、再度アプリ名を入力する
→削除される

こちらの内容はhttps://www.udemy.com/course/the-ultimate-ruby-on-rails-bootcamp/
を元に作成しました。

0
0
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
0
0