The Hosting WordPress on AWS reference architecture available on GitHub outlines best practices for deploying WordPress on AWS and includes a set of AWS CloudFormation templates to get you up and running quickly. The following architecture is based on that reference architecture. The rest of this section will review the reasons behind the architectural choices.
The based AMI in the GitHub was changed from Amazon Linux1 to Amazon Linux2 in July 2021. However, deployment templates at S3 were not changed yet. It is recommended to use templates at GitHub if there is an issue to deploy the reference architecture with templates at S3.
Reference architecture for hosting WordPress on AWS
Architecture components
The reference architecture illustrates a complete best practice deployment for a WordPress website on AWS.
- It starts with edge caching in Amazon CloudFront (1) to cache content close to end users for fasterdelivery.
- CloudFront pulls static content from an S3 bucket (2) and dynamic content from an Application Load Balancer (4) in front of the web instances.
- The web instances run in an Auto Scaling group of Amazon EC2instances (6).
- An ElastiCache cluster (7) caches frequently queried data tospeed up responses.An Amazon Aurora MySQL instance (8) hosts the WordPress database.
- The WordPress EC2 instances access shared WordPress data on an Amazon EFS file system via an EFS Mount Target (9) in each Availability Zone.
- An Internet Gateway (3) enables communication between resources in your VPC and the internet.
- NAT Gateways (5) in each Availability Zone enable EC2 instances in private subnets (App and Data) to access the internet.
Within the Amazon VPC there exist two types of subnets: public (Public Subnet) and private (App Subnet and Data Subnet). Resources deployed into the public subnets will receive a public IP address and will be publicly visible on the internet. The Application Load Balancer (4) and a Bastion host for administration are deployed here. Resources deployed into the private subnets receive only a private IP address and hence are not publicly visible on the internet, improving the security of those resources. The WordPress web server instances (6), ElastiCache cluster instances (7), Aurora MySQL database instances (8), and EFS Mount Targets (9) are all deployed in private subnets.
[fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” rotation_text=”” highlight_text=”” after_text=”” title_link=”off” link_url=”” link_target=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” content_align_medium=”” content_align_small=”” content_align=”left” size=”2″ animated_font_size=”” fusion_font_family_title_font=”” fusion_font_variant_title_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”orange” hue=”” saturation=”” lightness=”” alpha=”” animated_text_color=”” text_shadow=”no” text_shadow_vertical=”” text_shadow_horizontal=”” text_shadow_blur=”0″ text_shadow_color=”” margin_top_medium=”” margin_right_medium=”” margin_bottom_medium=”” margin_left_medium=”” margin_top_small=”” margin_right_small=”” margin_bottom_small=”” margin_left_small=”” margin_top=”25px” margin_right=”” margin_bottom=”” margin_left=”” margin_top_mobile=”” margin_bottom_mobile=”” gradient_font=”no” gradient_start_color=”” gradient_end_color=”” gradient_start_position=”0″ gradient_end_position=”100″ gradient_type=”linear” radial_direction=”center center” linear_angle=”180″ highlight_color=”” style_type=”default” sep_color=”” link_color=”” link_hover_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]
Scaling the web tier
[/fusion_title]
To evolve your single-server architecture into a multi-server, scalable architecture, you must use five key components:
- Amazon EC2 instances
- Amazon Machine Images (AMIs)
- Load balancers
- Automatic scaling
- Health checks
AWS provides a wide variety of EC2 instance types so you can choose the best server configuration for both performance and cost. Generally speaking, the compute-optimized (for example, C4) instance type may be a good choice for a WordPress web server. You can deploy your instances across multiple Availability Zones within an AWS Region to increase the reliability of the overall architecture.
Because you have complete control of your EC2 instance, you can log in with root access to install and configure all of the software components required to run a WordPress website. After you are done, you can save that configuration as an AMI, which you can use to launch new instances with all the customizations that you’ve made.
To distribute end-user requests to multiple web server nodes, you need a load balancing solution. AWS provides this capability through Elastic Load Balancing, a highly available service that distributes traffic to multiple EC2 instances. Because your website is serving content to your users via HTTP or HTTPS, we recommend that you make use of the Application Load Balancer, an application-layer load balancer with content routing and the ability to run multiple WordPress websites on different domains, if required.
Elastic Load Balancing supports distribution of requests across multiple Availability Zones within an AWS Region. You can also configure a health check so that the Application Load Balancer automatically stops sending traffic to individual instances that have failed (for example, due to a hardware problem or software crash). AWS recommends using the WordPress admin login page (/wp-login.php
) for the health check because this page confirms both that the web server is running and that the web server is configured to serve PHP files correctly.
You may choose to build a custom health check page that checks other dependent resources, such as database and cache resources. For more information, refer to Health checks for your target groups in the Application Load Balancer Guide.
Elasticity is a key characteristic of the AWS Cloud. You can launch more compute capacity (for example, web servers) when you need it and run less when you don’t. Amazon EC2 Auto Scaling is an AWS service that helps you automate this provisioning to scale your Amazon EC2 capacity up or down according to conditions you define with no need for manual intervention. You can configure Amazon EC2 Auto Scaling so that the number of EC2 instances you’re using increases seamlessly during demand spikes to maintain performance and decreases automatically when traffic diminishes, so as to minimize costs.
Elastic Load Balancing also supports dynamic addition and removal of Amazon EC2 hosts from the load-balancing rotation. Elastic Load Balancing itself also dynamically increases and decreases the load-balancing capacity to adjust to traffic demands with no manual intervention.
Stateless web tier
When it comes to user session data storage, the WordPress core is completely stateless because it relies on cookies that are stored in the client’s web browser. Session storage isn’t a concern unless you have installed any custom code (for example, a WordPress plugin) that instead relies on native PHP sessions.
However, WordPress was originally designed to run on a single server. As a result, it stores some data on the server’s local file system. When running WordPress in a multi-server configuration, this creates a problem because there is inconsistency across web servers. For example, if a user uploads a new image, it is only stored on one of the servers.
This demonstrates why we need to improve the default WordPress running configuration to move important data to shared storage. The best practice architecture has a database as a separate layer outside the web server and makes use of shared storage to store user uploads, themes, and plugins.
Shared storage (Amazon S3 and Amazon EFS)
Amazon Elastic File System (Amazon EFS) provides scalable network file systems for use with EC2 instances. Amazon EFS file systems are distributed across an unconstrained number of storage servers, enabling file systems to grow elastically and allowing massively parallel access from EC2 instances. The distributed design of Amazon EFS avoids the bottlenecks and constraints inherent to traditional file servers.
By moving the entire WordPress installation directory onto an EFS file system and mounting it into each of your EC2 instances when they boot, your WordPress site and all its data is automatically stored on a distributed file system that isn’t dependent on any one EC2 instance, making your web tier completely stateless. The benefit of this architecture is that you don’t need to install plugins and themes on each new instance launch, and you can significantly speed up the installation and recovery of WordPress instances. It is also easier to deploy changes to plugins and themes in WordPress, as outlined in the Deployment Considerations section of this document.
To ensure optimal performance of your website when running from an EFS file system, check the recommended configuration settings for Amazon EFS and OPcache on the AWS Reference Architecture for WordPress.
You also have the option to offload all static assets, such as image, CSS, and JavaScript files, to an S3 bucket with CloudFront caching in front. The mechanism for doing this in a multi-server architecture is exactly the same as for a single-server architecture, as discussed in the Static Content section of this whitepaper. The benefits are the same as in the single-server architecture—you can offload the work associated with serving your static assets to Amazon S3 and CloudFront, thereby allowing your web servers to focus on generating dynamic content only and serve more user requests per web server.
Data tier (Amazon Aurora and Amazon ElastiCache)
Amazon Aurora is a MySQL and PostgreSQL compatible relational database built for the cloud that combines the performance and availability of high-end commercial databases with the simplicity and cost-effectiveness of open source databases. Aurora MySQL increases MySQL performance and availability by tightly integrating the database engine with a purpose-built distributed storage system, backed by SSD. It is fault-tolerant and self-healing, replicates six copies of your data across three Availability Zones, is designed for greater than 99.99% availability, and continuously backs up your data in Amazon S3. Amazon Aurora is designed to automatically detect database crashes and restart without the need for crash recovery or to rebuild the database cache.
Amazon Aurora provides a number of instances types to suit different application profiles, including memory-optimized and burstable instances. To improve the performance of your database you can select a large instance type to provide more CPU and memory resources.
Amazon Aurora automatically handles failover between the primary instance and Aurora Replicas so that your applications can resume database operations as quickly as possible without manual administrative intervention. Failover typically takes less than 30 seconds.
After you have created at least one Aurora Replica, connect to your primary instance using the cluster endpoint to allow your application to automatically fail over in the event the primary instance fails. You can create up to 15 low-latency read replicas across three Availability Zones.
As your database scales, your database cache will also need to scale. As discussed previously in the Database Caching section, ElastiCache has features to scale the cache across multiple nodes in an ElastiCache cluster, and across multiple Availability Zones in a Region for improved availability. As you scale your ElastiCache cluster, ensure that you configure your caching plugin to connect using the configuration endpoint so that WordPress can use new cluster nodes as they are added and stop using old cluster nodes as they are removed. You must also set up your web servers to use the ElastiCache Cluster Client for PHP and update your AMI to store this change.
[fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” rotation_text=”” highlight_text=”” after_text=”” title_link=”off” link_url=”” link_target=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” content_align_medium=”” content_align_small=”” content_align=”left” size=”2″ animated_font_size=”” fusion_font_family_title_font=”” fusion_font_variant_title_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”orange” hue=”” saturation=”” lightness=”” alpha=”” animated_text_color=”” text_shadow=”no” text_shadow_vertical=”” text_shadow_horizontal=”” text_shadow_blur=”0″ text_shadow_color=”” margin_top_medium=”” margin_right_medium=”” margin_bottom_medium=”” margin_left_medium=”” margin_top_small=”” margin_right_small=”” margin_bottom_small=”” margin_left_small=”” margin_top=”25px” margin_right=”” margin_bottom=”” margin_left=”” margin_top_mobile=”” margin_bottom_mobile=”” gradient_font=”no” gradient_start_color=”” gradient_end_color=”” gradient_start_position=”0″ gradient_end_position=”100″ gradient_type=”linear” radial_direction=”center center” linear_angle=”180″ highlight_color=”” style_type=”default” sep_color=”” link_color=”” link_hover_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]Improving performance and cost efficiency[/fusion_title]
The following sections introduce a number of options that can improve aspects of your WordPress website’s performance and scalability. Some can be applied to a single-server deployment, whereas others take advantage of the scalability of multiple servers. Many of those modifications require the use of one or more WordPress plugins. Although various options are available, W3 Total Cache is a popular choice that combines many of those modifications in a single plugin.
Accelerating content delivery
An important aspect of the end-user experience is the network latency involved when delivering the previous content to users around the world. Accelerating the delivery of the previous content improves the end-user experience, especially users geographically spread across the globe. This can be achieved with a Content Delivery Network (CDN) such as Amazon CloudFront.
Amazon CloudFront is a web service that provides an easy and cost-effective way to distribute content with low latency and high data transfer speeds through multiple edge locations across the globe. Viewer requests are automatically routed to a suitable CloudFront edge location to lower the latency. If the content can be cached (for a few seconds, minutes, or even days) and is already stored in a particular edge location, CloudFront delivers it immediately. If the content should not be cached, has expired, or isn’t currently in that edge location, CloudFront retrieves content from one or more sources of truth, referred to as the origin(s) (in this case, the Lightsail instance) in the CloudFront configuration. This retrieval takes place over optimized network connections, which work to speed up the delivery of content on your website. Apart from improving the end-user experience, the model discussed also reduces the load on your origin servers and has the potential to create significant cost savings.
Static content offload
This has the positive side effect of offloading this workload from your Lightsail instance and letting it focus on dynamic content generation. This reduces the load on the server and is an important step towards creating a stateless architecture (a prerequisite before implementing automatic scaling).
You can subsequently configure Amazon S3 as an origin for CloudFront to improve delivery of those static assets to users around the world. Although WordPress isn’t integrated with Amazon S3 and CloudFront out-of-the-box, a variety of plugins add support for these services (for example, W3 Total Cache).
Dynamic content
Whole website delivery via Amazon CloudFront
The preceding figure includes two origins: one for static content and another for dynamic content. For implementation details, see Appendix A: CloudFront configuration and Appendix B: Plugins installation and configuration.
CloudFront uses standard cache control headers to identify if and for how long it should cache specific HTTP responses. The same cache control headers are also used by web browsers to decide when and for how long to cache content locally for a more optimal end user experience (for example, a .css
file that is already downloaded will not be re-downloaded every time a returning visitor views a page). You can configure cache control headers on the web server level (for example, via .htaccess
files or modifications of the httpd.conf
file) or install a WordPress plugin (for example, W3 Total Cache) to dictate how those headers are set for both static and dynamic content.
Database caching
Although WordPress has limited caching capabilities out-of-the-box, a variety of plugins support integration with Memcached, a widely adopted memory object caching system. The W3 Total Cache plugin is a good example.
In the simplest scenarios, you install Memcached on your web server and capture the result as a new snapshot. In this case, you are responsible for the administrative tasks associated with running a cache.
Another option is to take advantage of a managed service such as Amazon ElastiCache and avoid that operational burden. ElastiCache makes it easy to deploy, operate, and scale a distributed in-memory cache in the cloud. You can find information about how to connect to your ElastiCache cluster nodes in the Amazon ElastiCache documentation.
If you are using Lightsail and wish to access an ElastiCache cluster in your AWS account privately, you can do so by using VPC peering. For instructions to enable VPC peering, refer to Set up Amazon VPC peering to work with AWS resources outside of Amazon Lightsail.
Bytecode caching
A bytecode cache can be installed on any Lightsail instance that hosts WordPress and can greatly reduce its load. For PHP 5.5 and later, AWS recommends the use of OPcache, a bundled extension with that PHP version.
Note that OPcache is enabled by default in the Bitnami WordPress Lightsail template, so no further action is required.