# Deploy a Highly Available WordPress Application

In this final project, we will create a highly available (HA), scalable and fault-tolerant deployment of the WordPress application. You will deploy the WordPress application in such a way that the application server, load balancer and database will scale independently of one another. You will also deploy the application's components like the web server and database into two availability zones to distribute it and guard against failure of the anyone availability zone. The WordPress application will be deployed in a stateless fashion so that we can add or remove web application servers in response to the requests flowing into the system. Finally, we create a CloudFront distribution as CDN and change the configuration of WordPress.

## **Project Details**

### **Step 1: Create WordPress AMI (Amazon Machine Image)**

1 . Visit the EC2 instance page, and select the public WordPress instance created in the last assignment. In case you deleted it, you need to follow all steps to create an EC2 instance with a WordPress assignment. That we are going to use to create Amazon Machine Image.

* Click Actions → Image and templates → Create image
    
* Enter the Image name and click Create image
    

### Step 2: Create an Application Load Balancer

* Make sure you are in the right region to complete this project and stick with the same region all the time till the end of this project.
    
* Visit EC2/Load Balancing/Load Balancers
    
* Click Create Load Balancer
    
* In Step 1: Select load balancer type, find Application Load Balancer and click Create
    
* In the Basic Configuration section, enter the name WordPress-alb
    
* In Availability Zones, for VPC, choose Vpc created in the last assignment or create a new one.
    
* For Availability Zones, select PublicSubnet0 and PublicSubnet1
    
* In Step 2: Configure Security Settings, click Next
    
* In Step 3: Configure Security Groups, for Assign a security group, choose Create a new security group
    
* For the Security group name, enter alb-sg and click Next Configure Routing
    
* Step 4: Configure Routing
    
* For Target Group, select New target group
    
* For Name, enter wordpress-tg
    
* For Target type, select Instance and click Next: Register Targets
    
* In the Register Targets stage, click Next: Review
    
* In the Review stage, click Create
    

### Step 3: Create Auto Scaling Group

Visit EC2/Network & Security/Security Groups Click Create Security group

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683490160411/e5d8758b-8e4a-44dd-86bf-089751eabb1e.png align="center")

* For **Security group name**, enter asg-sg
    
* For **Description**, enter asg-sg
    
* In **Inbound rules** section
    
* Click **Add rule**
    
* For **Type**, select **HTTP**
    
* For **Source**, select **Custom** and find **alb-sg**
    
* Click **Add rule again**
    
* For **Type**, select **MYSQL/AURORA**
    
* For **Source**, select **Custom** and find **db-sg**
    
* Click **Create a security group**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683490216247/fc23a4e0-afc7-4f9a-8dee-f5f1a91561e4.png align="center")

* Visit **EC2/Network & Security/Security Groups**
    
* Find **db-sg** and click its **Security group ID**
    
* Click **Edit inbound rules**
    
* Click **Add rule**
    
* For **Type**, select **MYSQL/AURORA**
    
* For **Source**, select **Custom** and find **asg-sg**
    
* Click **Save Rules**
    
* Visit **EC2/Auto Scaling/Launch Configurations**
    
* Click **Create Launch configuration**
    
* For **name**, enter wordpress
    
* For **AMI**, choose AMI created in last step
    
* For **Instance type**, search and select **t2.micro**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683490246931/b622ff49-9eb4-4a72-90c4-dc14c9ed3935.png align="center")

* In the **Additional configuration** section, click **Advanced details**
    
* For **User data**, select **As text** and enter the script below
    
    ```bash
    
    
    #!/bin/bash
    yum update -y
    sudo service httpd restart
    ```
    
* For **Security group**, select **Select an existing security group** and select **as-sg** just created
    
* For **Key pair options**, select **Choose an existing key pair**
    
* For the **Existing key pair**, select the key created in Lab 1
    
* Finally, click **Create launch configuration**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683490926387/3505ac12-f0ac-439a-be90-a3be70fa37cb.png align="center")

* For **Security group**,select **Select an existing security group** and select **asg-sg** just created
    
* For **Key pair options**, select **Choose an existing key pair**
    
* For **Existing key pair**, select the key created in Lab 1
    
* Finally, click **Create launch configuration**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683490979535/8945e2c0-3b70-48e9-bc38-95c1218d7df6.png align="center")

* Visit **EC2/Auto Scaling/Auto Scaling Groups**
    
* Click **Create an Auto Scaling Group**
    
* For **Auto Scaling group name**, enterwordpress-sg
    
* For **Launch template** section, click **Switch to launch configuration** and select the launch configuration created in last step and click **Next**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491089329/8be59fb1-bb43-4864-9818-566cdda52bab.png align="center")

* In **Configure setting** stage,
    
* For **Vpc**, select **Vpc / vpc-stack**, created by CloudFormation
    
* For **Subnets**, select **WebSubnet0 / vpc-stack** and **WebSubnet1 / vpc-stack**, then click **Next**
    
* In **Configure advanced options** stage
    
* For **Load balancing**, select **Attach to an existing load balancer**
    
* For **Existing load balancer target groups**, select **alb-tg**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491150315/eb6ec94a-977a-49bb-9478-ab4fc06bc29e.png align="center")

* In **Configure group size and scaling policies** stage
    
* In **Group size - optional** Section
    
* For **Desired capacity, Minimum capacity, Maximum capacity**, enter **2,2,3** then click **Next**
    
* In **Add notifications** and **Add tags** sections, click **Next**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491278116/83d68da0-789b-49b5-b7b6-1788c051ddf2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491307272/6c5b6f3b-2979-40b0-beb1-775f52ee814a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491322182/4ee52377-4595-4d64-8f42-90bc55c21b72.png align="center")

In **Review** sections, click **Create Auto Scaling group**

### Step 4: Create CloudFront Distribution

Create distribution manually

* Visit CloudFront console, and click Create distributions, choose Web for delivery method
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491385548/e0869cb2-bb92-4a9f-83bc-8e328bd56f38.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491418086/2d2e63c6-2175-4ecd-a8f3-3bf3ccd119d4.png align="center")
    
    * In **Default Cache Behavior Settings**
        
    * For **Origin Protocol Policy**, select **Redirect HTTP to HTTPS**
        
    * For **Allowed HTTP Methods**, select **GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE**
        
    * For **Cached HTTP Methods**, select **GET, HEAD, OPTIONS**
        
    * For **Cache and origin request settings**, select **Use legacy cache settings**
        
    * For **Cache Based on Selected Request Headers, select Whitelist**
        
    * For **Whitelist Headers**, search and add **Host** and **Origin**
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491610545/90fe8cb6-c10e-4532-8993-d4cb341e339b.png align="center")
    
    * For **Object Caching**, select **Customize**
        
    * For **Minimum TTL**, enter 0
        
    * For **Maximum TTL**, enter 31536000
        
    * For **Default TTL**, enter 300
        
    * For **Forward Cookies**, select \*\*comment\_author\_\*, comment\_author\_email\_\*, comment\_author\_url\_\*, wordpress\_\*, wordpress\_logged\_in, wordpress\_test\_cookie, wp-setting-\*\*
        
    * For **Query String Forwarding and Caching**, select **Forward all, cache based on all**
        
    * For **Smooth Streaming**, select **No**
        
    * For **Restrict Viewer Access**, select **No**
        
    * For **Compress Objects Automatically**, select **Yes**
        
    * Finally, **Create Distribution**
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491665609/d434969f-11d8-4555-9cca-c33c057427af.png align="center")
    
    * Visit CloudFront Distribution page
        
    * Click the **distribution ID** created in last step
        
    * Click **Origins and Origin Groups** tab, and click **Create Origin**
        
    * For **Origin Domain Name**, select **S3 bucket** created in Lab 1 and Click **Create**
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491702323/bcdf40a5-2d24-471b-9079-214a71b2116b.png align="center")
    
    * Next, move to **Behavior** in your Distribution and click **Create Behavior**, follow the table below to create 4 new behaviors:
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491757543/345a9e13-e016-4305-843c-0782efb32e77.png align="center")
    
    **Test & access your blog with Cloudfront DNS.**
    
* ### Visit **RDS console**
    
* Click **Databases** in left menu,
    
* Click **Actions** -&gt; **Create read replica**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491823309/9e23b0ca-8c62-4241-bc40-c1a07c6d2437.png align="center")

* Enter the wp-read-replica for **DB instance identifier**
    
* Click **Create**
    
* Go back to databases page, you will see the read replica instance now. After minutes, it will be created successfully.
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683491847896/b3221559-cd80-4c9e-9289-f294fd631a2d.png align="center")
    
    REFERENCE
    
    * [Deploy WordPress with Amazon RDS](https://aws.amazon.com/getting-started/hands-on/deploy-wordpress-with-amazon-rds/?nc1=h_ls)
        
    * [Hosting WordPress on AWS](https://github.com/aws-samples/aws-refarch-wordpress)
        
    * [How to Accelerate Your WordPress Site with Amazon CloudFront](https://aws.amazon.com/blogs/startups/how-to-accelerate-your-wordpress-site-with-amazon-cloudfront/)
        
    * [Best Practices for WordPress on AWS](https://d1.awsstatic.com/whitepapers/wordpress-best-practices-on-aws.pdf)
        
    * [Deploy and Scale a LAMP stack application on Amazon Lightsail](https://www.lightsailworkshop.com/introduction.html)
        
    
    Attachment
    
    * [example-wp-config.php](https://general-webapp.workshop.aws/Reference/_index.en.files/example-wp-config.php) (2 kb)
        
    * [putty\_setup.pdf](https://general-webapp.workshop.aws/Reference/_index.en.files/putty_setup.pdf) (957 kb)
