• Alex Migit

Deploying a high-availability WordPress website with an external Amazon RDS database

This article describes how you launch an Amazon RDS DB instance that is external to AWS Elastic Beanstalk. Then it describes how to configure a high-availability environment* running a WordPress website to connect to it.


The website 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.


*The following instruction is using WordPress version 4.9.5 and PHP 7.0.


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.


~$ curl https://wordpress.org/wordpress-4.9.5.tar.gz -o wordpress.tar.gz


~$ wget https://github.com/aws-samples/eb-php-wordpress/releases/download/v1.1/eb-php-wordpress-v1.zip


~$ tar -xvf wordpress.tar.gz

~$ mv wordpress wordpress-beanstalk

~$ cd wordpress-beanstalk


~/wordpress-beanstalk$ unzip ../eb-php-wordpress-v1.zip


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

  • Instance security group

  • Load balancer

  • Load balancer security group

  • Auto Scaling group

  • Amazon S3 bucket

  • Amazon CloudWatch alarms

  • AWS CloudFormation stack

  • Domain name


All of these 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.


Upload the source bundle to Elastic Beanstalk to deploy WordPress to your environment.


Install WordPress


To complete your WordPress installation: open the Elastic Beanstalk consol, 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.


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.

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.


Upgrade 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.


Detailed tutorial and environment termination instructions available, HERE.

25 views

© 2020 Revenue Source