Hosting a Scalable WordPress Architecture on AWS

In this post, I will describe how to host a scalable and high availability WordPress Architecture using AWS resources. WordPress is a feature rich CMS, which is extremely popular. We will use the key AWS services such as ElastiCache, EFS, CloudFront, and LoadBalancer to make it scalable and fault tolerant.

A good website architecture is to provide the below principles:

  1. Elasticity – We want our website to scale up and down.
  2. Loose Coupling – We will be distributing our services across multiple AWS products –
    1. The database will be on AWS Aurora or  RDS.
    2. Servers on EC2.
    3. Storage on EFS.

 

Architecture Diagram

 

We will register our Application Load Balancer with  Route 53.  CloudFront will be fetching our uploads from the S3. To push your media files to S3, you would need to use the AWS Plugin . Since the application is stored in numerous instances, we would need to save the WordPress files on EFS. EFS is a network file storage which can be mounted on multiple devices. This makes updating WordPress files extremely easy. As there is always a chance of an instance failing, we would use ElastiCache Memcached or Redis to store the sessions. The database can be either Aurora or RDS. We will be using a Master and Read replica in case one of the database instance fails, it won’t have an impact on the website.

 

Why store files on S3?

There are many benefits in storing your media files to S3.

  • Easy website migration. By having files stored on S3 website migration can be done effortlessly.
  • Saves storage space. WordPress converts an image into multiple resolutions. This could become troublesome if you have a heavy used website. S3 can store an unlimited number of files. By saving the media files on S3 you will save space.

 

Why EFS?

EFS lets you distribute your application code across multiple EC2 instances. This makes deployment of code very efficient. Instead of deploying the application code to multiple instances, you would need to push it to a single instance which has the EFS mounted on it. Moreover, installing themes and plugins can be done on a single instance and the changes get reflected across all other mount targets!

don’t forget to include the mount script in the bootstrap script!

Increasing the performance of EFS

As EFS is a network file storage. This introduces latency when a file is being accessed. In order to increase the performance, a Bytecode caching must be used. By default, PHP 5.5 comes with OpCache. OpCache caches the compiled PHP code in memory, so the script doesn’t have to be compiled again. When a PHP file is updated, the cached compiled code is updated as well. OpCache can reduce the execution time by 70%.

Scalability

As we are using an Application Load Balancer (Layer 7), the number of instances can scale depending on the CPU, RAM usage or scheduled plan. This means we don’t need to worry about the amount of traffic our servers receive. To register a health check, there are many techniques which you could use. You could have a health check to the wp-login page or a custom PHP file which invokes a WordPress method such as retrieving the WordPres site url.

Instance types

AWS has many different types of instances. Identify the appropriate one is different from every project. But, while using more than one instance this becomes very flexible. If you’re happy with the instance type to try switching it to a reserved instance plan. With Reserved instance plan, you could be saving up to 75% of your EC2 billing!

 

Database types

WordPress like every other CMS relies on a database. This is why selecting the right database is extremely important in an architectural design. AWS provides two different types of databases AWS RDS and AWS Aurora.

AWS Aurora is designed and developed by AWS to provide a very cost-effective database while offering five-time throughput of MySQL. In addition, it copies the data across three availability zone and on SSD. By using AWS Aurora you will save cost and provide a better-tuned and scalable database.

AWS RDS which is a managed MySQL that has been out there for a long time. A key advantage of AWS RDS is the option to select the type of SSD storage device. (optimized SSD or general purpose).

 

To reduce the cost of database plan, try using WP Rocket the fastest caching plugin.

 

My top 3 tips!

  • Use AWS RDS or Aurora for your database.
  • Install WordPress on an EFS drive.
  • Use S3 to store your media files.

 

Conclusion

In this article, I described the architecture of WordPress website on AWS. If you find that multiple availability zones are expensive, you could launch the same architecture on a single availability zone. As always if you plan to host the website for a long-term on AWS, purchase the reserved plan it will save you around 70% billing cost.

 

 

 

 

I have a huge interest in AWS, Azure, and Ansible. In my spare time, I spend learning new features of AWS and Azure 🙂