AWS Linux

Setup a Load Balanced WordPress Website on AWS EC2 – Part 2

Setup a Load Balanced WordPress Website on AWS EC2. Learn how to setup your WordPress application to handle high traffic with auto-scaling capabilities on AWS with Elastic Load Balancing.

In this full guide you will install WordPress, configure your website to use AWS S3 for media files, create Amazon Machine Image, setup template, create a launch configuration, configure auto-scaling group to manage live traffic and setup RDS for your database.

Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Prerequisites

With the Part 1 you must have configured AWS EC2, RDS, Route 53 and installed WordPress.

Now in this part you will configure the remaining process to complete the high availability setup.

Setup AWS S3 bucket and configure Access Keys

Now you need to create a AWS S3 bucket and configure WordPress to use it.

Go to Services >> Storage >> S3 and click Create Bucket.

Enter Bucket name, choose the region.

Uncheck Block all public access.

Click Create bucket.

Configure IAM permissions

Go to Security, Identity, & Compliance >> IAM and navigate to Policies.

Click Create policy.

Click the JSON tab and paste the following.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:DeleteObjectTagging",
                "s3:ListBucketMultipartUploads",
                "s3:DeleteObjectVersion",
                "s3:ListBucket",
                "s3:DeleteObjectVersionTagging",
                "s3:GetBucketAcl",
                "s3:ListMultipartUploadParts",
                "s3:PutObject",
                "s3:GetObjectAcl",
                "s3:GetObject",
                "s3:AbortMultipartUpload",
                "s3:DeleteObject",
                "s3:GetBucketLocation",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::Bucket_name/*",
                "arn:aws:s3:::Bucket_name"
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "*"
        }
    ]
}

Replace Bucket_name with the name of the bucket you just created before.

Click Next >> Review.

Enter the Name for your policy.

Click Create policy.

Create IAM User and assign the policy

Now you need to create user and attach the policy you have created above.

Click Users on the left sidebar.

Click Create user.

Enter name of the user.

Check Programmatic access under AWS access type.

Click Next:Permissions.

Select Attach existing policies directly.

Check the policy you have created before.

Click Next: Tags and then click Next: Review on the screen that follows. Make sure everything is correct and click Create user.

Download the .csv file which has the Access Key and the Secret Key.

Configure WordPress to use S3

Once you have created user with the required permissions, you can login to your WordPress dashboard and navigate to plugins and install Media Cloud for WordPress.

Activate the plugin.

Go to plugin settings and choose Amazon web services as service provider.

Click Next.

Add the Access Key, Secret Key and the name of the bucket you created above.

Configure WordPress to use S3

Click Next, now the plugin will make some tests to make sure the configuration works fine.

You can complete the setup.

This plugin copies the uploaded media to S3 bucket you have created above.

Now it is time to configure Load balancer.

Modify WordPress for Elastic Load balancer

Edit the file and add the following below define('DB_COLLATE', ''); this line.

define('FORCE_SSL_ADMIN', true);
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
    $_SERVER['HTTPS']='on';

Configure HTTP to HTTPS redirect

Open your .htaccess file and add the below directives.

sudo nano /var/www/html/yourdomainname/public/.htaccess
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent] 

Save the file and exit the editor.

Request Amazon Managed SSL Certificate

Go to Services >> Certificate Manager.

Click Request a certificate.

Enter the domain names you wish to get certificate.

Click Next.

Follow the instructions to complete the validation.

Once validated you will receive your certificate.

Setup AWS Elastic Load Balancing

Go to EC2 >> Load Balancing >> Load Balancers and click Create Load Balancer.

Choose Application load balancer.

Enter Name for your load balancer.

Choose Scheme as Internet facing.

In the Listeners add a new listeners for HTTPS protocol.

In the Availability zones select any 2 zones.

Click Next: Configure Security Settings.

In Certificate type Choose a certificate from ACM.

Select the certificate you just created above.

Click Next: Configure Security Groups

Choose the security group you created while creating EC2 instance.

Click Next: Configure routing

Enter name for new target group.

Choose Target type as Instance.

Leave everything else to default and continue to create the load balancer.

Once the Load Balancer is created you need to configure forwarding rules in your listener.

Select your load balancer and go to the Listeners tab and click View/edit rules

Add a new redirect rule to forward HTTP traffic to HTTPS as shown in the screenshot below.

HTTP Forwarding rules

In the HTTPS rule you need to have your rule forwarded to the Target Group you created while creating load balancer.

HTTPS forwarding rules

Create Amazon Machine Image

Go to your EC2 dashboard and select the instance you were working on.

Click Actions >> Image and templates.

Click Create Image.

Create a Launch Configuration

Go to EC2 dashboard and navigate to Auto Scaling >> Launch Configurations.

Click Create launch configuration.

Provide a name for your configuration.

In the Amazon Machine Image choose the image you have created before.

In the Instance type choose the type you wish.

Under Security Groups choose Select an existing security group and check the security group that you have created while creating the instance.

Under Key pair, choose the key pair that you created while creating instance.

Click Create launch configuration.

Create Auto Scaling Group

Go to the EC2 dashboard and navigate to Auto Scaling >> Auto Scaling groups

Click Create auto scaling group.

Create Auto Scaling Group

Enter Name for your group.

In the Launch template click Switch to launch configuration.

Click Next.

In the Subnet select any one.

In the Advanced options, choose Attach to a existing load balancer.

Choose the target group you have created and attached with the load balancer.

In Group size you can enter your own values.

In Scaling policies choose Target tracking scaling policy.

Click Next.

You can configure notification if you need and complete the setup.

Configure Route 53 to Load Balancer

Go to Route 53 Hosted zones and click on your domain name.

Edit your A record and in Route traffic to choose Application load balancer, choose the region on which you created the load balancer and select your Load Balancer.

Click Save.

If you have enabled global accelerator in load balancing, you can choose the accelerator instead of the load balancer

Now you have your WordPress website to scale automatically during high CPU usage.

That’s for the part 1. In the next part you can configure AWS S3, configure WordPress to use S3 for media library, request AWS managed SSL, create load balancer, auto scaling group and point your domain to Load balancer.

Become a Certified AWS Professional with this easy to learn course now.

Conclusion

Now you have learned how to setup High availability WordPress on AWS with Elastic Load Balancing and auto-scaling the instances during traffic.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

If you find this helpful? Support me!
Buy me a coffee Donation Please buy me a coffee