【AWS】The procedure to deploy on EC2

# Introduction

I am on my way to get a Ruby developer.
This article is a note for me, but I would be happy if helpful for  someone.

##Environment
- macOS10
- Rails 5.2.3
- Nginx
- Unicorn
- Postgresql
- Sidekiq
- Redis

###Create a EC2 instance

- Select Amazon Linux AMI 2018.03.0(HVM,SSD Volume Type.
- Select t2.micro(for within free tier)
- Create a key pair.

↓
Public DNS generated.
(This article referred: 
https://qiita.com/Quikky/items/2897573a42fd71cfc47f)

###Prepare to install rbenv

```console
$ 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
```


###Install rbenv

```console
$ 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
```

###Install Ruby

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

###Install Bundler

```console

$ rbenv exec gem install bundler -v 2.0.1
$ rbenv rehash
```
###Create a project on GitHub
###Push to GitHub

```console
On Mac
$ git remote add origin <URL Address>                    
$ git add .                                          
$ git commit -m "1st push"                           
$ git push -u origin master                          
```

###Pull from GitHub to EC2

```console

$ git clone <URL Address>
$ cd <The project's directory>                       
```

###Install Rails

```console
$ gem i -v 5.2.3 rails
$ gem list rails                     
```

```console
$ bundle install                   
```

The below command executed due to an error occurred.

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

###Install postgreSQL

```console
 
$ yum install postgresql
$ sudo yum install postgresql-devel                   
```


To avoid the SQLite3's error

```console
 
$ sudo yum install sqlite-devel                    
```

###Configure instance's security group inbound


- Configure Custom TCP/IP port　3000 0.0.0.0/0 additionally.



###Start PostgreSQL on the EC2 instance

```console

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

###Configure PostgreSQL on the EC2 instance


```console

Master username：<the project's name>
Master password：<password>
postgres=# create role <username> with createdb login password '<password>';
postgres=# create database [database_name] owner [user_name];

$ sudo -u postgres psql
postgres=# create role <The project's name> with createdb login password '<password>';
postgres=# create database <the project's name>_production owner <The project's name>;
postgres=# create database <the project's name>_development owner <the project's name>;
postgres=# create database <the project's name>_test owner <the project's name>;
postgres=# \q                 
```

###Create DB（Postgres）

```
.bash_profile
export POSTGRES_USERNAME="<The project's name>"
export POSTGRES_PASSWORD="<Password>"

The project's directory
$ rails db:create                     
```

###

```
 /var/lib/pgsql9/data/pg_hba.conf

local all all peer

　　　　　↓

local all all md5

Modified                     
```

```console
$ rails db:create
$ rails db:migrate                    
```

###Configure SMTP
Configure the environment variable in .bash_profile.

```console
EMAIL_ADDRESS
EMAIL_PASSWORD
```

After that, execute the below command to reflect it.

```console
$ source .bash_profile                     
```

###


- Launch Redis server on EC2.
- Select ElasticCache.
- Press 'Create'.
 

  Primary endpoint generated.               


###Select 'security group' for Redis's inbound configuration on VPC configuration.

- Add it on the inbound edition of sg-694f3715.
- Custom TCP：TCP
- port：6379
- source 0.0.0.0/0

###Configure the below files

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

###Restart the project and start Sidekiq

```console
$ bundle exec sidekiq                      
```

###Install Nginx

```console
$ sudo yum install nginx
                   
```

###Copy /usr/local/etc/nginx/nginx.conf which is on Mac to EC2

```console

$ scp -i ~/.ssh/<The project name名> nginx.conf ec2-user@XXXXXXXXXXXXXXXXXXXXX.amazonaws.com:~/nginx.conf
$ sudo cp ~/nginx.conf /etc/nginx/
                       
```
Modify the port number and root directory.

###Start Unicorn

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

###Start Nginx

```console
$ sudo service nginx start                     
```

###Add port80(HTTP) for VPS configuration.

###Modify the permission of the home directory to 744(drwxr--r--)
```console
$ chmod 755 ~/                     
```

###Restart Nginx

```console
$ sudo service nginx restart
                     
```

###Create DB on the production environment

```console
$ rails db:migrate RAILS_ENV=production                      
```

###Restart Unicorn

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

An error occured in the asset pipeline.

```

Modify config.assets.compile from false to true in config/environments/production.rb                  
```
↓↓↓↓↓↓↓ My detail
*https://kakuyon.hatenablog.com/entry/2018/07/15/03
3059 referred.
───────
*https://kakuyon.hatenablog.com/entry/2018/07/15/033059 referred.
↑↑↑↑↑↑↑ The detail of the edit request