AWS has upgraded their LightSail infrastructure to include Load Balancer for horizontal scaling. Here is a step by step guide on how to set up Load Balancer with your WordPress Site.
You can always upgrade your AWS LightSail server by starting a more powerful instance and moving your WordPress blog to it. Now they have enabled AWS Load balancer which can help you add more than one server in your WordPress setup.
But sometimes just adding more resources to a single server is not enough. AWS LightSail also has a limit, once you reach their biggest server, you can’t add more resources.
In this post, we will discuss how you should plan to scale your servers. You can use the below TOC to jump to a specific section.
Horizontal Scaling VS Vertical Scaling
Before we talk about the correct way of scaling and load balancer, we need to understand types of server scaling.
There are two scaling methods which you can use to support the increased traffic -Horizontal Scaling and Vertical Scaling.
It’s a method to increase resources on your server so that it can support the increased traffic.
For Example, if you are using AWS LightSail $5 instance which is handling around 100 people per day for you. If your traffic gets doubled, you will move to $10 instance. This is Vertical Scaling as you have added more RAM and Bandwidth to your server so that you can handle the extra traffic.
It’s a method to include additional servers in the mix, which can help you serve your website to a greater number of people.
For Example, if you are using AWS LightSail $5 instance which is handling around 100 people per day for you. If your traffic gets doubled, you add another $5 instance. So now, there is two $5 AWS LightSail instance which can handle your site requests and you can distribute your traffic to those servers. In this case, you can still serve twice the number of people, but you have used Horizontal Scaling.
Which one is good?
As you have seen in the above example, you can use either horizontal scaling or vertical scaling to handle the same situation. So, which one is good and should be used?
There is no correct answer. It depends on how you want to handle it and how much growth you are expecting for your site.
My personal preference is to handle initial few upgrades with Vertical Scaling. I prefer moving to bigger server till I reach $40 instance, after that, I will look for horizontal scaling.
It also depends on how much downtime you can tolerate. If anything happens to your server and you were using vertical scaling, your entire site will be down. With the horizontal scaling, your other server can still serve your website. It might have to take more load and response time will take some hit, but your site is still up.
So, your scaling strategy should be a good mix of horizontal scaling with vertical scaling. Remember, you can use a mix of servers in your horizontal scaling. Like one can be a $5 instance while other can be a $10 instance and you can divert the traffic accordingly. You can also scale both the server vertically as needed based on your traffic.
Join AWS LightSail Facebook Group
Connect with like minded people and help each other manage AWS LightSail WordPress installs.
How To Do Vertical Scaling
AWS LightSail comes with an easy method to do a vertical scaling. You can just create a snapshot of your current instance and start a bigger instance with the snapshot.
1) Go to Your AWS LightSail Panel and click on manage for the Instance you want to vertically scale.
2) Click On Snapshot and create a snapshot.
3) Once the snapshot is created, you can create a new instance with that snapshot. Just select the bigger instance and create it.
4) Now, you can attach the static IP of your site to the new instance.
5) Verify your site, if everything looks ok, you can delete your old instance.
That’s it, you now have your site running on a more resourceful server and can handle more traffic.
How To Do Horizontal Scaling With AWS Load Balancer
Vertical scaling is easy as you can just move to next available server and increase the resources. Horizontal scaling need bit more planning and understanding of what you are trying to achieve.
It’s not about adding an additional server in the setup, you need to define the roles and responsibility of each server.
Also, you need to make sure that your users should not see a difference in your site, irrespective of the server they land.
To do that, we need to separate the WordPress installation in such a way that multiple instances can still run as a single site. You have to divide WordPress into 3 different sections – Images, Database, and Core WordPress.
The idea is to have multiple servers with Core WordPress and a separate server for the database. Images can be stored on any cloud-based solutions so multiple instances can access it.
Once your WordPress database is separate, multiple servers can connect and display the same content. It will also help in case you are making an addition to the database like adding a new post. It will be visible to other servers instantly. Each Core WordPress server can do it’s own caching to server the site faster.
Here is a basic horizontal scaling diagram to explain the connections which we are planning to create. This will help you visualize what we are trying to achieve.
Now if we are clear with the setup, let’s see how we can implement it.
Enable VPC Peering
You need to enable the VPC Peering to use the other AWS services along with AWS LightSail. We will be using AWS RDS service to create a MySQL database for the WordPress.
You can go to AWS LightSail Account Page and enable the VPC peering from Advanced tab. Just check the box and wait for some seconds before it gets checked. Once done, you can access normal AWS services along with AWS LightSail instances.
WordPress Database Creation
There are two ways you can handle this. You can create a new LightSail instance and install the MySQL service on that or you can use AWS RDS (Relational Database Service) service.
I prefer AWS RDS Service as it allows you to scale your database also. You may want to start with a single server but can add a failover server so your database won’t go down.
There are two type of databases which you can use under AWS RDS – MySQL and Amazon Aurora (MySQL Compatible).
Amazon Aurora is a cloud-based implementation on MySQL database which provides 5 times faster response time than the standard MySQL server. It gives 99.99% uptime and built on fault-tolerant and self-healing storage. So I would suggest using Aurora rather than a regular MySQL instance.
Create Amazon Aurora Database
Once you select the Amazon Aurora in Amazon RDS, click on Next. You will be asked some details about the instance you are creating including the master account and password.
First, select the instance which you want to create for your database. Your charges will be based on the type of instance you use. You can calculate your monthly charges with the help of AWS Simple Monthly Calculator.
You also need to select if you want to create a replica of your database. In case of failure to your main server, it will automatically fall back on the replica. So you get a high availability for your data. Keep in mind that replicas are also chargeable, so only select if high availability is a must for you.
Once done, you need to provide additional information about the instance and root user.
The DB instance identifier is like a server name so you can identify this instance easily. Once done, click on Next.
You need to add some more advanced options in the next panel like access method, database name etc. Complete that and create a MySQL database server.
If you are still under the free tier usage, you can also think of installing a MySQL server on EC2 instance. It will save some money for you.
Install WordPress on AWS LightSail Instance
Once you have created an Amazon Aurora database, its time to install the WordPress. I have a detailed guide on how to install WordPress on AWS LightSail, you can follow it in case you need any help.
The only change you need is that you need to connect your WordPress installation to Aurora Database rather than installed on the instance.
You can edit the WP-Config.php file to change the database connection.
You can change the Database name, username, password, and hostname for your MySQL database. Just take the endpoint address of your Aurora instance, DB Name, User Name, and Password from your AWS Console and update the WP-Config.php.
Once you do that, go to your site and you will see with WordPress welcome page. Just enter details and let it create the WordPress tables in that dataset.
You don’t need to install the SSL certificate on the AWS Lightsail Instance as your certificate will be installed on Load Balancer.
Cloud Storage for Images
Next thing which you need to setup is a central storage for your WordPress images. By default WordPress store the images in the local directory but if you are going to use multiple servers for your site, you can’t have them save images on local storage.
If WordPress is saving the images on local storage, your images will be scattered across the instances. This will make images disappear randomly on pages based on which instance you are connecting to.
You need all WordPress server to be able to access all the images. So you should set up a cloud storage for your images.
Setup Amazon S3
To move your WordPress media to the Amazon S3 bucket, you need WP Offload S3 Lite WordPress plugin. You can install the plugin from the WordPress repository for free.
Once you have installed the plugin, you can connect it to AWS Account from the WordPress dashboard. This plugin copies any new image upload to AWS S3 bucket.
Once you installed the plugin, you can go into Settings -> Offload S3 to set the plugin.
The preferred way of doing this is by editing WP-Config.php. Copy the two line, add it at the end of WP-Config.php file. Replace the Access Key ID and Secret Access Key from your AWS account to make the connection.
Once the connection is made, you can select the S3 bucket from the WordPress dashboard. This S3 bucket will be used to copy your image files. You can also check some other settings like deleting the file from your local server after it is copied to the S3 bucket.
What about Themes and Plugins?
Themes and plugins are also going to face a similar problem as media. There are few options to handle them.
The first option is to do the updates manually through FTP. Download the plugin and upload the files with the help of FTP client. As plugin settings and data will be stored in the database, every instance will be able to pick it up correctly.
Other Option, You can delete all the instances except one and do the theme and plugin update. After that, start new instances with the snapshot of the updated instance. This way all your instances will have updates propagated. I would only recommend this method if you do not allow your users to log in.
Create Multiple WordPress Instance
Once your WordPress instance is correctly setup, you can create multiple instances to replicate it. You can use the snapshot method as described above to create those.
You don’t need to use the same capacity for those instances but I would recommend it to distribute evenly at the start. Later on, you can vertically scale your servers as required.
Create a Load Balancer
Now your WordPress Server, Database Server, and AWS S3 bucket are ready, so you can go ahead and create a Load Balancer. You can create a Load Balancer under Networking tab of AWS LightSail Home screen.
Just select the location, keet the HTTP for now and give it a meaningful name.
Now you need to add A type record in your AWS LightSail DNS zone so that the incoming traffic can come to Load Balancer. You can use this step by step instruction provided by AWS support guide to do that.
Once you have incoming traffic coming to Load Balancer, its time to let it know where to send them.
1) Go To Networking Tab and click on Manage the Load Balancer.
2) Attach the instance to Target Instances.
3) You can attach multiple instances to the load balancer and your incoming traffic will be distributed to those instances.
Enable HTTPS Port
By default load balancer servers the incoming traffic over HTTP. If you want to enable an SSL certificate for your site, you can do it on the Load Balancer. This certificate will be used for all instances so you don’t need to install it manually on each instance.
AWS support team has put together a good step by step guide on how to enable the SSL certificate. So go ahead and check the support document.
If you face any issue in following this step by step guide, let me know in comments and I will try to help.
Another thing which you need to check is session persistence. If you allow people to log in to your site or use WooCommerce plugin to sell things, you might want to turn this on.
Under inbound traffic tab, you will find a setting for Session Persistence. Just check it and Load Balancer will make sure logged in users will use the same instance for repeated visits.
This way if your users have saved something in their cart, they will see it constantly and will be able to purchase it easily. Otherwise, they sometime might not see things in the cart as they will be served pages from other instances.
So this is how you can do the horizontal scaling with the help of load balancer in AWS LightSail.
There can be many different server configurations that can be done with horizontal scaling. I have tried to explain a simple setup so you have an idea of how to go about it. Now go ahead and design Horizontal Scaling strategy for your AWS LightSail Site with the help of Load Balancer.