aws部署php程序,实验1:在AWS上部署一个Web Application应用实践

Deploying a Web Applicaiton on AWS

实验目标

通过多个AWS的服务去部署一个web application

web application是基于PHP 部署在 EC2上

数据会被存储在Amazon DynamoDB上

图片会被保存在 Amazon S3 上

操作步骤

创建一个IAM Role

原因:EC2调用其他aws服务的时候需要具备角色去操作,否则是非法操作。这个AWS做的比较严谨。

选择IAM服务,从导航菜单栏能看到创建角色

创建一个角色,记得选择是为EC2创建角色,同时设置权限为AmazonS3FullAccess、AmazonDynamoDBFullAccess,这样就可以访问S3和DynamoDB了

创建一个S3存储Bucket

选择S3服务,直接创建Bucket,设置一个名字,注意这个名字是全球独一无二的,因为将来要通过这个名字组合出的域名访问S3内存储的内容;

创建一个DynamoDB Table

选择DynamoDB服务,这是一个托管的NosqlDB ,创建相关表.

创建表的时候需要指定主键名称和排序键的名称。

创建一个VPC,同时创建一个IGW和一个Public Subnet

创建一个VPC,规划好网段,一般不使用172网段,这里我们可以采用常用的策略 设置 IPv4 CIDR block的值为10.200.0.0/16

9414bdbe3beb?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

图片.png

这样VPC内的子网共计有256*256个IP可用(当然包含了那些预留的IP)

创建一个Public Subnet,要在vpc的网段内,详情参考如下

9414bdbe3beb?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

图片.png

创建一个Internet GateWay,这个就是用来连接互联网的。这里要将Internet Gateway 给Attach到刚才创建的VPC上

创建一个路由表,Route table 是VPC级别的,设置Destination为0.0.0.0/0,将他的Target设置为刚才创建的Internet Gateway。

将路由表与刚才创建的Public Subnet关联,这里就是更改路由表的属性标签,将其余Public Subnet进行关联。

至此我们完成了实验环境的VPC的整体设置

部署应用到EC2上

先创建一个EC2实例,填写的相关信息如下图,建议选择linux AMI,号称改写了内核,完全虚拟化,性能媲美纯物理服务器;

9414bdbe3beb?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

图片.png

上图设置EC2的VPC、子网,自动获取公网IP、拥有写DynamoDB和S3的能力。

打开EC2的user Data标签,这个标签可以输入一些shell脚本,在服务器启动的时候执行。

这里我将如下过程都给放到user data输入框里边去:

安装httpd服务器和PHP语言运行环境

下载web application程序脚本

下载安装AWS支持PHP的sdk

将文件拷贝到S3的webapp-开头的那个bucket中

将数据拷贝到DynamoDB中

启动web server

脚本如下

#!/bin/bash

# Install Apache Web Server and PHP

yum remove -y httpd php

yum install -y httpd24 php56

# Download Lab files

wget https://us-west-2-tcprod.s3.amazonaws.com/courses/ILT-TF-100-ARCHIT/v5.4.0/lab-1-webapp/scripts/lab1src.zip

unzip lab1src.zip -d /tmp/

mv /tmp/lab1src/*.php /var/www/html/

# Download and install the AWS SDK for PHP

wget https://github.com/aws/aws-sdk-php/releases/download/3.15.9/aws.zip

unzip aws -d /var/www/html

# Determine Region

AZ=`curl --silent http://169.254.169.254/latest/meta-data/placement/availability-zone/`

REGION=${AZ::-1}

# Copy files to Amazon S3 bucket with name webapp-*

BUCKET=`aws s3api list-buckets --query "Buckets[?starts_with(Name, 'webapp-')].Name | [0]" --output text`

aws s3 cp /tmp/lab1src/jquery/ s3://$BUCKET/jquery/ --recursive --acl public-read --region $REGION

aws s3 cp /tmp/lab1src/images/ s3://$BUCKET/images/ --recursive --acl public-read --region $REGION

aws s3 ls s3://$BUCKET/ --region $REGION --recursive

# Configure Region and Bucket to use

sed -i "2s/%region%/$REGION/g" /var/www/html/*.php

sed -i "3s/%bucket%/$BUCKET/g" /var/www/html/*.php

# Copy data into DynamoDB table

aws dynamodb batch-write-item --request-items file:///tmp/lab1src/scripts/services1.json --region $REGION

aws dynamodb batch-write-item --request-items file:///tmp/lab1src/scripts/services2.json --region $REGION

aws dynamodb batch-write-item --request-items file:///tmp/lab1src/scripts/services3.json --region $REGION

# Turn on web server

chkconfig httpd on

service httpd start

验证应用是否启动

直接访问EC2,看刚才启动的实例的公网IP

通过浏览器访问公网IP,就能看到启动的应用

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
书名:Beginning PHP6, Apache, MySQL Web Development 出版社:Wrox 作者:Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass ISBN: 978-0-470-39114-3 简介: 这本书被誉为PHP APACHE MYSQL新圣经,覆盖多平台,书中例子适合各种开发人员学习和研究。 目录: Introduction ......................................................................................................... xxiii Part I: Movie Review Web Site Chapter 1: Configuring Your Installation ..........................................................3 Chapter 2: Creating PHP Pages Using PHP6 ..................................................19 Chapter 3: Using PHP with MySQL ................................................................77 Chapter 4: Using Tables to Display Data ......................................................105 Chapter 5: Form Elements: Letting the User Work with Data ........................131 Chapter 6: Letting the User Edit the Database .............................................153 Chapter 7: Manipulating and Creating Images with PHP...............................175 Chapter 8: Validating User Input .................................................................217 Chapter 9: Handling and Avoiding Errors ......................................................241 Part II: Comic Book Fan Site Chapter 10: Building Databases ..................................................................263 Chapter 11: Sending E-mail .........................................................................315 Chapter 12: User Logins, Profiles, and Personalization .................................355 Chapter 13: Building a Content Management System ..................................407 Chapter 14: Mailing Lists ............................................................................469 Chapter 15: Online Stores ...........................................................................505 Chapter 16: Creating a Bulletin Board System .............................................557 Chapter 17: Using Log Files to Improve Your Site .........................................627 Chapter 18: Troubleshooting .......................................................................641 Appendix A: Answers to Exercises ..............................................................649 Appendix B: PHP Quick Reference...............................................................685 Appendix C: PHP6 Functions .......................................................................695 Appendix D: MySQL Data Types ...................................................................753 Appendix E: MySQL Quick Reference ...........................................................757 Appendix F: Comparison of Text Editors .......................................................761 Appendix G: Choosing a Third-Party Host .....................................................765 Appendix H: An Introduction to PHP Data Objects ........................................769 Appendix I: Installation and Configuration on Linux ......................................777 Index .........................................................................................................785

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值