Sample PHP WebApp to store contact info.
The app uses AWS RDS to store all contact info and it uses AWS SNS to send SMS notifications.
-
Login into AWS Console.
-
On Services type VPC and select the service.
-
Select Create VPC and complete with below parameters.
- Resources to create : VPC and more
- Auto-generate : db
- IPv4 CIDR block : 30.0.0.0/16
- Number of Availability Zones (AZs) : 2
- Customize AZs
- First availability zone : us-east-1a
- Second availability zone : us-east-1b
- Number of public subnets : 2
- Number of private subnets : 2
- Customize subnets CIDR blocks
- Public subnet CIDR block in us-east-1a : 30.0.1.0/24
- Public subnet CIDR block in us-east-1b : 30.0.3.0/24
- Private subnet CIDR block in us-east-1a : 30.0.2.0/24
- Private subnet CIDR block in us-east-1b : 30.0.4.0/24
- NAT gateways : None
- VPC endpoints : None
Note Remain standard values for all other options.
-
Click Create VPC.
-
Validate if all VPC resources were created.
Note Check carefully this step prior to proceed. This step is a requirement for all following steps. In case of questions/issues, don´t proceed, stop and ask for help.
Warning Discard message Failed to load rule groups, in case it appears.
-
On Services type VPC and select the service.
-
In the left side menu click Security Groups.
-
Click Create security group and complete with below parameters.
- Security group name : db-sg-pub
- Description : DB Security Group public
- VPC : db-vpc
- Inbound rules (Click Add rule for each rule below)
- Rule 1
- Type : All traffic
- Source : 30.0.0.0/16
- Rule 2
- Type : HTTP
- Source : 0.0.0.0/0
- Rule 3
- Type : SSH
- Source : 0.0.0.0/0
- Rule 1
Note Remain standard values for all other options.
-
Click Create security group.
-
On Services type VPC and select the service.
-
In the left side menu click Security Groups.
-
Click Create security group and complete with below parameters.
- Security group name : db-sg-priv
- Description : DB Security Group private
- VPC : db-vpc
- Inbound rules (Click Add rule)
- Rule 1
- Type : All traffic
- Source : 30.0.0.0/16
- Rule 1
Note Remain standard values for all other options.
-
Click Create security group.
- On Services type RDS and select the service.
-
In the left panel menu click Subnet groups.
-
Click Create DB subnet group and complete with below parameters.
- Name : db-sn-group
- Description: DB Subnet Group
- VPC : db-vpc
- Availability Zones : us-east-1a and us-east-1b
- Subnets: 30.0.2.0/24 and 30.0.4.0/24
Note Remain standard values for all other options.
-
Click Create.
-
In the left panel menu click Parameter groups.
-
Click Create parameter group and complete with below parameters.
- Parameter group family : mysql8.0
- Group name : db-param-group
- Description: DB Parameter Group
Note Remain standard values for all other options.
-
Click Create.
-
Check the line with the new Parameter Group db-param-group and click Parameter group actions, Edit.
-
In the Parameters search field type character_set_server.
-
Check the line with parameter character_set_server and select utf8 in the Values combo box.
-
Click Save changes.
-
In the Parameters search field type character_set_database.
-
Check the line with parameter character_set_database and select utf8 in the Values combo box.
-
Click Save changes.
-
In the left panel menu click Databases.
-
Click Create database and complete with below parameters.
- Engine options
- Engine type: MySQL
- Availability and durability
- Deployment options: Multi-AZ DB instance
- Settings
- DB instance identifier : db-instance-id
- Master username : dbadmin
- Master password : dbpassword
- Confirm master password : dbpassword
- Instance configuration
- DB instance class : Burstable classes (includes t classes)
- Storage
- Storage type : gp2
- Enable storage autoscaling: disabled
- Connectivity
- Virtual private cloud (VPC) : db-vpc
- Existing VPC security groups : db-sg-priv
Note Remove the default security group if selected.
- Monitoring
- Enable Enhanced monitoring : disabled
- Additional configuration
- Initial database name : dbname
- DB parameter group : db-param-group
- Enable automated backups : disabled
- Enable encryption : disabled
- Enable auto minor version upgrade : disabled
- Enable deletion protection : disabled
Note Remain standard values for all other options.
- Engine options
-
Click Create database.
-
Close screen Suggested add-ons for database-1 in case it appears.
-
Validate database creation.
Note Process should take 10-15 minutes. Wait until status is Available.
-
Click on db-instance-id and capture the Endpoint value. It will be used on later steps.
-
Login into AWS Console.
-
On Services type EC2 and select the service.
-
In the left panel menu, under Instances, click Launch Templates.
-
Select Create launch template and complete with below parameters.
- Launch template name : ec2-launch-template
- Application and OS Images (Amazon Machine Image)
- Quick start: Amazon Linux
- Amazon Machine Image (AMI) : Amazon Linux 2 AMI (HVM)
- Instance type : t2.micro
- Key pair : vockey (or any from your choice)
- Network Settings
- Security groups : db-sg-pub
- Add network interface
- Auto-assign public IP : Enable
- Advanced details
- User data
Note Replace [RDS_ENDPOINT] with the database endpoint captured in previous steps.
#!/bin/bash echo "Update/Install required OS packages" yum update -y amazon-linux-extras install -y php7.2 epel yum install -y httpd mysql php-mtdowling-jmespath-php php-xml telnet tree git echo "Config PHP app Connection to Database" cat <<EOT >> /var/www/config.php <?php define('DB_SERVER', '[RDS_ENDPOINT]'); define('DB_USERNAME', 'dbadmin'); define('DB_PASSWORD', 'dbpassword'); define('DB_DATABASE', 'dbname'); ?> EOT echo "Deploy PHP app" cd /tmp git clone https://github.com/kledsonhugo/notifier cp /tmp/notifier/app/*.php /var/www/html/ rm -rf /tmp/notifier echo "Config Apache WebServer" usermod -a -G apache ec2-user chown -R ec2-user:apache /var/www chmod 2775 /var/www find /var/www -type d -exec chmod 2775 {} \; find /var/www -type f -exec chmod 0664 {} \; echo "Start Apache WebServer" systemctl enable httpd service httpd restart
Note Remain standard values for all other options.
-
Click Create launch template.
-
In the left panel menu, under Auto Scaling, click Auto Scaling Groups.
-
Select Create Auto Scaling group and complete with below parameters.
- Auto Scaling group name : ec2-auto-scaling-group
- Launch template : ec2-launch-template
Note Remain standard values for all other options.
-
Click Next.
-
Complete with below parameters.
- VPC : db-vpc
- Availability Zones and subnets : 30.0.1.0/24 and 30.0.3.0/24
Note Remain standard values for all other options.
-
Click Next.
-
Complete with below parameters.
- Load balancing
- Attach to a new load balancer
- Attach to a new load balancer
- Load balancer name: ec2-load-balancer
- Load balancer scheme : Internet-facing
- Listeners and routing
- Default routing (forward to) : Create a target group
- New target group name : ec2-target-group
Note Remain standard values for all other options.
- Load balancing
-
Click Next.
-
Complete with below parameters.
- Group size - optional
- Desired capacity : 4
- Minimum capacity : 2
- Maximum capacity : 8
Note Remain standard values for all other options.
- Group size - optional
-
Click Next.
-
Click Next again.
-
Click Next last time.
-
Click Create Auto Scaling group.
-
In the left panel menu, under Load Balancing, click Load Balancers.
-
Click on ec2-load-balancer to open the Load Balancer page details.
-
Click in the Security menu.
-
Click Edit.
-
Remove the default Security Group and add db-sg-pub.
-
Click Save Changes.
-
In the left panel menu, under Load Balancing, click Target Groups.
-
Click on ec2-target-group and validate if 4 instances are Healthy.
Note The instance registration process takes 5-10 minutes.
-
In the left panel menu, under Load Balancing, click Load Balancers.
-
Click on ec2-load-balancer and capture the value for field DNS name.
-
Open a browser tab and navigate to the load balancer DNS name.
If you reach this step successfully, you completed the procedure.
Don´t forget to destroy all resources avoiding unnecessary costs.