《AWS云计算实战》2.五分钟搭建 WordPress 站点

Posted lijianming180

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《AWS云计算实战》2.五分钟搭建 WordPress 站点相关的知识,希望对你有一定的参考价值。

本文内容:使用 AWS CloudFormation 实现基础设施的自动化部署。

WordPress 基础设施架构

需要使用4个不同的 AWS 服务实现 WordPress 站点项目:

  • 弹性负载均衡(Elastic Load Balancing,ELB)——AWS 提供的弹性负载均衡服务。负载均衡器用于将流量分发到 Web 服务器。
  • 弹性计算云(Elastic Computer Cloud,EC2)——EC2 服务提供的虚拟服务器。

    如果 EC2 的 CPU 利用率高于 80%,应该添加第三台服务器,以防止 Web 页面加载的时间过长。

  • 适用于 mysql 的关系型数据库服务(Relational Database Service for MySQL)——AWS 的关系数据库服务(RDS)提供了对 MySQL 的支持。

    使用 RDS 的好处是,AWS 平台会自动完成数据库备份。

  • 安全组(Security group)——安全组类似于防火墙。

技术图片

准备工作:创建并设置密钥对

  1. 创建密钥对并下载一个名为 mykey.pem 的私钥文件。
  2. Mac 系统下,shift+cmd+.显示隐藏文件,将 mykey.pem 文件放到 .ssh 目录下,设置密钥文件访问权限仅自己可见:
1
chmod 400 mykey.pem

CloudFormation(堆栈)

AWS CloudFormation 允许您快速和轻松地部署自己的基础设施资源和应用程序在 AWS 上。

AWS CloudFormation 是一项服务,可帮助您对 Amazon Web Services 资源进行建模和设置,以便能花较少的时间管理这些资源,而将更多的时间花在运行于 AWS 中的应用程序上。您创建一个描述您所需的所有 AWS 资源(如 Amazon EC2 实例或 Amazon RDS 数据库实例)的模板,并且 AWS CloudFormation 将负责为您设置和配置这些资源。

指定的 Amazon S3 模板 URL:https://s3.amazonaws.com/awsinaction/chapter2/template.json

简单4步就可以创建一个 CloudFormation

技术图片

使用 CloudFormation(堆栈)的好处是:你可以随时按需自动化创建或删除基础设施。

估算成本

使用 AWS 简单月度计算器 分析博客站点基础设施的成本。

技术图片

?? 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS in Action: chapter 2",
"Parameters": {
"KeyName": { // 密钥对
"Description": "Key Pair name",
"Type": "AWS::EC2::KeyPair::KeyName",
"Default": "mykey"
}
},
"Mappings": {
"EC2RegionMap": {
"ap-northeast-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-cbf90ecb"},
"ap-southeast-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-68d8e93a"},
"ap-southeast-2": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-fd9cecc7"},
"eu-central-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-a8221fb5"},
"eu-west-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-a10897d6"},
"sa-east-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-b52890a8"},
"us-east-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-1ecae776"},
"us-west-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-d114f295"},
"us-west-2": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-e7527ed7"}
}
},
"Resources": {
"VPC": { // 虚拟网络
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": "172.31.0.0/16",
"EnableDnsHostnames": "true"
}
},
"InternetGateway": { // 网关
"Type": "AWS::EC2::InternetGateway",
"Properties": {
}
},
"VPCGatewayAttachment": { // VPC 网关关联设置
"Type": "AWS::EC2::VPCGatewayAttachment",
"Properties": {
"VpcId": {"Ref": "VPC"},
"InternetGatewayId": {"Ref": "InternetGateway"}
}
},
"SubnetA": { // 虚拟网络子网A
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": {"Fn::Select": ["0", {"Fn::GetAZs": ""}]},
"CidrBlock": "172.31.38.0/24",
"VpcId": {"Ref": "VPC"}
}
},
" 大专栏  《AWS云计算实战》2.五分钟搭建 WordPress 站点SubnetB": { // 虚拟网络子网B
"Type": "AWS::EC2::Subnet",
"Properties": {
"AvailabilityZone": {"Fn::Select": ["1", {"Fn::GetAZs": ""}]},
"CidrBlock": "172.31.37.0/24",
"VpcId": {"Ref": "VPC"}
}
},
"RouteTable": { // 路由表
"Type": "AWS::EC2::RouteTable",
"Properties": {
"VpcId": {"Ref": "VPC"}
}
},
"RouteTableAssociationA": { // 路由表关联到子网A
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {"Ref": "SubnetA"},
"RouteTableId": {"Ref": "RouteTable"}
}
},
"RouteTableAssociationB": { // 路由表关联到子网B
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {"Ref": "SubnetB"},
"RouteTableId": {"Ref": "RouteTable"}
}
},
"RoutePublicNATToInternet": { // NAT 路由设置
"Type": "AWS::EC2::Route",
"Properties": {
"RouteTableId": {"Ref": "RouteTable"},
"DestinationCidrBlock": "0.0.0.0/0",
"GatewayId": {"Ref": "InternetGateway"}
},
"DependsOn": "VPCGatewayAttachment"
},
"NetworkAcl": {
"Type": "AWS::EC2::NetworkAcl",
"Properties": {
"VpcId": {"Ref": "VPC"}
}
},
"SubnetNetworkAclAssociationA": {
"Type": "AWS::EC2::SubnetNetworkAclAssociation",
"Properties": {
"SubnetId": {"Ref": "SubnetA"},
"NetworkAclId": {"Ref": "NetworkAcl"}
}
},
"SubnetNetworkAclAssociationB": {
"Type": "AWS::EC2::SubnetNetworkAclAssociation",
"Properties": {
"SubnetId": {"Ref": "SubnetB"},
"NetworkAclId": {"Ref": "NetworkAcl"}
}
},
"NetworkAclEntryIngress": {
"Type": "AWS::EC2::NetworkAclEntry",
"Properties": {
"NetworkAclId": {"Ref": "NetworkAcl"},
"RuleNumber": "100",
"Protocol": "-1",
"RuleAction": "allow",
"Egress": "false",
"CidrBlock": "0.0.0.0/0"
}
},
"NetworkAclEntryEgress": {
"Type": "AWS::EC2::NetworkAclEntry",
"Properties": {
"NetworkAclId": {"Ref": "NetworkAcl"},
"RuleNumber": "100",
"Protocol": "-1",
"RuleAction": "allow",
"Egress": "true",
"CidrBlock": "0.0.0.0/0"
}
},
"LoadBalancer": { // 负载均衡
"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
"Properties": {
"Subnets": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}],
"LoadBalancerName": "awsinaction-elb",
"Listeners": [{ // 监听 80 端口
"InstancePort": "80",
"InstanceProtocol": "HTTP",
"LoadBalancerPort": "80",
"Protocol": "HTTP"
}],
"HealthCheck": { // 端口状态检测设置
"HealthyThreshold": "2",
"Interval": "5",
"Target": "TCP:80",
"Timeout": "3",
"UnhealthyThreshold": "2"
},
"SecurityGroups": [{"Ref": "LoadBalancerSecurityGroup"}], // 安全组设置
"Scheme": "internet-facing"
},
"DependsOn": "VPCGatewayAttachment"
},
"LoadBalancerSecurityGroup": { // 负载均衡安全组设置
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "awsinaction-elb-sg",
"VpcId": {"Ref": "VPC"},
"SecurityGroupIngress": [{
"CidrIp": "0.0.0.0/0",
"FromPort": 80,
"IpProtocol": "tcp",
"ToPort": 80
}]
}
},
"WebServerSecurityGroup": { // Web 服务安全组,打开80、22
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "awsinaction-sg",
"VpcId": {"Ref": "VPC"},
"SecurityGroupIngress": [{
"CidrIp": "0.0.0.0/0",
"FromPort": 22,
"IpProtocol": "tcp",
"ToPort": 22
}, {
"FromPort": 80,
"IpProtocol": "tcp",
"SourceSecurityGroupId": {"Ref": "LoadBalancerSecurityGroup"},
"ToPort": 80
}]
}
},
"DatabaseSecurityGroup": { // 数据库安全组
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "awsinaction-db-sg",
"VpcId": {"Ref": "VPC"},
"SecurityGroupIngress": [{
"IpProtocol": "tcp",
"FromPort": "3306",
"ToPort": "3306",
"SourceSecurityGroupId": {"Ref": "WebServerSecurityGroup"}
}]
}
},
"Database": { // 数据库设置
"Type": "AWS::RDS::DBInstance",
"DeletionPolicy": "Delete",
"Properties": {
"AllocatedStorage": "5",
"BackupRetentionPeriod": "0",
"DBInstanceClass": "db.t2.micro",
"DBInstanceIdentifier": "awsinaction-db",
"DBName": "wordpress",
"Engine": "MySQL",
"MasterUsername": "wordpress",
"MasterUserPassword": "wordpress",
"VPCSecurityGroups": [{"Fn::GetAtt": ["DatabaseSecurityGroup", "GroupId"]}],
"DBSubnetGroupName": {"Ref": "DBSubnetGroup"}
},
"DependsOn": "VPCGatewayAttachment"
},
"DBSubnetGroup" : {
"Type" : "AWS::RDS::DBSubnetGroup",
"Properties" : {
"DBSubnetGroupDescription" : "DB subnet group",
"SubnetIds": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}]
}
},
"LaunchConfiguration": { // 启动配置
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"packages": {
"yum": {
"php": [],
"php-mysql": [],
"mysql": [],
"httpd": []
}
},
"sources": {
"/var/www/html": "https://wordpress.org/wordpress-4.2.4.tar.gz"
},
"files": {
"/tmp/config": { // 虚拟机启动初始化 shell 脚本
"content": {"Fn::Join": ["", [
"#!/bin/bash -exn",
"cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.phpn",
"sed -i "s/'database_name_here'/'wordpress'/g" wp-config.phpn",
"sed -i "s/'username_here'/'wordpress'/g" wp-config.phpn",
"sed -i "s/'password_here'/'wordpress'/g" wp-config.phpn",
"sed -i "s/'localhost'/'", {"Fn::GetAtt": ["Database", "Endpoint.Address"]}, "'/g" wp-config.phpn",
"chmod -R 777 wp-content/ n"
]]},
"mode": "000500",
"owner": "root",
"group": "root"
}
},
"commands": {
"01_config": {
"command": "/tmp/config",
"cwd": "/var/www/html/wordpress"
}
},
"services": {
"sysvinit": {
"httpd": {
"enabled": "true",
"ensureRunning": "true"
}
}
}
}
}
},
"Properties": {
"EbsOptimized": false,
"ImageId": {"Fn::FindInMap": ["EC2RegionMap", {"Ref": "AWS::Region"}, "AmazonLinuxAMIHVMEBSBacked64bit"]},
"InstanceType": "t2.micro",
"SecurityGroups": [{"Ref": "WebServerSecurityGroup"}],
"KeyName": {"Ref": "KeyName"},
"AssociatePublicIpAddress": true,
"UserData": {"Fn::Base64": {"Fn::Join": ["", [
"#!/bin/bash -exn",
"yum update -y aws-cfn-bootstrapn",
"/opt/aws/bin/cfn-init -v --stack ", {"Ref": "AWS::StackName"}, " --resource LaunchConfiguration --region ", {"Ref": "AWS::Region"}, "n",
"/opt/aws/bin/cfn-signal -e $? --stack ", {"Ref": "AWS::StackName"}, " --resource AutoScalingGroup --region ", {"Ref": "AWS::Region"}, "n"
]]}}
}
},
"AutoScalingGroup": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"LoadBalancerNames": [{"Ref": "LoadBalancer"}],
"LaunchConfigurationName": {"Ref": "LaunchConfiguration"},
"MinSize": "2",
"MaxSize": "2",
"DesiredCapacity": "2",
"VPCZoneIdentifier": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}]
},
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "PT10M"
}
},
"DependsOn": "VPCGatewayAttachment"
}
},
"Outputs": {
"URL": {
"Value": {"Fn::Join": ["", ["http://", {"Fn::GetAtt": ["LoadBalancer", "DNSName"]}, "/wordpress"]]},
"Description": "Wordpress URL"
}
}
}

以上是关于《AWS云计算实战》2.五分钟搭建 WordPress 站点的主要内容,如果未能解决你的问题,请参考以下文章

五分钟带你玩转docker实战elk环境——kibana搭建

五分钟带你玩转docker(十三)实战elk环境——logstash搭建

五分钟带你玩转docker(十三)实战elk环境——logstash搭建

五分钟带你玩转docker实战elk环境——elasticsearch搭建

五分钟带你玩转docker实战elk环境——elasticsearch搭建

五分钟搭建WordPress博客