目录
- 一 .TASK
- 二. 环境准备
- IAM
- 创建存储库
- ec2-repo
- ec2-wp
- 三. Code Deploy
- 创建应用程序
- 创建部署组
- 创建管道
- 部署后的ec2-wp
一 .TASK
创建2台EC2实例,一台名为「ec2-repo」,用作开发环境,将编写好的代码提交至repository(需安装git),一台名为「ec2-wp」,需要通过user data预安装code deploy agent,通过Code Deploy将应用程序部署至「ec2-wp」
二. 环境准备
IAM
创建用户组,将IAM权限赋予给该组
用户组下创建一名用户,生成credential,用于后续代码提交凭证
创建角色,用于后期的CodeDploy
创建存储库
ec2-repo
- 创建ec2实例(全流量安全组)
- 链接到此实例下载 wordpress 包
# wget https://s3-us-west-2.amazonaws.com/us-west-2-awstraining/courses/spl-82/v1.4.5.prod-2bbeba30/scripts/WordPressmaster.zip
# unzip WordPress-master.zip -d /tmp/WordPress_Temp
# mkdir -p /tmp/WordPress
# cp -paf /tmp/WordPress_Temp/WordPress-master/* /tmp/WordPress
# rm -rf /tmp/WordPress_Temp
# rm -f WordPress-master.zip
# mkdir -p /tmp/WordPress/scripts
- 创建 install_dependencies.sh
vi /tmp/WordPress/scripts/install_dependencies.sh
#!/bin/bash
yum install -y httpd wget php-fpm php-mysqli php-json php php-devel mariadb105-
server
- 创建 start_server.sh
vi /tmp/WordPress/scripts/start_server.sh
#!/bin/bash
systemctl start httpd
systemctl enable httpd
systemctl start mariadb
systemctl enable mariadb
- 创建 stop_server.sh
vi /tmp/WordPress/scripts/stop_server.sh
#!/bin/bash
isExistApp=`pgrep httpd`
if [[ -n $isExistApp ]]; then
systemctl stop httpd
fi
isExistApp=`pgrep sql`
if [[ -n $isExistApp ]]; then
systemctl stop mariadb
fi
- 创建数据库脚本
vi /tmp/WordPress/scripts/create_test_db.sh
#!/bin/bash
mysql -uroot <<CREATE_TEST_DB
CREATE DATABASE IF NOT EXISTS test;
CREATE_TEST_DB
- 创建 change_permissions.sh 脚本
vi /tmp/WordPress/scripts/change_permissions.sh
#!/bin/bash
chmod -R 777 /var/www/html/WordPress
- 修改文件属性
chmod +x /tmp/WordPress/scripts/*
ls -la /tmp/WordPress/scripts
- 创建 vi /tmp/WordPress/appspec.yml 文件
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html/WordPress
hooks:
BeforeInstall:
- location: scripts/install_dependencies.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/change_permissions.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_server.sh
- location: scripts/create_test_db.sh
timeout: 300
runas: root
ApplicationStop:
- location: scripts/stop_server.sh
timeout: 300
runas: root
- 将 wordpress 代码,appspec.yml 和 script 文件夹都 git push 到仓库中
ec2-wp
- 创建ec2实例,预设置如下userdata,安装code deploy agent
#!/bin/bash
yum install ruby -y
aws s3 cp s3://aws-codedeploy-ap-northeast-2/latest/install . --region ap-northeast-2
chmod +x install
./install auto
三. Code Deploy
创建应用程序
点击「创建存储库」,进入到创建应用程序页面
自定义应用程序名称
计算平台选择「EC2/本地」
创建部署组
创建管道
部署后的ec2-wp