LoginSignup
7
9

More than 5 years have passed since last update.

RailsアプリをAWSにデプロイするまでの手順

Posted at

個人的なメモです。参考にはならないと思います。間違っていたらコメントでおしらせください。

1・Rails newの前に・・

まずは

$ mkdir [app_name]
$ cd [app_name]
$ bundle init
Writing new Gemfile to /path/to/project_name/Gemfile

作成されたGemgileを編集。

# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "rails"

railsをインストール。

$ bundle install --path vendor/bundle

2・Railsプロジェクトを作る

$ bundle exec rails new . -B -d mysql --skip-turbolinks --skip-test

そしてGemのインストール

# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

ruby '2.6.0'
gem 'rails', '5.2.0'

gem 'mysql2', '>= 0.4.4', '< 0.6.0'
gem 'puma', '~> 3.11'
gem 'bcrypt', '~> 3.1.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'devise'
gem "omniauth"
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
gem 'kaminari', '~> 0.17.0'
gem 'jquery-ui-rails'
gem 'jquery-rails'
gem 'materialize-sass'
gem 'mini_racer'
gem 'bootstrap-sass'
gem "font-awesome-rails"
gem 'carrierwave'
gem 'mini_magick'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano-rbenv'

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'pry-rails'
  gem 'pry-byebug'
  gem 'pry-doc'
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'rails-controller-testing'
  gem 'hirb'
  gem 'hirb-unicode'
  # gem 'capistrano3-unicorn'
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'sqlite3'
end

group :test do
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

group :production, :staging do
  gem 'unicorn'
  gem 'rails_12factor', '0.0.2'
end

不要なGemがいくつかある気がする。

3・ローカル環境とGitとの連携

gitと連携させる。まずはGithubにてアカウント作成。

$ git init
$ git add .
$ git commit -m "first commit"

https://github.com/ でレポジトリ作成。リポジトリ上に表示されるhttpsから始まるURLをコピー。

$ git remote add origin https://github.com/username/****.git(githubからコピー)
$ git push -u origin master

これでとりあえずはpushできるようになった。

gitとssh接続できるように公開鍵・秘密鍵を設定する。

$cd ~/.ssh
$ssh-keygen -t rsa
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/(username)/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

生成された公開鍵をgithubに登録。
そして

$vim ~/.ssh/config

でssh接続設定を行う。

Host github
  user git
  Hostname github.com
  Port 22
  IdentityFile ~/.ssh/id_rsa

これで接続テスト。

&ssh -T github

次にpush時に毎回URLを書かなくていいようにURLを登録する。

git remote add origin [git@github.com:Username/app_name***.git]

これで

git add .
git commit -m"second commit"
git push [remote-name] [branch-name]

でpushされるはず。初回は$git push origin masterでいいが、次回以降の開発ではBranchを切ってそこにpushする。

どんなURLが登録されているのか確認したいときは

$git remote -v

でおけ。

4・EC2(AWS)設定

まずはEC2の設定。※ルートユーザーでなくIAMユーザーとして操作する。
・大まかな流れはこちらの記事を参考にします。
・詳細はこちらの記事を参考にさせていただきます。

【4-1・ネットワーク環境設定】

・VPCの作成
・サブネット
・インターネットゲートウェイ
・サブネットとルートテーブルの関連付け
・セキュリティグループの作成

【4-2・RDS設定】

・サブネットグループの作成
・DBインスタンスの作成

【4-3・AWSにてEC2設定】

・インスタンスの作成
・Elastic IPの割り当て
・インスタンスにSSHでログイン

【4-4・サーバーの環境構築】

・Nginxの設定
・管理ユーザーの作成
・ドキュメントルート領域を作成
・プラグインのインストール
・MySQLをセットアップ
・パラメータグループを作成

【4-5・Railsアプリのデプロイ】

・GitHubとEC2の連携
・アプリケーション格納用の領域を作成
・GitHubからRailsアプリをクローン
・nginxの設定
・Unicornの設定

【4-6・ロードバランサー(AWS)の設定】

【4-7・アプリ起動】

5・独自ドメインを紐付ける

5-1・お名前.comで独自ドメインを取得

5-2・Route53(AWS)にて設定

5-3・お名前.comにてネームサーバー変更

Route53での設定で表示された4つのネームサーバーをお名前.comに転記。

5-4・ネームサーバーの確認

下記コマンドでネームサーバーが設定されてるか確認できる。

$nslookup
続いて下記コマンド入力。
> set type=ns
> ドメイン名

~~~~以下のように出たらOK~~~~
Server:     10.0.0.2
Address:    10.0.0.2#53

Non-authoritative answer:
christchurches-map.com  nameserver = ns-1520.awsdns-62.org.
christchurches-map.com  nameserver = ns-1643.awsdns-13.co.uk.
christchurches-map.com  nameserver = ns-193.awsdns-24.com.
christchurches-map.com  nameserver = ns-517.awsdns-00.net.

5-5・ドメイン適用

Route53にてCreate Record Setを作成する。

5-6・サーバーでNginx設定

$ sudo vi /etc/nginx/conf.d/アプリケーション名.conf
~~~~アプリケーション名.conf~~~~
変更前
server {
    listen 80;
    client_max_body_size 4G;
    server_name 52.192.101.190;

変更後
server {
    listen 80;
    client_max_body_size 4G;
    server_name www.christchurches-map.com;

5-7・Unicornのプロセスをkill

$ cd /var/www/projects/christchurches-map/
#Unicornの起動確認
$ ps -ef | grep unicorn | grep -v grep
$kill 番号

5-8・Nginx&Unicorn再起動

$ sudo service nginx restart

その後Unicornも再起動。
ドメインにアクセスしてアプリが表示されるか確認。

6・ssl(https)接続設定を行う

6-1・AWSのCertificate Managerで各種設定

設定を行いリクエストを送信。届いたメールを承認する。

6-2・EC2にて設定

EC2→ロードバランサー→リスナーを編集しhttpをhttpsに変更。
次の画面でACMの既存の証明書(CMで発行したもの)をチェック。
保存する。

6-3・Route53にて設定

Hosted Zone→対象ドメインを選んでGo to Record Setsへ。
Type:Aドメインを選択してAliasをYesに。
TargetにELBを選ぶ。

6-4・Nginx(サーバー側)の設定

何かをどうにかする。(調査中)

6-5・ローカルにて

config/environments/production.rbを編集。

#下記行のコメントアウト外す
config.force_ssl = true

6-6・もろもろ再起動

Unicornのプロセスkillして、Nginx再起動&Unicorn再起動

6-7・ブラウザからドメインにアクセス

以上。

下記記事を参考にしました。

初心者向け:AWS(EC2)にRailsのWebアプリをデプロイする方法

RailsアプリをAWSにイチからデプロイするまでの手順メモ

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