How To Deploy a High-Availability WordPress Website with External Amazon RDS Database
Alex Paul Migit
Elite Life Coach & Business Advisor | Sr. Data & Analytics Engineer | Athlete | Accredited Investor | 10X | Empire Builder | Founder & Serial Entrepreneur | Musician & Singer-Songwriter | The WLS Foundation 501(c)(3)
This article describes how to launch an Amazon RDS database instance that is external to AWS Elastic Beanstalk. Then you will get an overview of how to configure a high-availability environment running a WordPress website to connect to it.
The website in this example uses Amazon Elastic File System (Amazon EFS) as shared storage for uploaded files.
Running a DB instance external to Elastic Beanstalk decouples the database from the lifecycle of your environment. This lets you connect to the same database from multiple environments, swap out one database for another, or perform a blue/green deployment without affecting your database.
Launch a DB instance in Amazon RDS
To use an external database with an application running in Elastic Beanstalk, first launch a DB instance with Amazon RDS. When you launch an instance with Amazon RDS, it is completely independent of Elastic Beanstalk and your Elastic Beanstalk environments, and will not be terminated or monitored by Elastic Beanstalk.
Use the Amazon RDS console to launch a Multi-AZ MySQL DB instance. Choosing a Multi-AZ deployment ensures that your database will fail over and continue to be available if the master DB instance goes out of service.
Next, modify the security group attached to your DB instance to allow inbound traffic on the appropriate port. This is the same security group that you will attach to your Elastic Beanstalk environment later, so the rule that you add will grant ingress permission to other resources in the same security group.
Creating a DB instance takes about 10 minutes. In the meantime, download WordPress and create your Elastic Beanstalk environment.
Download WordPress
To prepare to deploy WordPress using AWS Elastic Beanstalk, you must copy the WordPress files to your computer and provide some configuration information.
#Download WordPress from wordpress.org ~$ curl https://wordpress.org/latest.tar.gz -o wordpress.tar.gz #Download the configuration files from the sample repository ~$ wget https://github.com/aws-samples/eb-php-wordpress/releases/download/v1.1/eb-php-wordpress-v1.zip #Extract WordPress and change the name of the folder ~$ tar -xvf wordpress.tar.gz ~$ mv wordpress wordpress-beanstalk ~$ cd wordpress-beanstalk #Extract the configuration files over the WordPress installation ~/wordpress-beanstalk$ unzip ../eb-php-wordpress-v1.zip
**Using 'https://wordpress.org/latest.tar.gz' will insure that you download the latest version of WordPress.
Launch an Elastic Beanstalk environment
Use the Elastic Beanstalk console to create an Elastic Beanstalk environment. Choose the PHP platform and accept the default settings and sample code. After you launch the environment, you can configure the environment to connect to the database, then deploy the WordPress code to the environment.
Environment creation takes about 5 minutes and creates the following resources:
- EC2 instance - An Amazon Elastic Compute Cloud (Amazon EC2) virtual machine configured to run web apps on the platform that you choose.
- Instance security group - An Amazon EC2 security group configured to allow inbound traffic on port 80.
- Load balancer - An Elastic Load Balancing load balancer configured to distribute requests to the instances running your application.
- Load balancer security group - An Amazon EC2 security group configured to allow inbound traffic on port 80.
- Auto Scaling group - An Auto Scaling group configured to replace an instance if it is terminated or becomes unavailable.
- Amazon S3 bucket - A storage location for your source code, logs, and other artifacts that are created when you use Elastic Beanstalk.
- Amazon CloudWatch alarms - Two CloudWatch alarms that monitor the load on the instances in your environment and that are triggered if the load is too high or too low.
- AWS CloudFormation stack - Elastic Beanstalk uses AWS CloudFormation to launch the resources in your environment and propagate configuration changes.
- Domain name - A domain name that routes to your web app in the form <subdomain>.<region>.elasticbeanstalk.com.
All of the above resources are managed by Elastic Beanstalk.
When you terminate your environment, Elastic Beanstalk terminates all the resources that it contains. The RDS DB instance that you launched is outside of your environment, so you are responsible for managing its lifecycle.
Configure security groups and environment properties
Add the security group of your DB instance to your running environment. This procedure causes Elastic Beanstalk to re-provision all instances in your environment with the additional security group attached.
Next, use environment properties to pass the connection information to your environment. The sample application uses a default set of properties that match the ones that Elastic Beanstalk configures when you provision a database within your environment.
Configure and deploy your application
Verify that the default structure of your wordpress-beanstalk folder is correct.
The customized wp-config.php file from the project repo uses the environment variables that you defined in the previous step to configure the database connection. The .ebextensions folder contains configuration files that create additional resources within your Elastic Beanstalk environment.
The configuration files require modification to work with your account. Replace the placeholder values in the files with the appropriate IDs and create a source bundle.
#Create a source bundle containing the files in your project folder ~/eb-wordpress$ zip ../wordpress-beanstalk.zip -r * .[^.]*
Upload the source bundle to Elastic Beanstalk to deploy WordPress to your environment.
Install WordPress
To complete your WordPress installation: open the Elastic Beanstalk console, navigate to the management page, and choose the environment URL to open your site in a browser.
You will be redirected to a WordPress installation wizard because you haven't configured the site yet.
Perform a standard installation. The wp-config.php file is already present in the source code and configured to read the database connection information from the environment. You shouldn't be prompted to configure the connection.
Installation takes about a minute to complete.
Update keys and salts
The WordPress configuration file wp-config.php also reads values for keys and salts from environment properties. Currently, these properties are all set to test by the wordpress.config file in the .ebextensions folder.
The hash salt can be any value that meets environment property requirements, but you should not store it in source control. Use the Elastic Beanstalk console to set these properties directly on the environment.
NOTE: Setting the properties on the environment directly overrides the values in wordpress.config file.
Remove access restrictions
The sample project includes a configuration file (loadbalancer-sg.config) that creates a security group and assigns it to the environment's load balancer, using the IP address that you configured in dev.config to restrict HTTP access on port 80 to connections from your network. Otherwise, an outside party could potentially connect to your site before you have installed WordPress and configured your admin account.
Now that you've installed WordPress, remove the configuration file to open the site to the world.
To remove the restriction and update your environment...
#Delete the .ebextensions/loadbalancer-sg.config file from your project directory ~/wordpress-beanstalk$ rm .ebextensions/loadbalancer-sg.config #Create a source bundle ~/eb-wordpress$ zip ../wordpress-beanstalk-v2.zip -r * .[^.]*
Upload the source bundle to Elastic Beanstalk to deploy WordPress to your environment.
Configure your Auto Scaling group
Finally, configure your environment's Auto Scaling group with a higher minimum instance count.
Run at least two instances at all times to prevent the web servers in your environment from being a single point of failure. This also allows you to deploy changes without taking your site out of service.
**To support content uploads across multiple instances, the sample project uses Amazon Elastic File System to create a shared file system.
Create a post on the site and upload content to store it on the shared file system. View the post and refresh the page multiple times to hit both instances and verify that the shared file system is working.
Upgrading WordPress
To upgrade to a new version of WordPress, back up your site and deploy it to a new environment.
Do not use the update functionality within WordPress or update your source files to use a new version. Both of these actions can result in your post URLs returning 404 errors even though they are still in the database and file system.
Finally, you will want to configure a custom domain name for your production environment and enable HTTPS for secure connections.
More information and a detailed tutorial with environment termination instructions can be found, here.