ALB+EC2+MySQL

概要

组件:一个VPC,两个AZ,AZ-1中创建两个Subnet,分别放EC2和RDS,AZ-2中创建1个Subnet,放EC2,ALB下接这2个EC2。

功能:通过外网访问ALB公共地址,更新MySQL中的表数据。

实施

网络部分

创建VPC

名称:VPC_Minato

IPv4 CIDRIPv6 CIDR
192.168.0.0/16No

创建Internet Getway

名称:IGW-Minato

NameVPC
IGW-MinatoVPC_Minato

创建Route Table

1.用于VPC的主路由

名称:RT_Minato

送信先Target
192.168.0.0/16local
2.用于外网访问的次路由

名称:RT_Minato_Internet

送信先Target
192.168.0.0/16local
0.0.0.0/0IGW-Minato

创建Subnet

NameIPv4 CIDRAZRoute Table
VPC_Minato_Subnet_A192.168.0.0/24AZ-1RT_Minato_For_Internet
VPC_Minato_Subnet_B192.168.1.0/24AZ-1RT_Minato
VPC_Minato_Subnet_C192.168.2.0/24AZ-2RT_Minato_For_Internet

创建SG

1.用于EC2

名称:SG-Minato
Inbound:

TypeProtocalPortSource
HTTPTCP80192.168.0.0/16
SSHTCP220.0.0.0/0

Outbound:

TypeProtocalPortSource
ALLALLALL0.0.0.0/0
2.用于RDS

名称:RDS-Security-Group
Inbound:

TypeProtocalPortSource
MYSQL/AuroraTCP3306192.168.0.0/16

Outbound:

TypeProtocalPortSource
ALLALLALL0.0.0.0/0
3.用于ALB

名称:LB_SG_for_Internet
Inbound:

TypeProtocalPortSource
HTTPTCP800.0.0.0/0

Outbound:

TypeProtocalPortSource
ALLALLALL0.0.0.0/0

实例部分

创建EC2

NameAZSubnetKey NameSecurity Group
Server-AAZ-1VPC_Minato_Subnet_ALinux_keySG-Minato
Server-CAZ-2VPC_Minato_Subnet_CLinux_keySG-Minato

创建ALB

NameAZskimtypeSecurity Group
LB-MinataAZ-1,AZ-2internet-facingapplicationLB_SG_for_Internet
Target GroupTarget typeprotocol:port
Target-GPinstanceHTTP:80

以下参照:https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/AuroraUserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html

如下操作分别在Server-A 和Server-B 上实施:

安装Apache

[ec2-user ~]$ sudo yum update -y
[ec2-user ~]$ sudo yum install -y httpd24 php56 php56-mysqlnd
[ec2-user ~]$ sudo service httpd start
[ec2-user ~]$ sudo chkconfig httpd on

Apache Web Server 的文件访问权限设置

[ec2-user ~]$ sudo groupadd www
[ec2-user ~]$ sudo usermod -a -G www ec2-user
[ec2-user ~]$ exit

[ec2-user ~]$ groups
[ec2-user ~]$ sudo chgrp -R www /var/www
[ec2-user ~]$ sudo chmod 2775 /var/www
[ec2-user ~]$ find /var/www -type d -exec sudo chmod 2775 {} +
[ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} +

Apache Web Server 连接数据库实例

[ec2-user ~]$ cd /var/www
[ec2-user ~]$ mkdir inc
[ec2-user ~]$ cd inc
[ec2-user ~]$ >dbinfo.inc
[ec2-user ~]$ nano dbinfo.inc

<?php

define('DB_SERVER', 'db_cluster_writer_endpoint');
define('DB_USERNAME', 'tutorial_user');
define('DB_PASSWORD', 'master password');
define('DB_DATABASE', 'sample');

?>
                

[ec2-user ~]$ cd /var/www/html
[ec2-user ~]$ >SamplePage.php
[ec2-user ~]$ nano SamplePage.php


<?php include "../inc/dbinfo.inc"; ?>
<html>
<body>
<h1>Sample page</h1>
<?php

  /* Connect to MySQL and select the database. */
  $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);

  if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

  $database = mysqli_select_db($connection, DB_DATABASE);

  /* Ensure that the EMPLOYEES table exists. */
  VerifyEmployeesTable($connection, DB_DATABASE);

  /* If input fields are populated, add a row to the EMPLOYEES table. */
  $employee_name = htmlentities($_POST['NAME']);
  $employee_address = htmlentities($_POST['ADDRESS']);

  if (strlen($employee_name) || strlen($employee_address)) {
    AddEmployee($connection, $employee_name, $employee_address);
  }
?>

<!-- Input form -->
<form action="<?PHP echo $_SERVER['SCRIPT_NAME'] ?>" method="POST">
  <table border="0">
    <tr>
      <td>NAME</td>
      <td>ADDRESS</td>
    </tr>
    <tr>
      <td>
        <input type="text" name="NAME" maxlength="45" size="30" />
      </td>
      <td>
        <input type="text" name="ADDRESS" maxlength="90" size="60" />
      </td>
      <td>
        <input type="submit" value="Add Data" />
      </td>
    </tr>
  </table>
</form>

<!-- Display table data. -->
<table border="1" cellpadding="2" cellspacing="2">
  <tr>
    <td>ID</td>
    <td>NAME</td>
    <td>ADDRESS</td>
  </tr>

<?php

$result = mysqli_query($connection, "SELECT * FROM EMPLOYEES");

while($query_data = mysqli_fetch_row($result)) {
  echo "<tr>";
  echo "<td>",$query_data[0], "</td>",
       "<td>",$query_data[1], "</td>",
       "<td>",$query_data[2], "</td>";
  echo "</tr>";
}
?>

</table>

<!-- Clean up. -->
<?php

  mysqli_free_result($result);
  mysqli_close($connection);

?>

</body>
</html>


<?php

/* Add an employee to the table. */
function AddEmployee($connection, $name, $address) {
   $n = mysqli_real_escape_string($connection, $name);
   $a = mysqli_real_escape_string($connection, $address);

   $query = "INSERT INTO EMPLOYEES (NAME, ADDRESS) VALUES ('$n', '$a');";

   if(!mysqli_query($connection, $query)) echo("<p>Error adding employee data.</p>");
}

/* Check whether the table exists and, if not, create it. */
function VerifyEmployeesTable($connection, $dbName) {
  if(!TableExists("EMPLOYEES", $connection, $dbName))
  {
     $query = "CREATE TABLE EMPLOYEES (
         ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
         NAME VARCHAR(45),
         ADDRESS VARCHAR(90)
       )";

     if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");
  }
}

/* Check for the existence of a table. */
function TableExists($tableName, $connection, $dbName) {
  $t = mysqli_real_escape_string($connection, $tableName);
  $d = mysqli_real_escape_string($connection, $dbName);

  $checktable = mysqli_query($connection,
      "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");

  if(mysqli_num_rows($checktable) > 0) return true;

  return false;
}
?>                        
                

数据库部分

创建RDS(MySQL)

创建Subnet Group:db-subnetgroup

AZSubnet
AZ-1Subnet-B
AZ-2Subnet-C(D)

创建Paremeter Group:Default

创建OptionGroup:Default

创建数据库

DBEngineAZSecurity GroupAdmin USERAdmin PW
database-minatoMySQL CommunityAZ-1RDS-Security-Groupadminadmin-xxx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值