Powershell AWS 自动化管理 (12) - 创建一个高可用的WordPress博客(下)
#Create AMI Image#创建EC2实例
#绑定Role
New-IAMInstanceProfile -InstanceProfileName "WordPress"
Add-IAMRoleToInstanceProfile -RoleName EC2-S3 -InstanceProfileName "WordPress"
$groupid=Get-EC2SecurityGroup | Where-Object {$_.GroupName -eq "WordPress"} | select -ExpandProperty groupid
#配置LAMP和WordPress
$userdata=@"
#!/bin/bash
apt-get update
apt-get upgrade -y
apt-get install -y mysql-client libmysqlclient15-dev apache2 apache2-doc apache2-mpm-prefork apache2-utils libexpat1 ssl-cert libapache2-mod-php5 php5 php5-common php5-curl php5-dev php5-gd php5-idn php-pear php5-imagick php5-mcrypt php5-mysql php5-ps php5-pspell php5-recode php5-xsl python-pip && pip install awscli
aws s3 cp --recursive s3://yuanliwordpress/wordpress /var/www/wordpress/
chown -R www-data.www-data wordpress
chmod 755 /var/www/wordpress/
aws s3 cp s3://yuanliwordpress/wordpress_vhosts /etc/apache2/sites-available/wordpress.conf
cd /etc/apache2/sites-avaiable
a2ensite wordpress.conf
service apache2 restart
chmod 777 /var/www/wordpress/wp-contents
echo */1 * * * * root aws s3 sync /var/www/wordpress/wp-content/uploads s3://yuanliwordpress/uploads >> /etc/crontab
a2enmod rewrite
service apache2 restart
"@
$b=::UTF8.GetBytes($userdata)
$a=::ToBase64String($b)
$instance=New-EC2Instance -ImageId ami-6c14310f -InstanceType t2.micro -KeyName aws -SubnetId $subnet1 -SecurityGroupId $groupid -MinCount 1 -MaxCount 1 -InstanceProfile_Name "WordPress" -UserData $a
$instanceid=($instance| select -expand instances).instanceid
write-host "Initilizing EC2 Instance, Please wait ..." -ForegroundColor Cyan -NoNewline
$state=$false
while($state -eq $false){
$name= (Get-EC2Instance -InstanceId $instanceid | select -ExpandProperty instances | select -ExpandProperty state).name
if($name.Value -eq "running"){
$state=$true
}else{
start-sleep -Seconds 2
write-host "..." -ForegroundColor Cyan -NoNewline
}
}
页:
[1]