WordPress as a static website generator on AWS Serverless
Many WordPress websites could be replaced by static HTML. Static websites are cheaper, faster, and more secure to host. Why not use WordPress as a static website generator so we can still edit content via our web browser and use many of the great WordPress themes and plugins?
✅ Fast & secure static website hosting
✅ Easy browser based content management
✅ Compatible with most WordPress themes and many plugins
✅ WordPress backend isolated from the web
✅ Pay only for what is used
Interested in a similar approach to WordPress on Serverless? Checkout Pressless for a tool to migrate an existing WordPress site to run on AWS.
ServerlessWP enables hosting the backend WordPress installation, where we add and manage content, on AWS Serverless products so that we don't have to worry about maintaining a server. Serverless means we only pay for what we use, and most websites don't get edited too often. Combined with AWS free tier offerings, we can host the backend for next to nothing in cost.
ServerlessWP puts Basic Authentication in front of the backend WordPress installation to limit its exposure to bots and other sources of unwanted traffic.
The backend WordPress website is crawled to generate the static website. The static HTML is uploaded to Amazon S3 for storage and hosting. AWS CloudFront is used to provide CDN hosting and SSL for the public-facing website.
A typical blog will cost around $1 per month to run (mainly depending on CloudFront data transfer and database uptime for content management). The RDS-based MySQL database for WordPress will shut down automatically after 2 hours of inactivity to reduce costs, since it is not necessary for the static frontend.
Disclaimer: This is a proof of concept! Breaking changes may be made if/when Aurora Serverless becomes a preferable database option versus the current RDS usage.
- Install the Serverless Framework for AWS - Serverless installation guide
- Clone or download this repository
- Place necessary binary files in the "bin" directory.
- This can be handled by running the "./build_bin.sh" script if you have Docker installed.
- "php-cgi" and "wget" are the currently necessary binaries. They must be compiled to run in the Lambda environment.
- The "./bin/lib" directory requires a library noted in bin/lib/readme.txt. It will be put in place if "./build_bin.sh" is used.
- Place a WordPress installation directly in the "wp" directory so that "index.php" is found in the root of "wp".
- This can be handled by executing the "./build_wp.sh" script.
- Modify "wp-config.php" for ServerlessWP friendly configuration.
- This is handled by the "./build_wp.sh" script if used.
- Otherwise, use this wp-config-base.php as a guide.
- To handle file uploads in the WordPress backend, install the necessary WordPress plugins:
- This is handled by the "./build_wp.sh" script if used.
- ServerlessWP
- Amazon Web Services
- WP Offload S3 Lite
- Place any other WordPress themes or plugins in the respective wp-content directories like a standard WordPress installation.
- Edit serverless.yml
- The "custom" section at the top of the file has variables that should be reviewed.
- Run "npm install"
- Run "severless deploy" -- may take 30-60 minutes for AWS to create the necessary resources the first time.
- Complete the WordPress installation by visiting the first URL in the "endpoints" output, this is the URL for the WordPress backend. It will look like endpoints: https://example.execute-api.us-east-1.amazonaws.com/dev/
- Login to WordPress and enable the ServerlessWP, Amazon Web Services, and WP Offload S3 Lite plugins.
- Point your domain's DNS at the created CloudFront distribution.
- Mitch MacKenzie - mitchmac
The following articles and repositories provided ideas, examples, and best practices for various parts of the project:
- Chris White - Hosting a Laravel application on AWS Lambda
- Danny Linden - aws-lambda-php
- Peter Tilsen - basicAuthApiGateway
- hemanth.hm - Copying shared library dependencies
- Ivan Perevernykhata - Start/Stop RDS instances on schedule
This project is licensed under the MIT License - see the LICENSE file for details.