LoginSignup
0
0

More than 3 years have passed since last update.

AWS CLIでWordPress環境を構築する~part2~

Last updated at Posted at 2020-02-11

概要

AWS学習目的で、下記記事のレシピを参考にAWS CLIでの環境構築を行う。
【Amazon3時間クッキング】材料費500円でAWSにWordPress環境を構築するレシピ ~part2~

この記事では以下の作業を行う。

  • キーペアの作成
  • セキュリティグループの作成(Webサーバー用)
    • ルールの追加
  • EC2インスタンスの作成(Webサーバー用)
  • Apacheのインストール

環境

  • macOS Catalina

前提条件

構築

キーペアの作成

項目
名前 test-key
秘密鍵のファイル名 test-key.pem
# ~/.sshディレクトリが存在しない場合は作成
mkdir ~/.ssh

# 権限変更
chmod 700 ~/.ssh

# フォルダの移動
$ cd ~/.ssh

# キーペアの作成
$ aws ec2 create-key-pair --key-name test-key --query 'KeyMaterial' --output text > test-key.pem

# 権限の変更
$ chmod 600 test-key.pem

セキュリティグループの作成

項目
グループ名 Web-Segment
説明 Web Server security group
# セキュリティグループの作成
$ aws ec2 create-security-group --group-name Web-Segment --description "Web Server security group" --vpc-id vpc-07154996e6c851750
{
    "GroupId": "sg-04520c52a1d8cc27f"
}

# 名前の設定
$ aws ec2 create-tags --resources vpc-07154996e6c851750 --tags Key=Name,Value=Web-Segment

 ルールの追加

タイプ プロトコル ポート範囲 ソース
SSH TCP 22 0.0.0.0/0
HTTP TCP 80 0.0.0.0/0
# SSHの有効化
$ aws ec2 authorize-security-group-ingress --group-id sg-04520c52a1d8cc27f --protocol tcp --port 22 --cidr 0.0.0.0/0

# HTTPの有効化
$ aws ec2 authorize-security-group-ingress --group-id sg-04520c52a1d8cc27f --protocol tcp --port 80 --cidr 0.0.0.0/0

EC2インスタンスの作成

Webサーバー用のEC2インスタンスを作成する。

項目
AMI Amazon Linux 2 AMI (HVM), SSD Volume Type
インスタンスタイプ t2.micro
自動割り当てパブリックIP 有効化
プライベートIP 10.0.1.10
名前 Web-Server

AMIのイメージIDはここで確認をする。

項目 既定値
ストレージタイプ gp2
ストレージサイズ 8G

ストレージタイプやサイズなどは既定値で作成をする。

# EC2インスタンスの作成
$ aws ec2 run-instances --image-id ami-011facbea5ec0363b --count 1 --instance-type t2.micro --key-name test-key --security-group-ids sg-04520c52a1d8cc27f --subnet-id subnet-02558655ff2936296 --private-ip-address 10.0.1.10 --network-interfaces '[ { "DeviceIndex": 0, "AssociatePublicIpAddress": true } ]'
{
    "Groups": [],
    "Instances": [
        {
            "AmiLaunchIndex": 0,
            "ImageId": "ami-011facbea5ec0363b",
            "InstanceId": "i-006e5f99c93e59b6a",
            "InstanceType": "t2.micro",
            "KeyName": "test-key",
            "LaunchTime": "2020-02-11T04:06:17.000Z",
            "Monitoring": {
                "State": "disabled"
            },
            "Placement": {
                "AvailabilityZone": "ap-northeast-1c",
                "GroupName": "",
                "Tenancy": "default"
            },
            "PrivateDnsName": "ip-10-0-1-10.ap-northeast-1.compute.internal",
            "PrivateIpAddress": "10.0.1.10",
            "ProductCodes": [],
            "PublicDnsName": "",
            "State": {
                "Code": 0,
                "Name": "pending"
            },
            "StateTransitionReason": "",
            "SubnetId": "subnet-02558655ff2936296",
            "VpcId": "vpc-07154996e6c851750",
            "Architecture": "x86_64",
            "BlockDeviceMappings": [],
            "ClientToken": "",
            "EbsOptimized": false,
            "Hypervisor": "xen",
            "NetworkInterfaces": [
                {
                    "Attachment": {
                        "AttachTime": "2020-02-11T04:06:17.000Z",
                        "AttachmentId": "eni-attach-0b6cc084839510e3b",
                        "DeleteOnTermination": true,
                        "DeviceIndex": 0,
                        "Status": "attaching"
                    },
                    "Description": "",
                    "Groups": [
                        {
                            "GroupName": "Web-Segment",
                            "GroupId": "sg-04520c52a1d8cc27f"
                        }
                    ],
                    "Ipv6Addresses": [],
                    "MacAddress": "0a:d9:42:fc:f4:e4",
                    "NetworkInterfaceId": "eni-0ae3beb1595c7e0f1",
                    "OwnerId": "403733593576",
                    "PrivateIpAddress": "10.0.1.10",
                    "PrivateIpAddresses": [
                        {
                            "Primary": true,
                            "PrivateIpAddress": "10.0.1.10"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Status": "in-use",
                    "SubnetId": "subnet-02558655ff2936296",
                    "VpcId": "vpc-07154996e6c851750",
                    "InterfaceType": "interface"
                }
            ],
            "RootDeviceName": "/dev/xvda",
            "RootDeviceType": "ebs",
            "SecurityGroups": [
                {
                    "GroupName": "Web-Segment",
                    "GroupId": "sg-04520c52a1d8cc27f"
                }
            ],
            "SourceDestCheck": true,
            "StateReason": {
                "Code": "pending",
                "Message": "pending"
            },
            "VirtualizationType": "hvm",
            "CpuOptions": {
                "CoreCount": 1,
                "ThreadsPerCore": 1
            },
            "CapacityReservationSpecification": {
                "CapacityReservationPreference": "open"
            },
            "MetadataOptions": {
                "State": "pending",
                "HttpTokens": "optional",
                "HttpPutResponseHopLimit": 1,
                "HttpEndpoint": "enabled"
            }
        }
    ],
    "OwnerId": "403733593576",
    "ReservationId": "r-043b930eee6f8945b"
}

# 名前の設定
$ aws ec2 create-tags --resources i-006e5f99c93e59b6a --tags Key=Name,Value=Web-Server

# パブリックIPを出力
$ aws ec2 describe-instances --instance-id i-006e5f99c93e59b6a --query "Reservations[].Instances[][PublicIpAddress]"
[
    [
        "54.249.79.116"
    ]
]

ssh接続する

$ ssh -i ~/.ssh/test-key.pem ec2-user@54.249.79.116

Apacheのインストール

10.0.1.10
# Apacheのインストール
$ sudo yum -y install httpd

# Apacheサービスの起動
$ sudo systemctl start httpd

# Apacheサービスのステータス確認
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 火 2020-02-11 04:08:49 UTC; 7s ago
     Docs: man:httpd.service(8)
 Main PID: 3616 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─3616 /usr/sbin/httpd -DFOREGROUND
           ├─3617 /usr/sbin/httpd -DFOREGROUND
           ├─3618 /usr/sbin/httpd -DFOREGROUND
           ├─3619 /usr/sbin/httpd -DFOREGROUND
           ├─3620 /usr/sbin/httpd -DFOREGROUND
           └─3621 /usr/sbin/httpd -DFOREGROUND

 2月 11 04:08:49 ip-10-0-1-10.ap-northeast-1.compute.internal systemd[1]: St...
 2月 11 04:08:49 ip-10-0-1-10.ap-northeast-1.compute.internal systemd[1]: St...
Hint: Some lines were ellipsized, use -l to show in full.

実際にアクセスして確認をする
http://54.249.79.116

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0