This article describes how to serve a WordPress application from Amazon Web Service’s Elastic Beanstalk, using Amazon RDS for the MySQL database and S3 for static media files. The hope is that this article is accessible to a large audience, though it assumes the following:
WordPress requires a MySQL database that we’ll create using Amazon RDS. While it’s possible to create a database as part of an Elastic Beanstalk instance, creating our own standalone RDS instance offers greater flexibility and means data won’t be lost if we terminate our Elastic Beanstalk instance.
Open the RDS dashboard, click “Instances” in the menu on the left , and click the “Launch DB instance” button . You will be guided through four configuration pages detailed below.
Choose “MySQL” and click the “Next” button.
Choose “Production – MySQL” and click the “Next” button.
There are a number of settings here that will affect your database performance and pricing. The below suggested configuration is really designed to get a test site up and running; modify the below selections accordingly for your use case.
|DB instance class||Choose d2.t2.micro|
|Multi-AZ deployment||Choose no|
|Storage type||Choose General Purpose (SSD)|
|Allocated storage||Enter 20GB|
|DB instance identifier||This can be any name you choose, eg, “myDbInstance”.|
|Master username||Enter a DB username. You’ll need this later.|
|Master password||Enter a DB password. You’ll need this later.|
Click the “Next” button.
|Public accessibility||Choose no|
|VPC security groups||Choose Create new VPC security group|
|Database name||Can be anything but “wordpress” works. You’ll need this later.|
|Database port||Confirm that it’s set to 3306.|
Click the “Launch DB Instance” button.
Above we’ve set publicly accessible to “no”, meaning that the database will only be accessible by instances in the same VPC security group. Later we’ll add Elastic Beanstalk to the security group so WordPress can access the database.
It will probably take 10 minutes or so for your instance to launch. Continue on to the next section while this is in progress.
By default WordPress stores your media files (eg, images, pdfs, etc.) in the file system. But any uploaded files will be lost when you deploy a new application version to Elastic Beanstalk. The simplest solution is to instead store your media files in an S3 bucket, which has other advantages as well. This can be achieved using the plugin WP Offload S3. Once activated and configured, WordPress will store media files in your S3 bucket.
Go to the S3 dashboard and click the “Create Bucket” button.
|Bucket name and region||Can be anything but “wordpress-uploads” works. Choose a region close to your target audience.|
|Set properties||You can accept the defaults.|
|Manage public permissions||Choose Grant public read access to this bucket.|
Create a new policy granting permissions to your S3 bucket. Go to the IAM dashboard, select “Policies” from the menu at left , and click the “Create Policy” button .
This policy will grant access to our S3 bucket:
Click the “Review Policy” button at bottom and create a name for your policy.
Create a new IAM user to provide access to your S3 bucket. Go to the IAM dashboard, select “Users” from the menu at left , and click the “Add User” button .
|User name||Enter something descriptive, like “wordpress-s3”|
|Access type||Choose Programmatic access|
|Permissions||Select Attach existing policies directly and add the policy you created in the previous step.|
We need an application to deploy to Elastic Beanstalk. In this section we’ll create the needed files to compose the source bundle we’ll upload to Elastic Beanstalk.
Download the latest version of WordPress or install it from the command line:
curl https://wordpress.org/latest.tar.gz | tar -xvz
You should now have a directory named wordpress.
We need to install the WP Offload S3 plugin that we’ll be using to store media files in S3. Go to the plugin page and download the latest version. Unzip the package and save the plugin to wordpress/wp-content/plugins/.
The WP Offload S3 plugin requires the Amazon Web Services plugin. Download this plugin as well, unzip the package, and save to wordpress/wp-content/plugins/.
Delete wordpress/wp-config-sample.php and create a new file wordpress/wp-config.php:
The configuration file references several properties of $_SERVER that we’ll define in an Elastic Beanstalk configuration file in the next step.
Create a new folder wordpress/.ebextensions. Elastic Beanstalk looks for configuration files in this directory when deploying an application. Create a new file wordpress/.ebextensions/env.config:
You’ll have to replace all the ‘placeholder’ values with your own values.
Your source directory wordpress should now look roughly like the following. New files and directories are highlighted:
To deploy the application to Elastic Beanstalk we zip all the members of the wordpress directory into a bundle. Note that we zip all the files in the directory, not the directory itself. In the console, navigate to inside the wordpress directory and enter the following command:
zip -rq artifact.zip .
This should create a file wordpress/artifact.zip.
Go to the Elastic Beanstalk dashboard and click “Create New Application” in the upper right . You will be guided through the following pages:
|Application name||Enter something descriptive, such as “wordpress”. Click “Next” button.|
Click Create web server.
|Predefined configuration||Choose PHP.|
|Environment type||Choose Load balancing, auto scaling.|
|Click “Next” button.|
|Source||Choose Upload your own and select the wordpress/artifact.zip file you created earlier.|
|Click “Next” button.|
|Environment name||Enter a name for your environment. Can be anything.|
|Environment URL||Can be anything unique to AWS.|
|Click “Next” button.|
Don’t select any additional resources for the environment. Click the “Next” button.
|Instance type||Choose t1.micro. As with the RDS configuration, you can choose a different tier depending on your use case. This selection is just to get an environment up and running.|
|EC2 key pair||You don’t have to select a key pair but it’s a good idea when in production so you can check in on your EC2 instances and tunnel into your RDS instance. Check out this link from AWS for more information on key pairs.|
|Leave the rest of the default values and click the “Next” button.|
You don’t need to add any tags. Click the “Next” button.
Choose to let AWS create new roles for the instance profile and service role. Click the “Next” button.
Review your selections and click the “Launch” button. It will take a few minutes for the application to launch. You can continue to the next section while you’re waiting.
Check out the post Automating WordPress Deployment to Elastic Beanstalk to learn how to automate the build and deployment process. This will let you make changes to your local WordPress development environment and deploy a new application with a single command.
Hopefully all of the above worked and you’re off and running, but if not feel free to post questions in the comments section below.