LoginSignup
50
36

More than 3 years have passed since last update.

【AWS】EC2へのデプロイの手順<備忘録>

Last updated at Posted at 2019-10-09

はじめに

プログラミング初心者です。自分のための備忘録として記録を残します。

環境

  • macOS10
  • Rails 5.2.3
  • Nginx
  • Unicorn
  • Postgresql
  • Sidekiq
  • Redis

EC2インスタンス作成

  • Amazon Linux AMI 2018.03.0(HVM,SSD Volume Typeを選択
  • t2.microを選択(無料枠を利用の場合)
  • キーペアの作成


パブリックDNSが生成される。
*https://qiita.com/Quikky/items/2897573a42fd71cfc47fを参考にしました。

rbenvインストールの準備

$ sudo yum install git
$ sudo yum -y install gcc
$ sudo yum -y install gcc-c++
$ sudo yum -y install zlib-devel
$ sudo yum -y install openssl-devel
$ sudo yum -y install readline-devel

rbenvのインストール

$ mkdir ~/.rbenv
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ mkdir ~/.rbenv/plugins ~/.rbenv/plugins/ruby-build
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ cd ~/.rbenv/plugins/ruby-build
$ sudo ./install.sh
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

Rubyのインストール

$ rbenv install 2.4.6
$ rbenv rehash
$ rbenv global 2.4.6
$ ruby -v

Bundlerのインストール


$ rbenv exec gem install bundler -v 2.0.1
$ rbenv rehash

GitHubでプロジェクトを作成

GitHubにPush

Mac上で
$ git remote add origin <URLアドレス>                    
$ git add .                                          
$ git commit -m "1st push"                           
$ git push -u origin master                          

GitHubからEC2にPull


$ git clone <URLアドレス>
$ cd <プロジェクトのディレクトリ>                       

Railsのインストール

$ gem i -v 5.2.3 rails
$ gem list rails                     
$ bundle install                   

上記でエラーが出たので以下のコマンド実施

$ gem install pg -v '1.1.4' --source 'https://rubygems.org/'

postgreSQLのインストール


$ yum install postgresql
$ sudo yum install postgresql-devel                   

SQLite3のエラー回避


$ sudo yum install sqlite-devel                    

インスタンスのセキュリティグループのインバウンド設定

  • カスタムTCP/IP port 3000 0.0.0.0/0 を設定追加。

EC2インスタンス内でPostgres起動


$ sudo yum install -y postgresql-server
$ sudo /sbin/service postgresql initdb
$ sudo /sbin/service postgresql start                    

EC2のインスタンス内でのPostgresの設定


マスターユーザー名:<プロジェクト名>
マスターパスワード:<パスワード>
postgres=# create role <username> with createdb login password '<password>';
postgres=# create database [database_name] owner [user_name];

$ sudo -u postgres psql
postgres=# create role <プロジェクト名> with createdb login password '<パスワード>';
postgres=# create database <プロジェクト名>_production owner <プロジェクト名>;
postgres=# create database <プロジェクト名>_development owner <プロジェクト名>;
postgres=# create database <プロジェクト名>_test owner <プロジェクト名>;
postgres=# \q                 

DB(Postgres)の作成

.bash_profile
export POSTGRES_USERNAME="<プロジェクト名>"
export POSTGRES_PASSWORD="<パスワード>"

プロジェクトのディレクトリ
$ rails db:create                     
 /var/lib/pgsql9/data/pg_hba.conf

local all all peer

     ↓

local all all md5

に変更。                     
$ rails db:create
$ rails db:migrate                    

SMTPの設定

環境変数を.bash_profileに設定します。

EMAIL_ADDRESS
EMAIL_PASSWORD

設定おわったら、それを反映させるため以下のコマンドを実行

$ source .bash_profile                     
  • RedisサーバをEC2に立ち上げる。
  • ElasticCache選択
  • 作成

primary endpointが生成。

VPCの設定で、Redisのインバウンド設定をセキュリティーグループにする

  • sg-694f3715のインバウンドルールの編集で追加
  • カスタムTCP:TCP
  • port:6379
  • source 0.0.0.0/0

以下のファイル設定

config/initializers/sidekiq.rb
config/redis.yml                    

プロジェクトを再起動してSidekiqを起動

$ bundle exec sidekiq                      

Nginxをインストール

$ sudo yum install nginx

Mac上の/usr/local/etc/nginx/nginx.confをEC2環境にコピーする。


$ scp -i ~/.ssh/<プロジェクト名> nginx.conf ec2-user@XXXXXXXXXXXXXXXXXXXXX.amazonaws.com:~/nginx.conf
$ sudo cp ~/nginx.conf /etc/nginx/

ポート番号や、rootディレクトリを修正する。

Unicornの起動

$ unicorn -c config/unicorn.rb -E production -D                      

Nginx起動

$ sudo service nginx start                     

VPCの設定で、port80(HTTP)を追加

ホームディレクトリの権限を744(drwxr--r--)に変更する。

$ chmod 755 ~/                     

Nginxのリスタート

$ sudo service nginx restart

production 環境のDBを構築する。

$ rails db:migrate RAILS_ENV=production                      

Unicornの再起動

$ kill [pid]
$ unicorn -c config/unicorn.rb -E production -D                     

アセットパイプラインでエラーが発生。



config/environments/production.rb内の
config.assets.compileをfalseからtrueに変更                      

↓↓↓↓↓↓↓ あなたの記事の内容
*https://kakuyon.hatenablog.com/entry/2018/07/15/03
3059を参考にしました。
───────
*https://kakuyon.hatenablog.com/entry/2018/07/15/033059を参考にしました。
↑↑↑↑↑↑↑ 編集リクエストの内容

50
36
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
50
36