WordPress on AWS: you are holding it wrong
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 properly.
The following figure shows the services that are needed to build a highly available and scalable WordPress environment on AWS for approximately 75.00 USD per month.
The following sections will explain the concepts and services used in detail. You will find a template that you can use to spin up the necessary resources on AWS within minutes at the end of this article.
A stateless server is the pre-requisite for building a highly available and scalable infrastructure on AWS. A stateless server does not store any data expect of temporary data like caches.
By default WordPress is storing data in two different ways:
- MySQL database: articles, comments, users and parts of the configuration are stored in a MySQL database.
- File system: media files uploaded by the authors are stored on the file system.
If the MySQL database is running on the same EC2 instance as the WordPress application itself, the server is not stateless. Same is true for the media files stored on the file system.
Why is this a problem? Because if the virtual machine becomes unavailable, the data will be unavailable, too. And if you need to add another EC2 instance to handle more traffic all the data will be missing on the additional server.
A stateless server is the easiest way to build a highly available and scalable WordPress environment. You can achieve a stateless server by outsourcing the state of your virtual machine as described in the following sections.
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.
- Article Prevent outages with redundant EC2 instances
- Article Building blocks for highly available systems
- Article Introducing the Object Store: S3
- Article 5 AWS mistakes you should avoid
- Tag cloudformation
- Tag ec2
- Tag rds