Skip to content
Advertisement

AWS CloudFront for PHP hosting

I’m new to AWS and am having some difficulties understanding CloudFront. I have started off with one EC2 instance with NGINX, MySQL, and some PHP files within the public folder to expose APIs to the world. Then I was told CloudFront could be used to protect the instance from malicious attacks. I figured CloudFront required an Elastic Load Balancer. So I created an ELB, one more EC2 instance (two EC2 instances in total), created a “Target group” grouping these two EC2 instances. I also created a S3 bucket, which seemed to be required for CloudFront even though I didn’t think I needed one since my app doesn’t have any static contents. Now searching through internet, the general idea seemed that the PHP files should live in the EBS of EC2 instances rather than CloudFront S3 bucket since PHP files need to be executed. (and that S3 can’t execute any files)

  • In this case, do I need to deploy my PHP files to both EC2 instances?
  • How do I make EC2 instances point to the same database so that all data is kept in once place? Right now, each EC2 instance holds its own MySQL database in its own EBS storage.
  • How does the end to end request flow look like? e.g. The client hits CloudFront. CloudFront talks to ELB. ELB talks to one of my EC2 instances. The EC2 instance connects to the common MySQL database and fetch data. Response data is returned in reverse order.

Am I understanding this correclty?

Advertisement

Answer

I figured CloudFront required an Elastic Load Balancer.

ELB is not required to use CF, but it simplifies few things, such as use of HTTPS between CF and your servers.

do I need to deploy my PHP files to both EC2 instances?

Yes, but this is not done “manually”. Instead it is done usually through the use of user data (or custom AMI) and auto scaling groups. So you would have to read up on these topics how to do it.

EC2 instance holds its own MySQL database in its own EBS storage.

Your DB should be outside of your web-servers. If you want to self-host it, then it would be dedicated instance. If you want to use managed solution, then you can migrate your MySQL to RDS MySQL.

How does the end to end request flow look like?

As you described.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement