---
# System prepended metadata

title: AWS 考試
tags: [AWS, '2023_1']

---

# AWS 期中考試


![photo_2023-11-07_15-05-03.jpg](https://hackmd.io/_uploads/Byvp8tvXp.jpg)


---

## VPC part
* 建立VPC

![](https://hackmd.io/_uploads/r1D6ZB1Qa.png)

給VPC名稱跟IP
**VPC的IP後面用/16**
![](https://hackmd.io/_uploads/HyCIzSymT.png)


---

## 新增subnet

如果有用到target group、ELB的話要3個subnet(public:2, private:1)

![](https://hackmd.io/_uploads/HygnESk76.png)

![](https://hackmd.io/_uploads/SyiTmS1Q6.png)


---

## 新增IGW

要綁定VPC

![](https://hackmd.io/_uploads/SJV5IBy7p.png)

![](https://hackmd.io/_uploads/BkJ1wSkQp.jpg)

![](https://hackmd.io/_uploads/BytgwS1mT.png)


---

## 路由表

應該會直接有對應剛剛創的VPC的，找一下後面的描述

![](https://hackmd.io/_uploads/rk1kOSkXa.png)

**要去連接IGW**
![](https://hackmd.io/_uploads/HJ9_qIJXp.png)

![](https://hackmd.io/_uploads/HJSTqUkX6.png)

**去Subnet assciaction把public的subnet加過去**
![](https://hackmd.io/_uploads/HJLLoLJ7p.png)

這一步做完pub的就可以連出去了
![](https://hackmd.io/_uploads/rkv0jUJ7p.png)

## 自動給IP

**在 VPC > Subnet**

更改預設
![](https://hackmd.io/_uploads/S1XZVuKxa.png)

![](https://hackmd.io/_uploads/B1csEdKgp.png)


---

:::info
key記得丟到linux後要改權限`chmod 400 asd.pem`
:::

## EC2 建立
* 先建兩台EC2 一台當WWW server 一台當 DB server

### www server 那台

* 放在對外的subnet裡面(subnet_public)

* sg的部分，開ssh 跟 http(不是https) (ICMP看老師有沒有要求)

* 最下面的detail放

(會在建立時自動弄好httpd，預設網頁會有私有IP)
```
#!/bin/bash
yum update -y
yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service
echo “Hello World from $(hostname -f)” > /var/www/html/index.html
```

**再來先放著這台**




### DB 那台

網路設定_1 (不能給對外IP)
![](https://hackmd.io/_uploads/HynlDuYe6.png)

網路設定_2 (要有ssh 跟 MYSQL/Aurora 3306port)
![](https://hackmd.io/_uploads/SyCkvOYga.png)

透過ssh 從本地 => www的VM => DB的VM

#### NAT_GW

![](https://hackmd.io/_uploads/SyCgMttlT.png)

**要選public的subnet**(allocate Elastic IP按就對了)
![](https://hackmd.io/_uploads/ByLDMYKep.png)


又來到Routing Table(再建立一個)

![](https://hackmd.io/_uploads/ryP2zKYla.png)
**到Route 編輯 Edit route**
![](https://hackmd.io/_uploads/Bk0z7ttgp.png)
**對象0.0.0.0/0 這次選NAT_GW**
![](https://hackmd.io/_uploads/H1PhrKFgp.png)
到associactions編輯
![](https://hackmd.io/_uploads/SJ4GNKKep.png)
把private的加進去
![](https://hackmd.io/_uploads/Hk80YFKg6.png)

### 安裝MariaDB

[Installing MariaDB on Amazon Linux 2023](https://linux.how2shout.com/installing-mariadb-on-amazon-linux-2023/)

進去mariadb後的最初設定

```
Switch to unix_socket authentication [Y/n] n

Change the root password? [Y/n] Y

Remove anonymous users? [Y/n]? Y

Disallow root login remotely [Y/n] n

Remove test database and access to it? [Y/n] Y

Reload privelege tables now? [Y/n] Y
```

### DB table
```
    show databases;            # 顯示目前有的資料庫
    create database testdb;    # 創建資料庫
    use testdb;                # 使用資料庫
    create table addrbook(name varchar(50) not null, phone char(10));    # 創建資料表
    insert into addrbook(name, phone) values ("tom", "0912123456");      # 加入資料
    insert into addrbook(name, phone) values ("mary", "0912123567");     # 加入資料
    select name,phone from addrbook;          # 選擇資料
    update addrbook set phone="0987465123";   # 更新資料
```


:::info
在mariadb內新增user的方法:

CREATE USER user@'%' IDENTIFIED BY 'user';    # 前面的 user 是帳號, %指的是anywhere, 後面的user指的是密碼

GRANT ALL PRIVILEGES ON * . * TO 'user'@'%';

FLUSH PRIVILEGES;    #新增完成
:::

遠端連進資料庫
* public 的那台安裝mariadb
    `sudo yum install mariadb105`

**用EC2 public 那台與DB連線(帳號密碼user user)**
    ![image.png](https://hackmd.io/_uploads/Bk-xgPMQ6.png)


## 建apache server (在public那台)



安裝php三個模組
    ![photo_2023-11-03_19-59-41.jpg](https://hackmd.io/_uploads/HkN6lDGQ6.jpg)

**安裝完後重啟網頁伺服器**
    `systemctl restart httpd`

2. 到 html 資料夾下編輯
    切到 /var/www/html/(加入PHP腳本)
    
3. 連接到DB腳本
    
    `vim test.php`
    ```
    <?php
    $servername="192.168.1.200"; #DB 那台的IP
    $username="user";    
    $password="user";
    $dbname="test2db"; # 改你的DB table name

    $conn = new mysqli($servername, $username, $password, $dbname);

    if($conn->connect_error){
        die("connection failed: " . $conn->connect_error);
    }
    else{
        echo "connect OK!" . "<br>";
    }

    $sql="select name,phone from addrbook";
    $result=$conn->query($sql);

    if($result->num_rows>0){
        while($row=$result->fetch_assoc()){
            echo "name: " . $row["name"] . "\tphone: " . $row["phone"] . "<br>";
        }
    } else {
        echo "0 record";
    }
    ?>
    ```

## 設定路由表

要把第二個public的 subnet也加進來
![photo_2023-11-03_20-52-36.jpg](https://hackmd.io/_uploads/rJ-Navzm6.jpg)
    ![photo_2023-11-03_20-53-11.jpg](https://hackmd.io/_uploads/Hk0H6PfXp.jpg)
    

## 複製EC2

**要等EC2完全關閉才能image**
![image.png](https://hackmd.io/_uploads/SyOMeOG7T.png)
    
**EC2 下的 images => AMIs(這邊也要等他變成Active才能launch)**
![image.png](https://hackmd.io/_uploads/SyrjxdzX6.png)

**在detail那邊加上**

```
cd /var/www/html
sudo bash -c 'echo “Hello World from $(hostname -f) I am www server 1C” > index.html'
```


---





## ELB (Elastic Load Balance)


到 Load Balancing > Target Groups 創建群組
        ![](https://hackmd.io/_uploads/rJ7_7IHGa.jpg)
    
**創建的幾個設定**
        ![](https://hackmd.io/_uploads/HJvb48BMa.jpg)
        ![](https://hackmd.io/_uploads/Sk5WELrzT.jpg)
        ![](https://hackmd.io/_uploads/rkTbV8Sz6.jpg)

把WWW server的兩個機器選近來
        ![](https://hackmd.io/_uploads/SyCON8HM6.jpg)

到 Load Balancing > Load Balancers 建立一個 Load Balancer
        ![](https://hackmd.io/_uploads/SJja4UHMp.jpg)

選擇最左邊的ALB
![](https://hackmd.io/_uploads/H14Wr8rza.jpg)

基本設定
![](https://hackmd.io/_uploads/SJnHUUBf6.jpg)
![](https://hackmd.io/_uploads/SkkUI8SGT.jpg)
![](https://hackmd.io/_uploads/H14rULBfp.png)

測試(透過建立好的 Load Balancer 下的DNS連接最初建立的兩個網頁伺服器)
![](https://hackmd.io/_uploads/HJOyv8BfT.jpg)
        

---