Host WordPress on AWS ECS using Fargate

Host WordPress on AWS ECS using Fargate

Avinash Dalvi's photo
Avinash Dalvi
·Nov 16, 2022·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Hello Devs,

In this blog I'm going to cover how to host WordPress websites on AWS ECS service using AWS Fargate. Let me tell you why this is needed and why not to use EC2 for hosting Wordpress.

Few days back I was working with my friend to enhance the performance of the WordPress website. He was facing timeout errors frequently due to memory exhaustion. Reason behind this is he has configured cron job ( PHP scripts ) which was running every 5 minutes and eating more memory due to this EC2 was getting dead state and getting timeout. After this problem I have done some research and talked to a few AWS community builders then came to conclusion to host WordPress site on ECS cluster where can run more than one task can accommodate separate infra for running main website and another task can run separate infra for running cron jobs. In this way load can be divided and easily manageable. Both activities will not disturb each other.

This is the reason for selecting AWS ECS service over EC2 to enhance performance and be more resilient.

Lets understand first perquisites:

Docker - Docker helps you to separate out your application from infra so that you can deliver the final product faster. This is exactly similar to making 2 minutes of instant noodles. While cooking instant noodles we have to open the package and put it into hot water and then after 4-5 minutes it will be ready to eat. An Exactly similar Docker container is the instant noodles package which we have to just configure to infra and run apps. More details can find here docs.docker.com/get-started/overview

ECR - AWS ECR ( Elastic container registry ) is an AWS service which enables us to keep a registry of docker images. We can store any docker image and pull inside AWS services whenever it requires.

ECS - AWS ECS ( Elastic container service ) is a service where you can deploy any docker image or host any code inside a container and enable us via 80 or 443 port to view in the browser using ALB ( Application load balancer ). This is like boiled water where you can put multiple instant noodle packets similarly in AWS ECS cluster can add more number tasks which run on each container. More details can find here aws.amazon.com/ecs

AWS Fargate - help you to focus on building applications without managing servers. AWS Fargate removes the operational overhead of scaling, patching, securing, and managing servers. AWS Fargate is an instant noodle masala packet which removes our headache of making noodles tastier. More details can find here aws.amazon.com/fargate

banetti-banetti-market.gif

AWS ECS components :

  • Clusters
  • Service
  • Tasks
  • Task definition
  • Container Agent

ECS_Component.drawio.png

Let's understand what to do.

Step 1 : Create ECR entry in AWS console. Go to AWS console -> ECR -> Create repository -> Click on Create

Screenshot 2022-11-16 at 11.41.16 PM.png Screenshot 2022-11-16 at 11.41.28 PM.png

Once it gets created click on "View push commands" as shown below image.

Screenshot 2022-11-16 at 11.44.08 PM.png

Step 2: Run aws configure command on your terminal if AWS credential is not configured in your macOS or Linux machine. Make sure AWS IAM users which are going to use it should have access to ECR. Then following command

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin {ACCOUNTID}.dkr.ecr.us-east-1.amazonaws.com {ACCOUNTID} - this will be your AWS account ID.

Output will be like :

Screenshot 2022-11-16 at 11.49.34 PM.png

Step 3: Get Docker image from official docker page hub.docker.com/_/wordpress. Run this command docker pull wordpress make sure your Docker desktop is installed and running on your machine.

Step 4: Push image to ECR. Run this command docker tag wordpress {ACCOUNTID}.dkr.ecr.us-east-1.amazonaws.com/sample-wordpress:latest then docker push {ACCOUNTID}.dkr.ecr.us-east-1.amazonaws.com/sample-wordpress:latest After this command docker image will be available under AWS ECR. Copy this URI from the ECR console.

Screenshot 2022-11-17 at 12.00.04 AM.png

Step 5: Go to AWS Console -> ECS -> Create custom cluster

Screenshot 2022-11-16 at 11.58.42 PM.png

Click on configure and paste the value of URI to the second field. Give the name to cluster as "sample-wordpress" and enter 80 port values. Other configurations are optional based on use case basis.

Screenshot 2022-11-17 at 12.01.42 AM.png

Screenshot 2022-11-17 at 12.08.46 AM.png Click on edit if any change needs to be done like name of task definition, memory, cpu etc. Screenshot 2022-11-17 at 12.09.22 AM.png

Click on next and select ALB mode.

Screenshot 2022-11-17 at 12.10.46 AM.png

Hit the create button and wait for final completion of creation.

Screenshot 2022-11-17 at 12.11.23 AM.png

Once it finished all creation process it will show status like this

Screenshot 2022-11-17 at 12.14.00 AM.png

Now, click on view services. Review created service and their task.

Screenshot 2022-11-17 at 12.15.40 AM.png

Screenshot 2022-11-17 at 12.16.05 AM.png

You can click on the target group and review its target group configuration.

Screenshot 2022-11-17 at 12.18.33 AM.png

Step 6 : Find out URL to check WordPress website. Go to EC2 console -> Elastic Load balancer -> Copy URL

Screenshot 2022-11-17 at 12.20.24 AM.png

Note : You need to configure a health check URL to keep the task healthy. If you don't do this task will keep creating after going to an unhealthy state due to lack of health check URL. Temporary until WordPress installation finishes you can set /wp-admin/setup-config.php as a health check URL.This is just a WordPress setup. For running a full WordPress website you need to set up an RDS instance for database connection.

Screenshot 2022-11-17 at 12.33.58 AM.png

Finished!

Screenshot 2022-11-17 at 12.30.41 AM.png

I hope this blog helps you to learn. Feel free to reach out to me on my twitter handle @AvinashDalvi_ or comment in the blog. If you like my blog please don't forget to like the article. It will encourage me to write more helpful articles. Stay tune for next blog will come up different use case with ECS.

References :

Did you find this article valuable?

Support Avinash Dalvi by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this