Create a Load Balanced WordPress Website

in Amazon Lightsail

Amazon Lightsail is the easiest way to get started on AWS. It offers virtual servers, storage, databases and networking, plus a cost-effective, monthly plan.

This tutorial shows you how to create a load balanced WordPress website in Amazon Lightsail. A load balancer is a server that distributes network traffic over a set of servers. By distributing network traffic to a pool of servers, you can dramatically improve the number of concurrent users your WordPress website can handle. Load balancers also add fault tolerance. The Lightsail load balancer ensures that only healthy WordPress instances attached to the load balancer receive traffic.

You can horizontally scale a WordPress website by increasing the number of instances attached to a load balancer. You can also vertically scale a WordPress website by replacing the instances attached to a load balancer with instances that use a bigger Lightsail plan (e.g., use the $5 USD per month plan, instead or the $3.50 USD per month plan). The vertical scaling scenario is not covered in this tutorial, but can be achieved using similar steps.

You complete the following steps in this tutorial:

  • Create a MySQL managed database in Lightsail
  • Create an IAM user for your WordPress instance
  • Connect to your WordPress instance via SSH to configure the managed database and IAM user credentials
  • Sign in to the dashboard of your WordPress website, and install the WP Offload Media plugin
  • Duplicate your WordPress instance
  • Create a load balancer in Lightsail, and attach your WordPress instances

Get started with Amazon Lightsail for free. 

About this Tutorial
Time 60 minutes                                           
Cost Costs Apply
Use Case Compute
Products Amazon Lightsail
Audience Developer
Level Intermediate 
Last Updated April 16, 2020

Already have an account? Sign-in

1. Create a WordPress instance in Lightsail

This tutorial requires that you create a WordPress instance in Lightsail. To do so, follow the steps in the Launch and configure a WordPress instance with Amazon Lightsail tutorial.

2. Create a MySQL managed database in Lightsail

Complete the following steps to create a MySQL managed database in Lightsail. The new MySQL managed database will serve as the central database for all of the WordPress instances that will be load balanced.


a. Sign into the Lightsail console


b. On the Databases tab of the Lightsail home page, choose Create database.

lb1

c. Choose the AWS Region and Availability Zone for your database.

Note: The database you create must be in the same region as your WordPress instance.

lb2

d. Choose the latest version of MySQL as your database engine. PostgreSQL is available, but it’s not compatible with WordPress.

lb3

e. Choose a database plan.

A plan includes a low, predictable cost, machine configuration (RAM, SSD, vCPU), and data transfer allowance. You can try the $15 USD Lightsail standard database plan without charge for one month (up to 750 hours).

lb4

f. Enter a name for your database.

Resource name guidelines:

  • Must be unique within each AWS Region in your Lightsail account.
  • Must contain 2 to 255 characters.
  • Must start and end with an alphanumeric character or number.
  • Can include alphanumeric characters, numbers, periods, dashes, and underscores.
lb5

g. Choose Create database.

Your database will take a few minutes to create.


h. After your database is created and available, choose the name of the database under the Databases tab in the Lightsail console.


i. Complete the following steps in the Connect tab of your database management page:

  1. Make note of the database DNS name. You will need this later in this tutorial.
  2. Make note of the database user name. You will need this later in this tutorial.
  3. Choose Show password, and choose Change password. Enter a new strong password that does not contain special characters. You must change the password to remove special characters because some of the commands later in this tutorial will not function as expected if special characters are used in the password.
  4. Make note of the database password. You will need this later in this tutorial.
hawp2i

3. Create an Amazon S3 bucket for your WordPress instance

Complete the following steps to create an Amazon S3 bucket that will be used to store the media from your WordPress website.


a. Sign in to the Amazon S3 console.


b. Choose Block public access in the left navigation menu.


c. Ensure that both options to block public access through access control lists (ACLs) are off. If they are on, then choose edit, de-select those options, and choose Save changes. Those options are required for WordPress to access the Amazon S3 bucket you create.

Note: These settings are applied to all S3 buckets, so ensure that these settings can also be applied to your other existing S3 buckets.

update1
update2

d. Choose Buckets in the left navigation, then choose Create bucket.

update3

e. Give your bucket a name. All S3 bucket names are globally unique, so you cannot use the same name as the one in this tutorial.

Make a note of the name you choose, you will need to enter it into your WordPress configuration later in this tutorial.


f. Choose the AWS Region for your S3 bucket. You should create all of your resources for this tutorial in the same AWS Region, so create your S3 bucket in the same region as your WordPress instance.

update4

g. Deselect Block all public access, and then choose Block public access to buckets and objects granted through new public bucket or access point policies, and Block public and cross-account access to buckets and objects through any public bucket or access point policies. Ensure that both options to block public access through access control lists (ACLs) are deselected.


h. Choose to acknowledge the settings you have selected.

update6

i. Choose Create bucket.


3. Create an IAM policy and user for your WordPress instance

Complete the following steps to create a policy for the Amazon S3 bucket you created, and then create an IAM user with the policy that can access the S3 bucket. Later in this tutorial, you will configure your WordPress instance with the IAM user’s credentials.

a. Sign in to the IAM console.


b. Choose Policies in the left navigation, then choose Create policy.

c. In the Create Policy page, choose the JSON tab, then paste the following policy into the text box. Be sure to replace your bucket name with the name of the Amazon S3 bucket you created earlier in this tutorial.

The result should look like the following picture.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObjectAcl",
                "s3:GetObject",
                "s3:PutBucketAcl",
                "s3:ListBucket",
                "s3:DeleteObject",
                "s3:GetBucketAcl",
                "s3:GetBucketLocation",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::<your bucket name>",
                "arn:aws:s3:::<your bucket name>/*"
            ]
        }
    ]
}
update7

d. Choose Review policy.

e. Give your policy a name, such as wp-s3-policy, then choose Create policy.

You now have a policy that you can apply to a new IAM user. In the next set of steps, you will create a new IAM user

update8

f. Choose Users in the left navigation. Then choose Add user.

lb7

g. Enter a user name.

Ex: wp-s3-user


h. Choose Programmatic access.

i. Choose Next: Permissions.

lb8

j. Choose Attach existing policies directly.


k. Search for the name of the IAM policy you created earlier in these steps, such as wp-s3-policy.

 


l. Choose the policy you created in the search results.

m. Choose Next: Tags.

 

update10

n. You don’t need to add tags to the IAM user. Choose Next: Review.


o. Review the user details, then choose Create user.

lb10

p. Copy the user’s access key ID, and paste it to the credentials text file you created earlier in this tutorial.

lb11

q. Choose Show under the Secret access key column.


r. Copy the secret access key, and paste it to the credentials text file.


s. Save the credentials text file to a secure location. You will need these credentials later in this tutorial.


t. Choose Close.

You are now done with the IAM console and you can close it.

4. Connect to your WordPress instance via SSH to configure the managed database and IAM user credentials

Complete the following steps to export the data from the MySQL database on your WordPress instance, and import it to your new MySQL managed database in Lightsail. You will also configure your WordPress instance to connect to the MySQL managed database, and configure the IAM user credentials in your WordPress instance.


a. Sign in to the Lightsail console.


b. On the Instances tab of the Lightsail home page, choose the browser-based SSH client icon for your WordPress instance.

lb12

c. After the browser-based SSH client window opens, enter the following command to retrieve the Bitnami default password for the applications on your WordPress instance:

cat $HOME/bitnami_application_password

        d. Copy the password that is displayed, and paste it to the text file where you are         storing your credentials.

lb13

e. Enter the following commands one by one to establish environment variables with the information you copied and pasted to your credentials text file.

LSDB_USERNAME=<DatabaseUserName>
LSDB_ENDPOINT=<DatabaseEndpoint>
LSDB_PASSWORD='<DatabasePassword>'
ACCESS_KEY=<AccessKeyID>
SECRET_KEY=<SecretAccessKey>

Note: Be sure to put a single quote before and after the LSDB_PASSWORD parameter.

For example:

LSDB_USERNAME=dbmasteruser
LSDB_ENDPOINT=ls-24e932a56656a5b471398ea052a04bc32ea0703d.czowadgeezqi.us-west-2.rds.amazonaws.com
LSDB_PASSWORD='3stTVwnfJh9CnKR6'
ACCESS_KEY=AKIAIOSFODNN7EXAMPLE
SECRET_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

f. Enter the following command to create a dump of the MySQL database that is on your WordPress instance. This command creates a dump.sql file in the current directory.

mysqldump -u root --databases bitnami_wordpress --single-transaction --order-by-primary -p > dump.sql

g. Enter the Bitnami default password when prompted for a password. This is the Bitnami default password for the applications on your WordPress instance, which you obtained earlier in these steps.


h. Enter the following command to import the dump.sql file to your MySQL managed database in Lightsail.

cat dump.sql | mysql --user $LSDB_USERNAME --host $LSDB_ENDPOINT -p

i. Enter the MySQL managed database password when prompted for a password. This is the password for your MySQL managed database that you got from the Lightsail console earlier in this tutorial.


j. We will modify the wp-config.php file in the next set of steps. Before proceeding, enter the following command to create a backup of the wp-config.php file in case something goes wrong.

cp /home/bitnami/apps/wordpress/htdocs/wp-config.php /home/bitnami/apps/wordpress/htdocs/wp-config.php.bak

k. Enter the following command to get the password for the MySQL database on the instance from the wp-config.php file, and set it as a WPDB_PASSWORD environment variable.

WPDB_PASSWORD=`cat /home/bitnami/apps/wordpress/htdocs/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`

l. Enter the following command to add the port to your Lightsail database endpoint environment variable.

LSDB_ENDPOINT=$LSDB_ENDPOINT:3306

m. Enter the following command to use the WordPress command line to add the MySQL managed database user name, password, and endpoint to the wp-config.php file.

wp config set DB_USER $LSDB_USERNAME && wp config set DB_PASSWORD $LSDB_PASSWORD && wp config set DB_HOST $LSDB_ENDPOINT

n. Enter the following command to create a credfile.txt that contains the credentials of the IAM user you created earlier in this tutorial.

cat <<EOT >> credfile.txt
define( 'AS3CF_SETTINGS', serialize( array (
    'provider' => 'aws', 
    'access-key-id' => '$ACCESS_KEY', 
    'secret-access-key' => '$SECRET_KEY',
) ) );
EOT

o. Enter the following command to insert the contents of the credfile.txt into the wp-config.php file.

sed -i "/define( 'WP_DEBUG', false );/r credfile.txt" /home/bitnami/apps/wordpress/htdocs/wp-config.php

p. Enter the following command to delete the credfile.txt, because you no longer need it. At this point, you can also delete the credentials file that contains all of the credentials that you copied and pasted earlier in this tutorial.

sudo rm –r credfile.txt

q. The previous set of commands reset the permissions for the wp-config.php file. Enter the following command to correct the permissions for the wp-config.php file, and the backup that you created.

sudo chown bitnami:daemon /home/bitnami/apps/wordpress/htdocs/wp-config.php && sudo chown bitnami:daemon /home/bitnami/apps/wordpress/htdocs/wp-config.php.bak

r. Enter the following command to restart the services on the instance.

sudo /opt/bitnami/ctlscript.sh restart

You can close the browser-based SSH client window. Your WordPress instance is now configured to connect to your new MySQL managed database in Lightsail. When you publish new posts to your WordPress website, the data for those posts is written to the MySQL managed database in Lightsail, instead of the MySQL managed database that is on the instance.

5. Sign in to the dashboard of your WordPress website and install the WP Offload Media plugin

Complete the following steps to install the WP Offload Media plugin on your WordPress website. The WP Offload Media plugin creates an Amazon S3 bucket in your AWS account using the IAM credentials configured in the previous section of this tutorial. It also connects your WordPress to the new S3 bucket so that media files uploaded to your website are saved to your new S3 bucket. The new S3 bucket will serve as the central media files location for all of the WordPress instances that will be load balanced.


a. Browse to your instance. For example, http://192.0.2.0.

You can get your instance's public IP address from the Lightsail console as shown in the following image:

lb14

b. Choose the Bitnami manage icon in the bottom, right corner of the page

lb15

c. Choose Login.

lb16

d. Enter user in the Username text box, and enter the Bitnami default password in the Password text box. Then, choose Log in.

lb17

e. Hover your cursor over Plugins in the left navigation, then choose Add new.

lb18

f. Enter WP Offload Media in the search text box, and press Enter.

lb19

g. In the search results, choose Install Now next to the WP Offload Media Lite plugin.

lb29

h. Choose Activate after the plugin is installed.

lb21

i. After the plugin is activated, hover your cursor over Settings in the left navigation, and choose Offload Media Lite.

lb22

j. In the Offload Media Lite settings page, enter the name of the S3 bucket you created earlier in this tutorial into the Bucket text box.

Then, choose Save Bucket Setting.

update11

The Offload Media Lite page confirms your settings were saved.

Your WordPress website is now configured to upload media files to your Amazon S3 bucket. The new central S3 bucket for your WordPress media files, together with the new central MySQL managed database in Lightsail, means that you can now duplicate your WordPress instance, and each duplicate version of your instance will refer to the S3 bucket and the MySQL managed database. Continue to the next step to duplicate your WordPress instance in Lightsail.

Picture1

6. Duplicate your WordPress instance

Complete the following steps to create a snapshot of your WordPress instance, and then use the snapshot to create a duplicate of your instance.


a. Sign in to the Lightsail console.


b. On the Instances tab of the Lightsail home page, choose the name of your WordPress instance.

lb26

c. In your instance management page, choose the Snapshots tab. 

d. Choose Create snapshot, and choose Create.

lb28

e. When the snapshot is done creating, choose the ellipsis (⋮) icon, and choose Create new instance.

lb29real

f. In the Create an instance from a snapshot page, don't change the default selections. Scroll to the bottom of the page, and choose Create instance.

This creates an exact duplicate of your original WordPress instance, which is already configured to connect to the MySQL managed database and the Amazon S3 bucket created earlier in this tutorial.

Repeat the steps in this section of the tutorial to create as many instances as you will need load balanced. Continue to the next section of this tutorial to create a load balancer in Lightsail and attach your instances.

lb30
lb31

7. Create a load balancer in Lightsail and attach your WordPress instances

Complete the following steps to create a load balancer in Lightsail, and attach your WordPress instances.


a. Sign in to the Lightsail console.


b. On the Lightsail home page, choose the Networking tab.


c. Choose Create load balancer.

lb32

d. On the Create a load balancer page, give your load balancer a name, and choose Create load balancer.


e. Choose the name of your load balancer.

lb33

f. In the Target instances tab, under the Target instances section of the page, choose the dropdown menu, and choose one of your WordPress instances. Choose Attach to attach the instance.

lb34

g. Choose Attach another to attach another instance to the load balancer.

Attach all of the duplicate WordPress instances to the load balancer.

lb35

h. At the top of the Load balancer management page, copy the load balancer's DNS name.

lb36

i. Open a new browser window, paste the DNS name to the address text box, and press enter.

Your request is routed through the load balancer, and your WordPress website loads. The website that loads is from one of the WordPress instances attached to your load balancer. At this point, your load balancer is configured to route to one of your duplicate WordPress instances. To complete the process, add your load balancer's DNS name as an address (A) record to your registered domain's DNS.

Congratulations

You have created a load balanced WordPress website in Lightsail.

Amazon Lightsail is great for developers, WebPros, and anyone looking to get started on AWS in a quick and cheap way. You can launch instances, databases, SSD-based storage, transfer data, monitor your resources, and so much more in a managed way.

Was this tutorial helpful?

Thank you
Please let us know what you liked.
Sorry to disappoint you
Is something out-of-date, confusing or inaccurate? Please help us improve this tutorial by providing feedback.