5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby on Rails チュートリアル 第1章 AWSを利用

Last updated at Posted at 2019-05-19

はじめに

Ruby on Rails チュートリアルの第1章を実際に行た際の手順やコマンドについて
自身の備忘録のためも含め説明いたします。
(2019.07.24追記)
手順に一部不備があったため、更新いたします。

0.Ruby on Rails とは

Webアプリの開発用のフレームワーク(Ruby言語で記述する)

1.AWS Cloud9 への登録

AWS cloud9でAWSアカウントの登録を行う。
登録後はルートユーザサインインを行い、ログインする。

※補足

  1. 登録時のサポートプランは「ベーシックプラン」
  2. Cloud9の対応ブラウザは「Chrome」or「firefox」

2.AWS Cloud9で新しいワークスペースを作成、インテンドの更新

手順に関しては図1-3(こちら)~図1-6に記載されている通り実施する。

※補足:ワークスペースを削除する場合

図1-3のところにも見える「Delete」で削除可能

3.Railsのバージョンを指定してインストール

bash
$ gem install rails -v 5.1.6

※以降すべてAWSのbashで行う

4.アプリケーションの作成

bash
$ rails _5.1.6_ new hello_app

コマンド実行後、「13 gems installed」と表示されれば問題ない。

※作成されるフォルダの構成については(こちら

5.Bundlerのインストール

Gemfileを以下のように更新(リスト1.5と同様)

Gemfile
source 'https://rubygems.org'

gem 'rails',        '5.1.6'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.6.4'

group :development, :test do
  gem 'sqlite3',      '1.3.13'
  gem 'byebug', '9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           '3.5.1'
  gem 'listen',                '3.1.5'
  gem 'spring',                '2.0.2'
  gem 'spring-watcher-listen', '2.0.1'
end

# Windows環境ではtzinfo-dataというgemを含める必要があります
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
bash
$ cd hello_app
$ bundle install
$ bundle update

6.ローカルWebサーバーを起動する

別ターミナルを起動する

bash
$ cd hello_app
$ rails server

※Serverの停止は「Ctrl」+「C」

7.Applicationコントローラにhelloを追加する(リスト1.7)

application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hello, world!"
  end

end

8.ルートルーティングを設定する(リスト1.9)

routes.rb
Rails.application.routes.draw do
  root 'application#hello'
end

演習1-3-1

application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hola, mundo!"
  end
end

演習1-3-2

割愛いたします。

演習1-3-3

application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hola, mundo!"
  end

  def goodbye
    render html: "goodbye, world!"
  end

end
routes.rb
Rails.application.routes.draw do
  root 'application#goodbye'
end

9.リポジトリ上で公開するgitの設定

bash
$ git config --global user.name "Miyasako(任意の名前)"
$ git config --global user.email 任意のメールアドレス

10.gitのセットアップ & コミット

bash
$ cd hello_app
$ git init
$ git add -A
$ git status
$ git commit -m "Initialize repository"

※あくまでローカルマシン上での操作である

※もしも、コミットしたファイルから変更させた内容を戻したい場合

bash
$ git checkout -f

11.Bitbucketのアカウントを作成

12.公開鍵の確認 & 作成

bash
$ cd ~/.ssh
$ ls

このとき、「○○」「○○.pub」というファイル名の組み合わせがあるか確認する。
「○○」が秘密鍵、「○○.pub」が公開鍵になる。(例.「id_rsa」と「id_rsa.pub」)
ない場合は、公開鍵を作成する。

bash
$ ssh-keygen
  Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): id_rsaを入力
  Enter passphrase (empty for no passphrase): パスフレーズを入力。
  入力したくない場合は、パスフレーズを空のままにする
  Enter same passphrase again: 同じパスフレーズを入力
$ ls
  authorized_keys  id_rsa  id_rsa.pub  
$ cat ~/.ssh/id_rsa.pub
ssh-rsa 
AAAAB3NzaC1yc2EAAAADAQABAAABAQDK/oiK0LxiuZFnC4PGedNtJVx9d/xIPoL/o
~~~
7r18M2w2MPoQo2rLFOtv+m3c3H8kr70wgEIRNO8DNl4jnA7 ec2-user@ip-172-
31-44-34

作成した公開鍵を、catコマンドで出力し、コピー(ssh~ ~ 44-34まですべて)したのち、
Bitbucketの公開鍵設定で設定する
左下のアイコン -> Bitbucket settings -> SSH鍵

13.Bitbucketで新しいリポジトリを作成する(READMEは含めない)

※登録のユーザ名は「MiyasakoShohei」とした

14.ローカル環境にあるリポジトリをBitbucketにあげる

新しいターミナルを開く
また、bitbucketのリポジトリを確認すると、
bitvucketのサイトに接続用のコマンドが記載されている

bash
$ cd hello_app
$ git remote add origin git@bitbucket.org:MiyasakoShohei/hello_app.git
$ git push -u origin --all

※もしも、間違えてgit remote addしてしまった場合
 ① まず、すでにaddした内容を取り消す。
   現在登録されているディレクトリをまず確認
 ② その後取り消しコマンドを入力
例えば間違えてユーザ名を「Miyasako」でaddした場合

bash
$ git remote -v
origin  git@bitbucket.org:Miyasako/hello_app.git (fetch)
origin  git@bitbucket.org:Miyasako/hello_app.git (push)
git remote rm origin
$ git remote -v
$ git remote add origin git@bitbucket.org:MiyasakoShohei/hello_app.git
$ git push -u origin --all

15.READMEの更新

ブランチを作成し、作成したブランチへ移動する

bash
$ git checkout -b modify-README
$ git branch

READMEのファイルを更新し、masterへ移動、
masterにブランチの変更をマージし、ブランチを削除する
最後に、Bitbucketにpushする

bash
$ git status
$ git commit -a -m "Improve the README file"
$ git checkout master
$ git merge modify-README
$ git branch -d modify-README
$ git push

16.Herokuのアカウントを作成

本番環境(Heroku)にデプロイする
※デプロイ・・・システムを利用可能な状態にする

17.Gemfileを修正し、gemをインストールする

bash
$ bundle install --without production
$ git commit -a -m "Update Gemfile for Heroku"

18.Herokuのインストール & サインイン

bash
$ source <(curl -sL https://cdn.learnenough.com/heroku_install)
$ heroku --version
  heroku/7.26.2 linux-x64 node-v11.14.0
$ heroku login --interactive
  herokuのアカウント登録時のメルアドとパスワード入力

19.HerokuのSSHキーを登録し、アプリケーションの実行場所を作成する

bash
$ heroku keys:add
$ heroku create

20.デプロイする

bash
$ git push heroku master

※Herokuにデプロイしたアプリの確認

bash
$ heroku apps
  アプリ名が表示される

※Herokuにデプロイしたアプリの情報の確認(URLなど)

bash
$ heroku info --app <アプリ名>

21.デプロイしたアプリの名前変更

bash
$ heroku rename <変更したいアプリ名>

あとがき

cloud9の使用が初めてなこと、またコマンドをたびたび間違えてしまい
その修復のためのコマンド確認も行ったため
完了まで約10時間くらいかかった

以上です。
ご指摘等あればよろしくお願いします。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?