Tuesday, May 31, 2022

AWS Deployment

There are two popular editors that you can run in a terminal: vim and emacs but this time we will use vim.

example: 

MacBook-Pro:~$ vim ./poge OR $ vim poge

Basic principles

-When editing, press i to enter edit mode. 

-When moving the cursor or executing a vim command, press the esc key to enter normal mode.

-To save the file, enter normal mode, enter :wq and press enter. 

-If you want to open the file and close it without doing anything, enter normal mode, type :q! and press enter.

How to check permissions

The command, ls -l or ls -la can be used to check what permissions a file or a folder has.

===============================================

LET'S START

$ cd .ssh

$ chmod 400 achieve-key.pem (downloaded key pair)


SSH to EC2 using a key pair

$ cd .ssh

$ ssh -i "achieve-key.pem" ec2-user@13.113.254.221 (example)


AWS EC2 instance initial settings

1. Switch to administrator privileges

[ec2-user@ip-1-250 ~]$ sudo su -

[root@ip-1-250 ~]#


2.Update package management software

Implement at EC2

# yum update -y


3. Time zone correction

Implement at EC2

ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# vi /etc/sysconfig/clock


You can edit the file by pressing i to enter insert mode.


ZONE="Asia/Tokyo"

UTC=false


Exit the insert mode by pressing the esc key, type :wq and hit Enter.


4. Change language information

Implement at EC2

# vi /etc/sysconfig/i18n


LANG=ja_JP.UTF-8


5. Settings to optimize for the environment of your country

Implement at EC2

# vi /etc/cloud/cloud.cfg


Change repo_upgrade: security to repo_upgrade: none

Also newly add locale: ja_JP.UTF-8


6. Create swap area

Implement at EC2

# dd if=/dev/zero of=/swapfile bs=1M count=1024

# mkswap /swapfile

# swapon /swapfile

# vi /etc/fstab


After opening the file, add the following

/swapfile swap swap defaults 0 0


7. Create new users

Implement at EC2

# useradd app -G wheel

# visudo


When the file opens, remove # at the head of #% wheel ALL = (ALL) NOPASSWORD: ALL


SSH connection to AWS

1. Copy public key

The public key is saved in id_rsa.pub file.


Let's log out of EC2

# exit

$ exit


Perform the following locally

$ cat ~/.ssh/id_rsa.pub


Copy the public key displayed.

What to copy starts with ssh-rsa and finishes with the end of the email address.


2. Set public key on EC2

Implement at EC2

$ sudo su app

$ cd ~

Create #ssh directory (omitted if it already exists)

$ mkdir .ssh

#Give yourself only the right to execute

$ chmod 700 .ssh

Move to #ssh directory

$ cd .ssh

Create #authorized_keys file

$ touch authorized_keys

#Give edit permission to only yourself for the file

$ chmod 600 authorized_keys

Open the #authorized_keys file

$ vi authorized_keys


If you can open the file, add the public key you just copied into this file.

After editing the file, leave the AWS environment with exit.


3. Connect SSH to EC2 using the copied public key

Perform the following locally

$ cd ~/.ssh

$ ssh -i "/Users/Akihiro/.ssh/id_rsa" app@1.234.567.89 (example)


If you can connect as the app user, what you used to log in as ec2-user can now log in as app as shown below.


[app@ip-172-31-50 ~]$


This completes SSH connection.


4. Checking time zone and time

Implement at EC2

[app@ip-172-31-50 ~]$ date


Next, restart crond to correct the time difference of crond.

Implement at EC2

$ sudo service crond restart

$ sudo reboot

If you log out of EC2 here, log in again.


5. Remove ec2-user

Implement at EC2

$ sudo su -

# userdel ec2-user


Various installations on EC2

1. Install git

Implement at EC2

$ sudo su -

# yum -y install git

# git --version


2. Install rbenv

Implement at EC2

# git clone https://github.com/rbenv/rbenv.git /usr/local/rbenv

(Create a new file called rbenv.sh file)

# vi /etc/profile.d/rbenv.sh


Describe the following in the opened file.

export RBENV_ROOT=/usr/local/rbenv

export PATH="$RBENV_ROOT/bin:$PATH"

eval "$(rbenv init -)"


# source /etc/profile.d/rbenv.sh

# rbenv --version


3. Installation of ruby-build

Implement at EC2

# git clone https://github.com/rbenv/ruby-build.git /usr/local/rbenv/plugins/ruby-build


4. Check the list of Ruby versions that can be installed with rbenv

Implement at EC2

# rbenv install -list


5. Install the tools and libraries for installing Ruby

Implement at EC2

# yum -y groupinstall "Development Tools"

# yum -y install gcc-c++ glibc-headers openssl-devel readline libyaml-devel zlib zlib-devel libffi-devel libxml2 libxslt libxml2-devel libxslt-devel mysql-devel readline-devel

# yum -y install ImageMagick ImageMagick-devel


6. Install Ruby

Implement at EC2 (Install the version of Ruby you are currently using locally.)

# rbenv install 3.0.1

(It takes a while)


7. Check if Ruby can be installed on EC2

Implement at EC2

# rbenv versions


8. Change the version of Ruby used in EC2

Use the command rbenv global to switch the Ruby version of the entire system.

Now, let's switch the ruby version.

Implement at EC2

# rbenv global 3.0.1

# ruby -v


Check the version of bundler by running the following command on both EC2 and your local application directory.


$ bundler -v


Implement at EC2

# gem install bundler -v 2.3.13 (example)

# exit


Implement at EC2

$ cd /usr/local/rbenv/

$ sudo chown app version

$ sudo chown app shims

$ ls -la 


Implement at EC2

$ source /etc/profile.d/rbenv.sh

$ rbenv global 3.0.1


If you can execute the above command without any problem, check the version of ruby.

$ ruby -v


9. Install node.js on EC2

Check the version of nvm and node.js locally

Implement at local

$ nvm --version

$ node -v


Specify the version of nvm confirmed locally in the "Version" part below and install it.

Implement at EC2

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash (example)

$ . ~/.nvm/nvm.sh

$ nvm install v14.17.3 (example)

$ node -e "console.log('Running Node.js ' + process.version)"

Running Node.js v14.17.3


10. Installing yarn on EC2

Implement at EC2

$ npm install --global yarn

$ yarn -v


Database Settings EC2

1. Install PostgreSQL

Implement at EC2

$ sudo su -

# amazon-linux-extras install postgresql11 -y

# yum install postgresql-server postgresql-devel -y

# psql --version


2. Initial PostgreSQL Settings

Implement at EC2

# postgresql-setup initdb


3. Starting PostgreSQL

Implement at EC2

# systemctl start postgresql.service


4. Edit Settings file

Implement at EC2

# vi /var/lib/pgsql/data/postgresql.conf

(remove the # at the beginning and edit the setting from 'localhost' to '*')

#------------------------------------------------- ----------------------------- 

 # CONNECTIONS AND AUTHENTICATION 

 #------------------------------------------------- ----------------------------- 

 #-Connection Settings- 

 listen_addresses ='*' # what IP address(es) to listen on; 

 # comma-separated list of addresses; 

 # defaults to'localhost'; use'*' for all 

 # (change requires restart) 

 #port = 5432 # (change requires restart)


Implement at EC2

# vi /var/lib/pgsql/data/pg_hba.conf

At the bottom of this file, add the following:

local all all trust

host all all 0.0.0.0/0 md5


Also comment out the lower part of the file as follows (add a # at the beginning).

# "local" is for Unix domain socket connections only

To comment→ # local all all peer

# IPv4 local connections:

To comment→ # host all all 127.0.0.1/32 ident

# IPv6 local connections:

To comment→ # host all all ::1/128 ident


When you have finished editing, please save the file with :wq .


Implement at EC2

# systemctl restart postgresql.service


Create user and database

Implement at EC2

# psql -U postgres

※​When connecting to PostgreSQL, the prompt will be postgres=#


[Important]

In PostgreSQL, we use the concept of roleto manage permissions on the database, but there are points to note here.

The role name and database name you are going to create must match the username and database names in the production settings written in your app's config/database.yml.



postgres=# CREATE ROLE "sample" WITH SUPERUSER LOGIN;

CREATE ROLE


postgres=# CREATE DATABASE "sample_production";

CREATE DATABASE


postgres=# \q


Finally, restart PostgreSQL

# systemctl restart postgresql.service


Web server and application server

1. Installation of the web server (Nginx)

Implement at EC2

$ sudo su -

# amazon-linux-extras install nginx1 -y


2. Autostart Settings and start confirmation

Implement at EC2

# chkconfig nginx on


Execute the following command to start Nginx

# systemctl start nginx

No comments:

Post a Comment