Horizontally scale? Yes, for either a WordPress blog or website.
Configuration recommendations before using a Lightsail load balancer
- Separate your database so that every WordPress instance running behind the load balancer is storing and retrieving information from the same place. If you need more performance from your database, you can replicate or change the processing power or memory independently of your web server.
- Use a WordPress plugin that lets you store your files in Amazon S3. This gives you a centralized place for your content or images, rather than keeping separate copies on each target instance. That way, if you edit your content or change an image, the updates get picked up from the centralized store and your users see the same content, regardless of which instance they hit.
RDS: managed MySQL database
WordPress uses a MySQL database to store articles, comments, users and configuration. You can use RDS (Relational Database Service) to spin up a managed MySQL database on AWS. RDS supports master-standby setups (called Multi-AZ) offering an easy way to achieve high availability for MySQL databases.
S3: media file storage
By default, WordPress uses the file system to store media files (images, videos, …) on the disks attached to the virtual machine. Unfortunately, this is a show stopper if you want to build a stateless server. You can use a plugin for WordPress to store media files on S3. Amazon S3 is a highly available and scalable object store offered by AWS and can be used to outsource media files.
Outsourcing the state to RDS and S3 allows you to achieve the concept of a stateless server for your web servers. The next challenge is to manage a dynamic number of virtual machines to serve the incoming HTTP requests.
ELB: synchronous decoupling
Scaling horizontally means adding more virtual machines to handle traffic peaks. To allow multiple servers to answer HTTP(S) requests in parallel you need to distribute the workload. A load balancer solves this problem for synchronous requests (e.g. HTTP requests). AWS offers a highly available and scalable load balancer: Elastic Load Balancer (ELB).
Auto Scaling based on usage
The big advantage of AWS and other public cloud providers is the possibility to add and remove capacity on demand. AWS offers a feature called Auto Scaling helping you to spin up and terminate virtual machines based on the current workload. For example, the number of EC2 instances can be increased if the CPU load reaches 75%.
Auto Scaling will also replace failed EC2 instances even if a whole datacenter (availability zone) fails.
CloudFormation template available
Sounds like a lot of work to create a highly available and scalable WordPress setup on AWS? Not if you are using the right tool for the job: AWS CloudFormation. I’m offering a template that you can use to create the described WordPress setup automatically within minutes.
You will find the CloudFormation template to create a highly available and scalable WordPress setup on GitHub: widdix/aws-cf-templates.
WordPress is a very popular blogging software. You can run the PHP application together with a MySQL database on an EC2 instance (virtual machine) easily. But if you are following the official tutorial to setup your blogging environment on AWS you are missing two important advantages:
- High Availability: a single EC2 instance is not highly available. A failing host system or datacenter will cause an outage of your virtual machine and therefore of your WordPress blog.
- Scalability: a single EC2 instance is not scalable. If one of your blog posts gets attention from Hacker News or another social media source, you are not able to serve all the incoming requests properl