Add a Custom Domain to an Elastic Beanstalk WordPress Application

Updated Jan 25, 2018

This post is part of the series WordPress and Elastic Beanstalk.

Introduction

If you read through the post WordPress and Elastic Beanstalk you should have a working Elastic Beanstalk deployment, but with a URL like “myapp.us-east-1.elasticbeanstalk.com”. This post describes how to point a custom domain to your Elastic Beanstalk environment.

Create a hosted zone in Route 53

We’re going to use Amazon’s Route 53 as our domain name system as it fully integrates with Elastic Beanstalk. If you already own a domain managed by another service you can either transfer the domain registration or keep your domain registration and transfer your DNS to Route 53. If you don’t already own a domain you can register a domain with Route 53. This will automatically created a hosted zone for you in Route 53.

WordPress and URLs

WordPress requires that you specify a “site url” (the URL for your website) and a “home url” (the URL for your WordPress installation). These are typically the same. The values can be specified in the wp_options database, in Settings > General in the WordPress admin, or via a wp-config.php file. We’re going to use the wp-config.php method as it is the easiest to maintain.

If you followed the WordPress and Elastic Beanstalk post your “site url” and “home url” are set to your Elastic Beanstalk URL “myapp.us-east-1.elasticbeanstalk.com”. When you point a custom domain to your Elastic Beanstalk environment WordPress will fail, since the custom domain won’t match the “site url” and “home url” saved in the database.

To address this, we’ll update our .ebextensions/env.config and wp-config.php files to specify the new domain.

Set up Route 53

Once you have a hosted zone in Route 53 you can point your domain to your Elastic Beanstalk instance. Follow these instructions to point your domain to your Elastic Beanstalk environment. Assuming your domain is example.com, you should create A records for example.com and www.example.com that point to your Elastic Beanstalk environment. You can route traffic to one or the other when you specify your WordPress URLs.

Update the WordPress configuration

It may take a while for the DNS records to propagate, but once they do your site will not be available from your new domain as we have not updated the “site url” and “home url” in WordPress.

To make those changes we need to modify our .ebextensions/env.config and wp-config.php files. The below assumes that you want your website traffic to go to www.example.com. If you’d rather traffic go to example.com remove the “www” in the configuration values.

First, update .ebextensions/env.config by adding the lines in red (and replacing with your own domain name):

.ebextensions/env.config
option_settings:
 aws:elasticbeanstalk:application:environment:
  ...
  WP_HOME: 'http://www.example.com'
  WP_SITEURL: 'http://www.example.com'
  ...

Next, update your wp-config.php file by adding the following lines:

wp-config.php
...
define('WP_HOME', $_SERVER['WP_HOME']);
define('WP_SITEURL', $_SERVER['WP_SITEURL']);
...

This tells WordPress to use the URLs specified in our env.config file.

Once you’ve deployed the updated files to Elastic Beanstalk you should be able to access your site via your custom domain. Check out the post Force HTTPS Connections to ensure that all connections to your environment are secure. If you have any questions or suggestions please feel free to use the comments section below.

Comments

No comments exist. Be the first!

Leave a comment