[Part 2] Let's build a web server on EC2 Linux

【procedure】

--Step 1. Create an EC2 Linux instance --Step 2. Prepare the web server --Step 3. Test the web server --Step 4. Create ALB and try to configure Multi AZ

[Purpose of learning]

Learn the following items

--What is EC2? --What is ALB? --Similar services to ALB, and their differences --What is Multi-AZ?

[What is EC2]

Amazon Elastic Compute Cloud (Amazon EC2)

EC2 is a "virtual server" provided by AWS. You can prepare an environment where you can immediately run virtual servers of various OS such as Linux, Windows, and Red Hat.

EC2 can be built in units called instances. There are multiple types of instances. For example, in the case of a small type instance, the CPU and memory are fixed, such as CPU1 and memory 1GB. It is not possible to add only CPU or only memory, and if you want to increase the specifications, you need to change the instance type. (Like small → medium)

[What is ALB]

ALB (Application Load Balancer) is a part of the system called AWS (Amazon Web Services) provided by Amazon.com, and is a load balancing service that distributes the load generated by Web services.

[Difference between ALB and ELB]

ELB is an abbreviation of "Elastic Load Balancing", and this ELB was originally a load balancing service on AWS. However, when ALB was later developed as an additional option, ELB would change its name to "Classic Load Balancer (CLB)". And ELB came to be used as a general term for the services of ALB and CLB.

[Characteristics of ALB]

--Behavior on layer 7 (application layer) --New support for WebSocket and HTTP / 2 --For the latest application architecture --Can route to target group --Using multiple Availability Zones

ALB has many distinctive features compared to CLB, and has the charm of ALB.

[What is Multi-AZ]

Assumption: What is Availability Zone?

A data center that is physically separated within each region (Tokyo, Oregon, Frankfurt, etc.). In the Tokyo region

  • ap-northeast-1a
  • ap-northeast-1b
  • ap-northeast-1c

There are three Availability Zones. Also, it seems that the region of user A and the region of user B are not always the same.

What is Multi-AZ

Build an application for one AZ (EC2, RDS, etc.) For Single-AZ Building for multiple AZs is often called Multi-AZ.

[Step 1. Create an EC2 Linux instance]

1. Create a key pair creation

What is a key pair

Public and private keys are called key pairs. Amazon EC2 uses public key cryptography to encrypt and decrypt your login information. Public key cryptography uses a public key to encrypt data, and recipients use a private key to decrypt data. Amazon EC2 stores only public keys, and users store private keys. Users must specify a private key when sshing to the instance.

step1_1_KeyPair.PNG

2. Create a security group and add inbound rules

Create a security group

Added TCP / 80, TCP / 22 to inbound rules

--TCP / 80: Rule to allow HTTP communication (Source: set "0.0.0.0/0") --TCP / 22: Rule to allow ssh connection (Source: set "My IP"). The IP address you are currently accessing The dress is set automatically

b. Display the EC2 Management Console
c. Select an AMI
  1. Select "Instance" in the left pane and select "Create Instance".
  2. In Step 1: Amazon Machine Image (AMI), select Amazon Linux 2 AMI

step1_2_3_chooseAMI.PNG

d. Select an instance type

Select instance type t2.micro

e. Make detailed instance settings

Enable default VPC, default Subnet, and auto-assigned public IP

  1. Network: vpc-xxxx | Default
  2. Subnet: subnet-YYYY | default in ap-northeast-1a
  3. Automatically assigned public IP: Valid
f. Add storage and tag

No particular changes

g. Set up security groups

Select a pre-created security group

h. Launch an instance
i. Specify the key pair

In the key pair selection pop-up, select "Select existing key pair" and select the created key pair name.

j. Check the instance being created

Select "Instance" in the left pane and check the status of the instance being created.

3. Create an Amazon Linux instance and make sure you can ssh it

Check the instance information required for connection

Refrain from public DNS (IPv4) for ssh connection Example.) Ec2-18-183-224-68.ap-northeast-1.compute.amazonaws.com

step1_3_1_checkPublicDNS(IPv4).PNG

b. Connect with ssh (using Teraterm)

[Step 2. Prepare the Web server]

1. Install Apache and PHP

Type the following code on TeraTerm connected by SSH

$ sudo yum -y update
$ sudo amazon-linux-extras install -y php7.2
$ sudo yum install -y httpd
$ sudo systemctl start httpd
$ sudo systemctl enable httpd

2. Go to "http : // EC2 instance public DNS" and verify that the Apache test page is displayed

Example.) Ec2-52-193-98-49.ap-northeast-1.compute.amazonaws.com

step2_2_CheckApacheTestPage.PNG

3. Check Apache settings

Check the document root and port from the Apache configuration file /etc/httpd/conf/httpd.conf

--DocumentRoot: DocumentRoot "/ var / www / html"

--Listing port: Listen 80

[Step 3. Test the web server]

1. Set ec2-user user privileges

a. Add ec2-user to the apache group to grant permission to edit files under the Apache document root
b. Log back in to see if the user belongs to the apache group
c. Change group ownership of / var / www / html and its contents to apache group

2. Test PHP

Create a PHP test file with the following command.
$ echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
b. Access the page you created. Set it to "http : // EC2 instance public DNS name /phpinfo.php" and check if it is displayed.

Example.) Http://ec2-52-193-98-49.ap-northeast-1.compute.amazonaws.com/phpinfo.php

c. Delete the test page
$ rm /var/www/html/phpinfo.php

3. Create a test page

Create a PHP file in the document root
b. Display the current time in the specified format, /var/www/html/index.php
<html>
	<head>
Let's build a web server on EC2 Linux </ title> </head> <body> <h1>Hello World! </h1><br/> <?php echo date('Y-m-d H:i:s'); ?> </body> </html> <h3>4. Check the test page</h3> <p>Go to "http : // EC2 instance public DNS" and verify that index.php is displayed. Example.) Http://ec2-52-193-98-49.ap-northeast-1.compute.amazonaws.com When you press the refresh button on your browser, check that the displayed time changes to the current time.</p> <h2>[Step 4. Create ALB and try to configure Multi AZ]</h2> <p>Overview <img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/231057/6222bbae-f287-6019-bc1a-21d087d876b5.png" alt="step4_MultiAZSummary.PNG" /></p> <h3>1. Create an ALB with two Availability Zones (AZs) and target your EC2 instances</h3> <h5>Create a target group from EC2 Management Console> Target Group</h5> <h6>What is a target group?</h6> <blockquote> <p>Target groups are used to route requests to one or more registered targets. Specify target groups and conditions when creating rules for each listener. When the rule conditions are met, traffic is forwarded to the appropriate target group.</p> </blockquote> <h5>b. Register the instance with the target</h5> <h5>c. EC2 Management Console> Create an ALB from the load balancer</h5> <h5>d. Configure load balancer / security group / routing</h5> <h3>2. Access "http : // ALB DNS name" and check that index.php is displayed.</h3> <p>Example) http://webalb-781715948.ap-northeast-1.elb.amazonaws.com</p> <h3>3. Create an AMI for the EC2 instance with the WEB server built</h3> <h3>4. Create a new instance from the AMI, create an instance in a different AZ, and then add it to the ELB target.</h3> <h3>5. Repeatedly access "http : // ALB DNS name" and verify that access is distributed across the two EC2 instances.</h3> <p>Example) [Instance A] [ec2-user@ip-10-0-0-155 ~]$ sudo tail -f /var/log/httpd/access_log 10.0.2.106 - - [16/Apr/2020:04:06:03 +0000] "GET / HTTP/1.1" 200 168 "-" "curl/7.54.0"</p> <p>[Instance B] [ec2-user@ip-10-0-2-254 ~] $ sudo tail -f /var/log/httpd/access_log 10.0.2.106 - - [16/Apr/2020:04:06:05 +0000] "GET / HTTP/1.1" 200 168 "-" "curl/7.54.0"</p> <h2>[Considerations for next issue]</h2> <p>--Creating a development environment that allows you to edit files on EC2 via Visual Studio</p> <p>(Learning VS Code "Remote Development"?)</p> <p>--Or about how to set Route 53</p> <h2>[Reference]</h2> <h3>What is EC2</h3> <ul> <li><a href="https://www.slideshare.net/AmazonWebServicesJapan/20190305-aws-black-belt-online-seminar-amazon-ec2">AWS Black Belt Online Seminar Amazon EC2</a> -<a href="https://recipe.kc-cloud.jp/archives/7413">EC2 in 5 minutes</a></li> </ul> <h3>What is ALB</h3> <ul> <li><a href="https://www.slideshare.net/AmazonWebServicesJapan/application-load-balancer">Application Load Balancer</a> -<a href="https://www.wafcharm.com/blog/alb-for-beginners/">[What is ALB? ]</a> -[[Difference between ALB and ELB]](https://www.wafcharm.com/blog/difference-between-alb-and-elb/#:~:text=ALB%EF%BC%88Application%20Load%20Balancer % EF% BC% 89% E3% 81% A8,% E3% 81% 99% E3% 82% 8B% E3% 83% AD% E3% 83% BC% E3% 83% 89% E3% 83% 90% E3% 83% A9% E3% 83% B3% E3% 82% B7% E3% 83% B3% E3% 82% B0% E3% 82% B5% E3% 83% BC% E3% 83% 93% E3% 82% B9% E3% 81% A7% E3% 81% 99% E3% 80% 82)</li> </ul> <h3>What is Multi-AZ?</h3> <p>-<a href="http://natural-hokke.hateblo.jp/entry/2014/10/24/133205">I checked AWS MultiAZ, so I will leave it</a> -<a href="https://www.skyarch.net/blog/?p=7648">What is Multi-AZ? </a></p> <h3>Step1. Create an EC2 Linux instance</h3> <p>-<a href="https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/ec2-key-pairs.html">Amazon EC2 Key Pair</a> --<a href="https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/ec2-security-groups.html">Amazon EC2 Security Groups for Linux Instances</a> -<a href="https://dev.classmethod.jp/articles/aws-beginner-ec2-ssh/">Ssh connection to Amazon EC2</a></p> <h3>Step2. Prepare the web server</h3> <p>--<a href="https://httpd.apache.org/docs/2.4/ja/urlmapping.html#documentroot">Apache HTTP Server --DocumentRoot Directive</a> --<a href="https://httpd.apache.org/docs/2.4/ja/bind.html">Apache HTTP Server --Listen Directive</a></p> <h3>Step3. Test the web server</h3> <ul> <li><a href="https://www.php.net/manual/ja/function.date.php">PHP: date - Manual</a> --<a href="https://www.atmarkit.co.jp/ait/articles/1706/02/news014.html">Linux Users and Groups</a></li> </ul> <h3>Step4. Create ALB and try to configure Multi AZ</h3> <p>-[Create an AMI from an Amazon EC2 instance](https://docs.aws.amazon.com/ja_jp/toolkit-for-visual-studio/latest/user-guide/tkv-create-ami-from-instance. html) -<a href="https://docs.aws.amazon.com/ja_jp/elasticloadbalancing/latest/application/create-target-group.html">Create Target Group</a> -<a href="https://docs.aws.amazon.com/ja_jp/elasticloadbalancing/latest/application/target-group-register-targets.html">Register target in target group</a> -<a href="https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/create-application-load-balancer.html">Create Application Load Balancer</a></p> <!-- ENDDDDDDDDDDDDDDDDDDDDDDDDDDDDD --> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- post_new_ui_horiz --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5469278205356604" data-ad-slot="4209814965" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div style="margin-top: 30px;"> <div class="link-top" style="margin-top: 1px;"></div> <p> <font size="4">Recommended Posts</font> <!-- BEGIN LINK ************************* --> <div style="margin-top: 10px;"> <a href="/en/70c169aba1eaaab99c7b">[Part 2] Let's build a web server on EC2 Linux</a> </div> <div style="margin-top: 10px;"> <a href="/en/950b27977dfbd1d9e132">[Part 1] Let's set up a Minecraft server on Linux</a> </div> <div style="margin-top: 10px;"> <a href="/en/5b596b76ef7dc1be9a39">Build a simple WebDAV server on Linux</a> </div> <div style="margin-top: 10px;"> <a href="/en/90f4c335cb6e94ef46e2">Build a Samba server on Arch Linux</a> </div> <div style="margin-top: 10px;"> <a href="/en/b389419c220da10fbb17">Build a web server on your Chromebook</a> </div> <div style="margin-top: 10px;"> <a href="/en/daaf8ff8feb50ad04f41">Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server ―― 1. Apache introduction</a> </div> <div style="margin-top: 10px;"> <a href="/en/fca116b1b387fe56c93c">Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server --2 PHP introduction</a> </div> <div style="margin-top: 10px;"> <a href="/en/61d01a4a62c42a52c9a0">Run a Linux server on GCP</a> </div> <div style="margin-top: 10px;"> <a href="/en/76807c637c096618a0e1">Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux to build a Web server --3 MySQL 8.0 introduction</a> </div> <div style="margin-top: 10px;"> <a href="/en/082af79bdf9a381a1d1b">Build a Pypi cache server on QNAP</a> </div> <div style="margin-top: 10px;"> <a href="/en/50219cd43e51c99ead34"># 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2</a> </div> <div style="margin-top: 10px;"> <a href="/en/55e2eec044e06ca30946">Build an NFS server on Arch Linux</a> </div> <div style="margin-top: 10px;"> <a href="/en/f9cf37cfc359aa893797">Install Python3 and Django on Amazon Linux (EC2) and run your web server</a> </div> <div style="margin-top: 10px;"> <a href="/en/a76f88e2026bb8b57b5f">Build Oracle Database 19c on Oracle Linux 8.3 (DB Build Part 2)</a> </div> <div style="margin-top: 10px;"> <a href="/en/35ce32f7935b394cec90"># 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)</a> </div> <div style="margin-top: 10px;"> <a href="/en/7992bfdaf3c87b87e741">Build Apache HTTP Server and Wildfly on Oracle Linux 8</a> </div> <div style="margin-top: 10px;"> <a href="/en/a1830cfae6a1fa121ad9">CTF beginner tried to build a problem server (web) [Problem]</a> </div> <div style="margin-top: 10px;"> <a href="/en/e45e9e084076d7e8b09b">How to build a Python environment on amazon linux 2</a> </div> <div style="margin-top: 10px;"> <a href="/en/3a195842a89b5e7850c5">Build a server on Linux and local network with Raspberry Pi NextCloud and desktop sharing</a> </div> <div style="margin-top: 10px;"> <a href="/en/0d8cf32403b99bd3914a">Install LAMP on Amazon Linux 2 and build a WordPress environment.</a> </div> <div style="margin-top: 10px;"> <a href="/en/2c068eab3b34336147fb">SSH connection to a private server using a bastion server on EC2</a> </div> <div style="margin-top: 10px;"> <a href="/en/499acd26d16cfac02759">Yes, let's make a Minecraft server (Oracle Linux + Spigot + Geyser)</a> </div> <div style="margin-top: 10px;"> <a href="/en/4f363cbca8c415862e22">Build a web API server at explosive speed using hug</a> </div> <div style="margin-top: 10px;"> <a href="/en/913bb815d8d419148c33">Build a speed of light web API server with Falcon</a> </div> <div style="margin-top: 10px;"> <a href="/en/938e2a4b1eb0d53f14d5">[Introduction to AWS] A memorandum of building a web server on AWS</a> </div> <div style="margin-top: 10px;"> <a href="/en/eea72012fa064b1ba71b">Build a python environment on CentOS 7.7 for your home server</a> </div> <div style="margin-top: 10px;"> <a href="/en/19ec98080ae6081e4fa8">Linux Web server construction (Ubuntu & Apache)</a> </div> <div style="margin-top: 10px;"> <a href="/en/1f2560123bf740f2d361">Let's make a WEB application for phone book with flask Part 1</a> </div> <div style="margin-top: 10px;"> <a href="/en/371d2cc790f9d7bec505">Build a Selenium environment on Amazon Linux 2 in the shortest time</a> </div> <div style="margin-top: 10px;"> <a href="/en/418908e290b54732968f">Build a Flask / Bottle-like web application on AWS Lambda with Chalice</a> </div> <div style="margin-top: 10px;"> <a href="/en/55f06405da8bba14cf8a">Install Arch Linux on DeskMini A300</a> </div> <div style="margin-top: 10px;"> <a href="/en/5bf408bf95220930afdb">Let's make a WEB application for phone book with flask Part 2</a> </div> <div style="margin-top: 10px;"> <a href="/en/6a50e04233a724d5b4df">Install pyenv on EC2 (Amazon Linux)</a> </div> <div style="margin-top: 10px;"> <a href="/en/81d5966986e71e62bbbe">Create a Linux environment on Windows 10</a> </div> <div style="margin-top: 10px;"> <a href="/en/9cd0ca5cda7f459533c3">Build a web application with Django</a> </div> <div style="margin-top: 10px;"> <a href="/en/a8be387eb315f7d17474">Let's make a WEB application for phone book with flask Part 3</a> </div> <div style="margin-top: 10px;"> <a href="/en/b4c3eafd1dbee38526a5">Run TensorFlow2 on a VPS server</a> </div> <div style="margin-top: 10px;"> <a href="/en/c70c34639c0096930a62">Let's make a WEB application for phone book with flask Part 4</a> </div> <div style="margin-top: 10px;"> <a href="/en/d8b6dbb3c6d76a5bcba6">Build a python3 environment on CentOS7</a> </div> <div style="margin-top: 10px;"> <a href="/en/e60476f494824a7138df">Host the network library Mirror for Unity on a Linux server</a> </div> <div style="margin-top: 10px;"> <a href="/en/ffff9f0a8548c8c1481c">What I stumbled upon when using CodeIgniter on a Linux server</a> </div> <div style="margin-top: 10px;"> <a href="/en/85a78aaa8c5234d98fb4">Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server</a> </div> <div style="margin-top: 10px;"> <a href="/en/a64806138b16af9a8ada">Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2</a> </div> <div style="margin-top: 10px;"> <a href="/en/c91638d1c82b3cfbe62c">Verification of how to periodically execute a script on a Linux server on Windows</a> </div> <div style="margin-top: 10px;"> <a href="/en/e2ecb524240d81c57ea2">Rock-paper-scissors with Python Let's run on a Windows local server for beginners</a> </div> <div style="margin-top: 10px;"> <a href="/en/e518048b9bbfe58ffa52">Try running Python's web framework Bottle on a lollipop server (lollipo plan)</a> </div> <div style="margin-top: 10px;"> <a href="/en/ec6835a6ccfcffac3bcc">Build Linux on a Windows environment. Steps to install Laradock and migrate</a> </div> <div style="margin-top: 10px;"> <a href="/en/f7d0ca5ca0416bb5d514">I made a webAPI! Build environment from Django Rest Framework 1 on EC2</a> </div> <div style="margin-top: 10px;"> <a href="/en/fb2ced7272f363ad5df7">Build a proxy server with nginx on multiple remote servers using Ansible</a> </div> <div style="margin-top: 10px;"> <a href="/en/01eab1b2b5785163e684">I'll install Ruby on EC2 (Amazon Linux2) 2020</a> </div> <div style="margin-top: 10px;"> <a href="/en/09aa831395bc296bc003">How to build my own Linux server</a> </div> <div style="margin-top: 10px;"> <a href="/en/0a695a8de793a3d8d0e9">[UE4] Build DedicatedServer on Windows and Linux</a> </div> <div style="margin-top: 10px;"> <a href="/en/174883d7fa8d2dd4240a">Easily build a DNS server using Twisted</a> </div> <div style="margin-top: 10px;"> <a href="/en/3b103188185dc1a04a8d">Build a python environment on MacOS (Catallina)</a> </div> <div style="margin-top: 10px;"> <a href="/en/61d4c5af7d8ffcf49ad3">Build CGI Server running on Python 3 on Docker</a> </div> <div style="margin-top: 10px;"> <a href="/en/7563d8e7fcbe63754e90">Build an LNPP environment on Amazon Linux 2</a> </div> <div style="margin-top: 10px;"> <a href="/en/76b80cdd18a8d6f799df">Install and Configure TigerVNC server on Linux</a> </div> <div style="margin-top: 10px;"> <a href="/en/9b0bb45b5143c4e04f6b">Dockerfile: Install Docker on your Linux server</a> </div> <div style="margin-top: 10px;"> <a href="/en/a74535fd5b07e4793a64">Build jupyter notebook on remote server (CentOS)</a> </div> <div style="margin-top: 10px;"> <a href="/en/aaff12703ddcfbc1f069">[Linux] Build a jenkins environment with Docker</a> </div> <div style="margin-top: 10px;"> <a href="/en/c320de519e6f4e5e7b7b">Create a Linux virtual machine on Windows</a> </div> <!-- END LINK ************************* --> </p> </div> </div> </div> <div class="footer text-center" style="margin-top: 40px;"> <!-- <p> Licensed under cc by-sa 3.0 with attribution required. </p> --> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.2/build/highlight.min.js"></script> <script> $(document).ready(function() { var cfg_post_height = 60; var cfg_per = 0.51; var ads_obj = $('<ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-5469278205356604" data-ad-slot="7950405964"></ins>'); $('pre code').each(function(i, e) {hljs.highlightBlock(e)}); function getDocumentOffsetPosition( el ) { var _x = 0; var _y = 0; while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) { _x += el.offsetLeft - el.scrollLeft; _y += el.offsetTop - el.scrollTop; el = el.offsetParent; } return { top: _y, left: _x }; } if ( $( "#article202011" ).length ) { var h1_pos = getDocumentOffsetPosition($('h1')[0]); var footer_pos = getDocumentOffsetPosition($('.link-top')[0]); var post_distance = footer_pos.top - h1_pos.top; // console.log('h1_pos: '+ h1_pos.top); // console.log(cfg_post_height) if((post_distance/h1_pos.top)>=cfg_post_height) { // console.log('tesssssssssssssssssssssssssssssssss'); $( ".container p" ).each(function( index ) { var p_tag_pos = $(this).position().top; var dis = p_tag_pos - h1_pos.top; var per = dis/post_distance; if(per>cfg_per) { ads_obj.insertAfter($(this)); (adsbygoogle = window.adsbygoogle || []).push({}); console.log( index + ": " + $( this ).text() ); return false; } }); } } }); </script> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <!-- ads --> <script data-ad-client="ca-pub-5469278205356604" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" type="d7540fe192d881abe59fcf57-text/javascript"></script> <!-- end ads --> </body> </html><script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="a9253205e592ab6271dbb2ce-|49" defer></script>