Skip to content

Instantly share code, notes, and snippets.

@iitenkida7
Last active December 16, 2018 07:50
Show Gist options
  • Save iitenkida7/0f4c5fcdc4569e52efce559be17f5037 to your computer and use it in GitHub Desktop.
Save iitenkida7/0f4c5fcdc4569e52efce559be17f5037 to your computer and use it in GitHub Desktop.

AWS VPC構築メモ

  • 2つのAZに分けてサブネット4つを作成し、その2つをPublic、2つをPrivateとする
  • RDSでMySQLをMultiAZで起動
  • ELB配下に2台置きWebサーバはApacheを置く
    • セキュリティグループは80番と22番のみ公開

VPC

  1. vpc
    • CIDR 10.0.0.0/16
  2. subnet ( t2.* 建てられる、ap-northeast-1c, ap-northeast-1d を選択)
    • pub_c: 10.0.1.0/24
    • pub_d: 10.0.2.0/24
    • pte_c: 10.0.3.0/24
    • pte_d: 10.0.4.0/24
  3. pub subnet allow set global ip
  4. internet-gw
    • create
    • vpc に attach
  5. rouging
    • main

      • add routing: 0.0.0.0 igw-XXXX
      • associations: pub_c,pub_d
    • create private routing table

      • associations: pte_c,pte_d

SG

  • web
    • 0.0.0.0/0 80
    • 0.0.0.0/0 22
  • rds
    • web 3306

RDS

  • DB サブネットグループの作成
    • Privateのセグメントを選択
  • インスタンス作成
    • MySQL(本番稼動用 - MySQL)
    • MultiAZ
    • subnet: pte
    • t2.smallぐらいで
    • db config
      • 識別子: dev
      • user: masteruser
      • pass: mkpasswd -l 24
      • db名:dev

EC2

  1. [client]ssh-keygen

    ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa

  2. add key pair

  3. create instance x2(AZ それぞれで2回実行)

    • Amazon Linux 2

    • t2.small

    • userdata

      • 以下のスクリプト
    • sg

      web: 80,22

    #!/bin/bash -x
    yum install -y git
    cd ~root
    git clone https://gist.github.com/0f4c5fcdc4569e52efce559be17f5037.git bootstrap
    chmod 700 bootstrap/bootstrap.sh
    bootstrap/bootstrap.sh
    
    cat << EOT >> /etc/php-fpm.d/www.conf
    env[DB_HOST] = RDSエンドポイント.ap-northeast-1.rds.amazonaws.com
    env[DB_USER] = masteruser
    env[DB_PASS] = パスワード
    env[DB_NAME] = dev
    EOT
    
    systemctl restart php-fpm
    

ELB

  • listen port 80:80
  • az: pte_c,pte_d
  • sg: web
  • pingパス:/

Check

mysql -hRDSエンドポイント.ap-northeast-1.rds.amazonaws.com -umasteruser -p dev
CREATE TABLE dev(name varchar(20));
INSERT INTO dev(`name`) VALUES ("tanaka");
#!/bin/bash -x
yum -y update
amazon-linux-extras install -y php7.2 nginx1.12
yum -y install mysql
sed -i.bk 's/\(user\|group\) = apache*/\1 = nginx /g' /etc/php-fpm.d/www.conf
systemctl start nginx
systemctl enable nginx
systemctl enable php-fpm
systemctl start php-fpm
mv $(cd $(dirname $0);pwd)/test.php /usr/share/nginx/html
<?php
echo gethostname();
echo "<br>";
$db_host = getenv('DB_HOST');
$db_user = getenv('DB_USER');
$db_pass = getenv('DB_PASS');
$db_name = getenv('DB_NAME');
$pdo = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name . ';charset=utf8', $db_user, $db_pass);
$stmt = $pdo->query("SELECT * FROM dev");
while($row = $stmt -> fetch(PDO::FETCH_ASSOC)) {
echo $row['name'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment