This post is contributed by Mike Coleman | Developer Advocate for Lightsail | Twitter: @mikegcoleman
The previous posts in this series (Implementing a highly available Lightsail database with WordPress and Using Amazon S3 with WordPress to securely deliver media files), showed how to build a WordPress site and configure it to use Amazon S3 to serve your media assets. The next step is to use an Amazon CloudFront distribution to increase performance and add another level of security.
CloudFront is a content delivery network (CDN). It essentially caches content from your server on endpoints across the globe. When someone visits your site, they hit one of the CloudFront endpoints first. CloudFront determines if the requested content is cached. If so, CloudFront responds to the client with the requested information. If the content isn’t cached on the endpoint, it is loaded from the actual server and cached on CloudFront so subsequent requests can be served from the endpoint.
This process speeds up response time, because the endpoint is usually closer to the client than the actual server, and it reduces load on the server, because any request that CloudFront can handle is one less request your server needs to deal with.
In this post’s configuration, CloudFront only caches the media files stored in your S3 bucket, but CloudFront can cache more. For more information, see How to Accelerate Your WordPress Site with Amazon CloudFront.
Another benefit of CloudFront is that it responds to requests over HTTPS. Because some requests are served by the WordPress server and others from CloudFront, it’s important to secure both connections with HTTPS. Otherwise, the customer’s web browser shows a warning that the site is not secure. The next post in this series shows how to configure HTTPS for your WordPress server.
To configure CloudFront to work with your Lightsail WordPress site, complete the following steps:
- Request a certificate from AWS Certificate Manager.
- Create a CloudFront distribution.
- Configure the WP Offload Media Lite plugin to use the CloudFront distribution.
The following diagram shows the architecture of this solution.
This post assumes that you built your WordPress site by following the previous posts in this series.
Configuring SSL requires that you have a registered domain name and sufficient permissions to create DNS records for that domain.
You don’t need AWS or Lightsail to manage your domain, but this post uses Lightsail’s DNS management. For more information, see Creating a DNS zone to manage your domain’s DNS records in Amazon Lightsail.
Creating the SSL certificate
This post uses two different subdomains for your WordPress site. One points to your main site, and the other points to your S3 bucket. For example, a customer visits
https://www.example.com to access your site. When they click on a post that contains a media file, the post body loads off of
https://www.example.com, but the media file loads from
https://media.example.com, as depicted in the previous graphic.
Create the SSL certificate for CloudFront to use with the S3 bucket with the following steps. The next post in this series shows how to create the SSL certificate for your WordPress server.
- Open the ACM console.
- Under Provision certificates, choose Get started.
- Choose Request a public certificate.
- Choose Request a certificate.
- For Domain name*, enter the name of the domain to serve your media files.
This post uses
- Choose Next.
The following screenshot shows the example domain name.
- Select Leave DNS validation.
- Choose Review.
- Choose Confirm and request.
ACM needs you to validate that you have the necessary privileges to request a certificate for the given domain by creating a special DNS record.
Choose the arrow next to the domain name to show the values for the record you need to create. See the following screenshot.
You now need to use Lightsail’s DNS management to create the CNAME record to validate your certificate.
- In a new tab or window, open the Lightsail console.
- Choose Networking.
- Choose the domain name for the appropriate domain.
- Under DNS records, choose Add record.
- From the drop-down menu, choose CNAME record.
The following screenshot shows the menu options.
- Navigate back to ACM.
Under Name, the value is formatted as randomcharacters.subdomain.domain.com.
- Copy the random characters and the subdomain.
For example, if the value was
_f836d9f10c45c6a6fbe6ba89a884d9c4.media.mikegcoleman.com, you would copy
- Return to the Lightsail DNS console.
- Under Subdomain, enter the value you copied.
- Return to the ACM console.
- For Value, copy the entire string.
- Return to the Lightsail DNS console.
- For Maps to, enter the value you copied.
- Choose the green checkmark.
The following screenshot shows the completed record details in the Lightsail DNS console.
ACM periodically checks DNS to see if this record exists, and validates your certificate when it finds the record. The validation usually takes approximately 15 minutes; however, it could take up to 48 hours.
To track the validation, return to the ACM console. You can periodically refresh this page until you see the status field change to Issued. See the following screenshot.
Building the CloudFront distribution
Now that you have the certificate, you’re ready to create your CloudFront distribution. Complete the following steps:
- Open the CloudFront console.
- Choose Create distribution.
- Under Web, choose Get Started.
- For Origin Domain Name, select the S3 bucket you created for your WordPress media files.
The following screenshot shows the options available in the Origin Domain Name drop-down menu. This post chooses
By default, WordPress does not pass information that indicates when to clear an item in the cache; specifying how to configure that functionality is out of the scope of this post.
Because WordPress doesn’t send this information, you need to set a default time to live (TTL) in CloudFront.
As a starting point, set the value to 900 seconds (15 minutes). This means that if you load a post, and that post includes media, CloudFront checks the cache for that media. If the media is in the cache, but has been there longer than 15 minutes, CloudFront requests the media from your WordPress server and update the cache.
While 15 minutes is a reasonable starting value for media, the optimal value for the TTL depends on how you want to balance delivering your clients the latest content with performance and cost.
- For Object Caching, choose Customize.
- For Default TTL, enter 900.
The following screenshot shows the TTL options.
CloudFront has endpoints across the globe, and the price you pay depends on the number of endpoints you have configured. If your traffic is localized to a certain geographic region, you may want to restrict which endpoints CloudFront uses.
- Under Distribution Settings, for Price Class, choose the appropriate setting.
This post chooses Use All Edge Locations. See the following screenshot. You should choose a setting that make sense for your site. Choosing only a subset of price classes will reduce costs.
- For Alternate Domain Names (CNAMEs), enter the name of the subdomain for your S3 bucket.
This name is the same as the subdomain you created the certificate for in the previous steps. This post uses
Assign the certificate you created earlier.
- Choose Custom SSL Certificate.
When entering in the text field, the certificate you created earlier shows in the drop-down menu.
- Choose your certificate.
The following screenshot shows the drop-down menu with custom SSL certificates.
- Choose Create Distribution.
It can take 15–30 minutes to configure the distribution.
The next step is to create a DNS record that points your media subdomain to the CloudFront distribution.
- Return to the CloudFront console, choose the ID of the distribution.
The following screenshot shows available distribution IDs.
The page displays the distribution ID details.
- Copy the Domain Name
The following screenshot shows the distribution ID details.
- Return to the Lightsail DNS console.
- Choose Add record.
- Choose CNAME record.
- For Subdomain, enter the subdomain you are using for the S3 bucket.
This post is using the domain
media.mikegcoleman.com, so the value is media.
- For Maps to, enter the domain name of the CloudFront distribution that you previously copied.
The following screenshot shows the CNAME record details.
- Choose the green check box.
- Return to the CloudFront console.
It can take 15–30 minutes for the distribution status to change to Deployed. See the following screenshot.
Configuring the plugin
The final step is to configure the WP Offload Media Lite plugin to use the newly created CloudFront distribution. Complete the following steps:
- Log in to the admin dashboard for your WordPress site.
- Under Plugins, choose WP Offload Media Lite.
- Choose Settings.
- For Custom Domain (CNAME), select On.
- Enter the domain name for your S3 bucket.
This post uses
media.mikegcoleman.com as an example.
- For Force HTTPS, select On.
This makes sure that all media is served over HTTPS.
- Choose Save Changes.
The following screenshot shows the URL REWRITING and ADVANCED OPTIONS details.
You can load an image from your media library to confirm that everything is working correctly.
- Under Media, choose Library.
- Choose an image in your library.
If you don’t have an image in your library, add a new one now.
The URL listed should start with the domain you configured for your S3 bucket. For this post, the URL is
The following screenshot shows the image details.
To confirm that the image loads correctly, and there are no SSL errors or warnings, copy the URL value and paste it into your web browser.
You now have your media content served securely from CloudFront. The final post in this series demonstrates how to create multiple instances of your WordPress server and place those behind a Lightsail load balancer to increase performance and enhance availability.
If you haven’t got one already, head on over and create a free AWS account, and start building your own WordPress site – or whatever else might think of!
from AWS Compute Blog