1
1

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 3 years have passed since last update.

【AWS】The procedure to deploy on EC2

Last updated at Posted at 2020-05-17

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

$ 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

$ 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

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

###Install Bundler


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

###Create a project on GitHub
###Push to GitHub

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


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

###Install Rails

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

The below command executed due to an error occurred.

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

###Install postgreSQL

 
$ yum install postgresql
$ sudo yum install postgresql-devel                   

To avoid the SQLite3's error

 
$ 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


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

###Configure PostgreSQL on the EC2 instance


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                     
$ rails db:create
$ rails db:migrate                    

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

EMAIL_ADDRESS
EMAIL_PASSWORD

After that, execute the below command to reflect it.

$ 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

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

###Restart the project and start Sidekiq

$ bundle exec sidekiq                      

###Install Nginx

$ sudo yum install nginx
                   

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


$ 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

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

###Start Nginx

$ sudo service nginx start                     

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

###Modify the permission of the home directory to 744(drwxr--r--)

$ chmod 755 ~/                     

###Restart Nginx

$ sudo service nginx restart
                     

###Create DB on the production environment

$ rails db:migrate RAILS_ENV=production                      

###Restart Unicorn

$ 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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?