A simple bash script that installs WordPress on a new EC2 instance.
- AWS EC2 account
- AWS cli installed
- with credentials setup
aws_secret_access_key = abc123 aws_access_key_id = 456789
- with region set in the config
region = ap-southeast-1
- with credentials setup
- Bash shell
- jq installed
- This is needed to parse the json response of AWS cli.
install-wordpress-ec2.sh keyName
- The keyName to access the instance is required. If you don't have one yet, check out the AWS EC2 documentation on how to create a key pair.
- A security group named wordpress-sg is created if it doesn't exists. It allows access to SSH and HTTP ports 22 and 80, respectively.
- A t2.micro instance is created using Amazon Linux (image-id=ami-de90a5a2).
- The script will wait for the instance creation until it is in the running state.
- The script will then poll the blog's url until it receives a http code 200 success response.
- The blog's url will be displayed at the end.
$ ./install-wordpress-ec2.sh madrian-keypair
Using KeyName madrian-keypair
Creating the EC2 instance ...
Instance created and running.
> instanceId=i-0c2122863cc167f53
Installing WordPress on the EC2 instance, this might take a few minutes ...
...... ...... ...... ...... ...... ...... [OK]
Your WordPress blog is ready!
Go to http://ec2-54-179-135-255.ap-southeast-1.compute.amazonaws.com/blog
A simple monitoring script is added to see the health status of the EC2 instance and the WordPress installation.
monitor-wordpress-ec2.sh instanceId
Pass here the instanceId returned by the installer script.
The script will query the following:
- The EC2 metric CPUUtilization from AWS CloudWatch reporting the maximum CPU percentage reached in the last one hour.
- The different EC2 instance states including the instance status, system status, and the instance state pending, running, etc.
- The blog's url and its underlying PHP server's availability whether they are UP or DOWN
$ ./monitor-wordpress-ec2.sh i-0c2122863cc167f53
-------------------------------------------------------------------------------
Start time : 2018-06-30T12:07:49+08:00
End time : 2018-06-30T13:07:49+08:00
Max CPU : 70.5084745762712
-------------------------------------------------------------------------------
Instance status : "ok"
System status : "ok"
Instance state : "running"
-------------------------------------------------------------------------------
Blog URL : http://ec2-54-179-135-255.ap-southeast-1.compute.amazonaws.com/blog
PHP site status : UP
Blog site status : UP
-------------------------------------------------------------------------------