【AWS学習記】WordPress 環境構築④~WEBサーバ用EC2インスタンス起動
WordPress構築を行うにあたり、RDSの起動まで完了したため、WEBサーバ用のEC2のインスタンスを起動する。」 ここでも、極力aws-cliを利用する。
目次
- 目次
- 構成
- EC2の起動
- EIPの取得
- EIPの割り当て
- セキュリティグループへのインバウンドルールの追加
- teratermから公開用WEBサーバEC2インスタンスへの接続
- mysqlクライアントインストール
- 公開用WEBサーバEC2インスタンスからRDSへの接続
構成
(構成図)
EC2の起動
EC2には、AmazonLinuxを起動する。
なお、作成するにあたり、以下で、作成した下記のサブネットに該当するsubnetIDを指定する。
* 10.0.0.0/24 公開用WEBサーバを収容する。(SubnetIDはここでは「subnet-xxxbf105」)
root@Linux ~]# aws ec2 run-instances --image-id ami-374db956 --instance-type t2.micro \ > --security-group-ids sg-xxxafdb4 --key-name my-keypair \ > --subnet-id subnet-xxxbf105 { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 中略 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ] } [root@Linux ~]#
EIPの取得
インターネットからアクセス可能となるようにEIPを取得する。
[root@Linux ~]# aws ec2 allocate-address { "PublicIp": "52.xxx.xx.247", "Domain": "vpc", "AllocationId": "eipalloc-xxx496ac" } [root@Linux ~]# [root@Linux ~]#
EIPの割り当て
取得したEIPを起動したEC2インスタンスに割り当てる。
[root@Linux ~]# aws ec2 associate-address --allocation-id eipalloc-xxx496ac \ > --instance-id i-xxxxxxf837cd79xx { "AssociationId": "eipassoc-1a1eed7d" } [root@Linux ~]#
セキュリティグループへのインバウンドルールの追加
起動したEC2向けにインバウンドルールを適用する。
ここでは、SSHとHTTPを許可する。
[root@Linux ~]# aws ec2 authorize-security-group-ingress --group-id sg-xxxafdb4 \ > --protocol tcp --port 22 --cidr 0.0.0.0/0 [root@Linux ~]#
[root@Linux ~]# aws ec2 authorize-security-group-ingress --group-id sg-xxxafdb4 \ > --protocol tcp --port 80 --cidr 0.0.0.0/0 [root@Linux ~]#
teratermから公開用WEBサーバEC2インスタンスへの接続
作成したEIP(ここでは、52.xxx.xx.247)に対して、teratermからSSH接続する。
※ここでは、ユーザー名「ec2-user」で接続し、以降の作業を行う。
mysqlクライアントインストール
接続した公開用WEBサーバEC2インスタンスから、RDSインスタンスに接続するため、
mysqlクライアントインストールを公開用WEBサーバEC2インスタンスにインストールする。
[root@Linux ~]# sudo yum install -y mysql
公開用WEBサーバEC2インスタンスからRDSへの接続
mysqlクライアントを利用して、RDSへ接続をしてみる。
接続するために、RDSへのエンドポイント名を確認する。
RDSの管理コンソールから、起動しているインスタンスを選択し、
下記のエンドポイント名を取得する。
エンドポイント名を取得したら、RDSへ下記のとおり接続できることを確認する。
[ec2-user@ip-10-0-0-158 html]$ mysql -h aws-mysql-rds.ciszm1fnyhoj.ap-northeast-1.rds.amazonaws.com -u admin -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 68 Server version: 5.6.27-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>