[Ruby] EC2 initial settings

7 minute read

Introduction

The procedure for “using the AWS server” is divided into 5 parts.

Please check the articles as summarized below.

EC2 initial settings ← Imakoco ② Create database in production environmentStart Rails of EC2Set Web ServerAutomate deployment

AWS environment construction

Log in to your AWS account and set the “Region” next to your account to Tokyo.

Create EC2 instance

An EC2 instance is what is called a “virtual machine” in AWS. Even if you say “generate a server”, AWS does not physically prepare all servers, Actually, the software called “virtual machine” is used to utilize a mechanism that allows virtually one Linux server to be used.

After logging in to your AWS account and going to the top page, select “EC2” from “Services” in the upper left. Image from Gyazo

Click “Start Instance” Image from Gyazo


Next, select “AMI”. AMI is an abbreviation for “Amazon Machine Image”, which is data that stores the entire server data. Some of these have “OS” installed in advance, so you can save yourself the trouble of installing various tools from scratch.

Select “Amazon Linux 2 AMI” Image from Gyazo

Then select “EC2 Instance Type” This time, select “t2.micro” that can be used in the free frame and click “Confirm and create” Image from Gyazo

After the screen transition, click “Start” in the lower right


Next, download a “key pair”. A key pair is a “private key” required to log in to an instance.

Select Create new key pair and enter a name for your key pair Then click “Download Key Pair” Image from Gyazo

When the key pair was downloaded, it was in a state where it could not be clicked “Create Instance” is changed to be clickable. Click there to create an EC2 instance. Then it will transition to the completion screen.

If you click “View Instance” in the lower right, you will be returned to the “Instance list screen”. Copy the created “Instance ID” and write it down in a memo app. Image from Gyazo


Preparing to start the server

The IP address is automatically assigned to the “EC2 instance” created earlier, when it is created, The drawback is that this public IP will change every time you restart the server.

“Elastic IP” solves this problem.

Elastic IP is a fixed “public IP address” assigned by AWS. By linking this public IP address to the EC2 instance, regardless of whether the instance is started or stopped It is possible to always communicate with the same IP address.

Click “Elastic IP” from “Network Security” on the left side, Click “Elastic IP address assignment” on the upper right Image from Gyazo

Select “Amazon IPv4 address pool when the screen transitions” and click “Assign”

Next, link the “acquired Elastic IP address” and “EC2 instance”.

Click Actions on the top right and select Associate Elastic IP Address [Image from Gyazo]] (https://gyazo.com/4f12c9f892752cb684b6296f9dd02669)

When the screen transitions, select the instance you made a note of in the instance search window Leave “Private IP Address” blank and click “Associate” in the lower right Image from Gyazo.

Return to the instance list screen again and confirm that the “Elastic IP” of the created instance is set. At the bottom right of the image, “Elastic IP” is set. Image from Gyazo


EC2 instance setting change

The EC2 instance just launched can be accessed with “ssh”, It does not connect to any other connection such as HTTP. Therefore, the EC2 instance used as a server is It is necessary to open the “port” in advance so that HTTP can be connected.

“Ssh” is used when you want to work in a server in a remote place. You can securely communicate with the remote server using encryption and authentication technology.

Set security group port

Select the created instance from the EC2 instance list screen, Click the “Security Group” link (“launch-wizard-1” in the figure) Image from Gyazo

Since it transits to the setting screen of the security group to which the instance belongs, Check “launch-wizard-1” displayed in the security group. Click “Inbound Rules” and then click “Edit Inbound Rules” in the upper right. Image from Gyazo

Since the page switches, click “Add rule” Select “HTTP” as the type and check the information such as the port number. Select “0.0.0./0” from the mark next to the custom magnifying glass. After selecting, select “::/0” again from the magnifying glass.

*Be careful not to lose the existing “ssh settings”

Finally, press “Save Rule” in the lower right and the opening of the port is completed


Login to EC2 instance

When you create an EC2 instance, the ssh private key corresponding to the user “ec2-user” is generated.

Run the following command in the terminal

% cd ~
% mkdir ~/.ssh

Create a directory called #.ssh
The .ssh directory exists even if the error #File exists is displayed.
% mv Downloads/ Downloaded key name .pem .ssh/

Move the downloaded pem file from the download directory to the .ssh directory with the #mv command
# In the "Downloaded key name" part, find the file "~.pem" in the download folder with the Finder and replace it with the name of the "~" part.
% cd .ssh/
% ls

# Check if pem file exists
% chmod 600 Name of the downloaded key.pem

# Look for "Downloaded key name" in the download folder
% ssh -i Name of the downloaded key.pem [email protected] Elastic IP associated with the created EC2 instance

# Check the "Elastic IP associated with the created EC2 instance" from the AWS instance

When ssh connection (the following command) is executed, the following message may be displayed, but enter “yes” and execute.

EC2 Amazon Linux AMI

If it is displayed as big, it is successful.


Install the tools required for setting

First, use the command yum to update the programs that are originally on this server.

The “yum command” is a software management mechanism in Linux, and plays the same role as homebrew for MacOS.By using the yum command, you can manage the version of programs under the control of yum, You can update all at once.

A “package” is a set of programs with a certain role/function under Linux OS. Also called “software” or “library”.

Update the package by executing the following command in the terminal (inside EC2)

[[email protected]***-**-**-*** ~]$ sudo yum -y update

Next, install various packages necessary for environment construction (long on the right)

[[email protected]***-**-**-*** ~]$ sudo yum -y install git make gcc-c++ patch libyaml-devel libffi-devel libicu-devel zlib-devel readline-devel libxml2 -devel libxslt-devel ImageMagick ImageMagick-devel openssl-devel libcurl libcurl-devel curl
  • y is attached. “-Y option” is an option of the yum command. Execute the command with the setting “Answer all questions automatically”. Do you really want to install it with a command such as yum install? Since y/n is asked, it can be omitted.

Next, install Node.js to run JavaScript on EC2.

“Node.js” is a JavaScript package that runs on the server side. It will be used when compressing CSS and images in the work for future deployment.

Execute the following command in the terminal (inside EC2)

[[email protected]***-**-**-*** ~]$ sudo curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash-
[[email protected]***-**-**-*** ~]$ sudo yum -y install nodejs
  • If a confirmation screen appears, enter “Y” and press the Enter key to complete the installation.

Preparation for running applications with Rails version 6.0 or later

You need to install Yarn to run applications with Rails version 6.0 or later.

Execute the following command in the terminal (inside EC2)

[[email protected]***-**-**-*** ~]$ sudo yum -y install wget
[[email protected]***-**-**-*** ~]$ sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d /yarn.repo
[[email protected]***-**-**-*** ~]$ sudo yum -y install yarn

Install rbenv and ruby-build

These need to be installed before installing Ruby, Tools used in combination to manage Ruby versions

Execute the following command in the terminal (inside EC2)

# ① Install rbenv
[[email protected]***-**-**-*** ~]$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

# ② Pass
[[email protected]***-**-**-*** ~]$ echo'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

# ③ Description to call rbenv
[[email protected]***-**-**-*** ~]$ echo'eval "$(rbenv init -)"' >> ~/.bash_profile

# ④ Read .bash_profile
[[email protected]***-**-**-*** ~]$ source .bash_profile

# ⑤ Install ruby-build
[[email protected]***-**-**-*** ~]$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

# ⑥ do rehash
[[email protected]***-**-**-*** ~]$ rbenv rehash

Command role ① Clone “rbenv” from git ② & ③ Command required to pass the path ④ Read the set path ⑤ Clone “ruby-build” from git ⑥ Command required to use gem commands in the Ruby version you are using


Install Ruby

The commands to execute below are “Version of Ruby to install” “The version of Ruby used in my application” Please change accordingly.

Here, “2.6.5” will be installed.

Execute the following command in the terminal (inside EC2)

# Install Ruby 2.6.5 version
[[email protected]***-**-**-*** ~]$ rbenv install 2.6.5

# Decide which Ruby version to use in the EC2 instance
[[email protected]***-**-**-*** ~]$ rbenv global 2.6.5

# rehash
[[email protected]***-**-**-*** ~]$ rbenv rehash

# Check Ruby version
[[email protected]***-**-**-*** ~]$ ruby -v

that’s all. Thank you for your hard work.

Continued → here