Increase speed of Wordpress Lightsail by adding global CDN


As you may have already known, without further configuration, websites’ speed is restricted by the physical distance between your end users and the location where you host your server. A common approach to solve this problem would be to add what is called content delivery network (CDN) to your website, so that there will be cache copies of your website all around the world, and your users will be able to download contents from the cache server that is closest to their location. In this tutorial we will discuss how you may add global CDN to your Wordpress site that’s hosted on AWS Lightsail. The image below demonstrates the distribution diagram of your website when you have CDN. The number one in the diagram represents where you host your Lightsail instance. Number twos in the diagram represent cache servers all around the world and number threes represent your end users.

Distribution diagram from AWS


Before you can continue with this tutorial, make sure you have already created a Wordpress website on AWS Lightsail and registered a domain on Lightsail.

Create Distribution

On the AWS Lightsail console, navigate to the Networking tab.

Lightsail Dashboard

Click on Create distribution

Create Distribution

Choose your Lightsail instance as your distribution origin. This is where your CDN will pull contents from.

Choose Distribution Origin

Select Yes, apply to use the Wordpress cache behavior preset. Wordpress has some default cookies that it uses internally, by enabling the Wordpress cache behavior preset, this will make sure these default Wordpress cookies can still function as expected.

Wordpress preset

Choose the pricing plan that fits your needs, enter a distribution name then click on Create distribution.

Choose Distribution Pricing Plan

It will take 5 ~ 10 minutes before your distribution is ready to use.

Distribution Create In Progress

In the meantime, let’s modify our Wordpress configuration, so it will work with our distribution.

On the Lightsail console, click on Connect using SSH to open a ssh session in a browser window.

Connect to Lightsail with SSH

Paste in the following command to create a backup file of the wp-config.php

sudo cp /opt/bitnami/apps/wordpress/htdocs/wp-config.php /opt/bitnami/apps/wordpress/htdocs/wp-config.php.backup

Enter the following command to open the wp-config.php file with Vim

sudo vim /opt/bitnami/apps/wordpress/htdocs/wp-config.php

Press I to enter insert mode in Vim, and delete the following lines of code in the file.

define(‘WP_SITEURL’, ‘http://' . $_SERVER[‘HTTP_HOST’] . ‘/’);
define(‘WP_HOME’, ‘http://' . $_SERVER[‘HTTP_HOST’] . ‘/’);

Add the following lines of code at the place where you deleted the two line of code.

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/');define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/');if (isset($_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'])&& $_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'] === 'https') {$_SERVER['HTTPS'] = 'on';}

Press the Esc key and type :wq! then press Enter to save and exit the file.

Enter the following command to restart the Apache service.

sudo /opt/bitnami/ restart apache

Close the window to terminate the SSH connection.

Point Custom Domain to the Distribution

After the distribution is ready, it will by default have a domain name like, but this domain name doesn’t look friendly to end users. Let’s add a custom domain to our distribution.

On the distribution page, click on the Custom domains tab then click on Create certificate.

Create Certificate

Enter your custom domain name.

Enter Custom Domain Name

Once you click create, your certificate will be in the validating progress.

Validating Certificate

To validate the certificate, we must create CNAME records in the DNS Zone. Click on the Lightsail logo on the top left corner to return to the home page.

Click on the Networking tab then click on the DNS Zone that you registered your domain in.

Click on Add record, select CNAME record then copy and paste Name into the Subdomain field and copy and paste Value field into the Maps to field.

Create CNAME Record

Add both records to DNS Zone.

It may take 5 ~ 10 minutes before the Lightsail pickup changes.

When Lightsail pickups your changes, the status of your certificate will become Valid, not in use

Certificate Ready, Not in use

Toggle the switch to enable custom domain.

Enable Custom Domain
Custom Domain Enabled

Now let’s link the domain to our distribution.

Navigate back to the Networking tab and select the DNS zone where you registered your domain. Click on Add record, select A record and choose your distribution in the Resolves to field. Then click on the green check mark to save.

Create A Record

Add a second record, but this time choose CNAME record. Enter www as the subdomain and point it to your domain.

Create CNAME Record

Validate Content is Served from CDN

On a new browser tab, enter your domain url to visit your site. Press F12 to open up the developer console and go to the Network tab. Look for any image of the website, and you’ll see the content is indeed served from CloudFront when you look at the via and x-cache property.

Validate Content Served from CloudFront

Original Post:




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to set up log monitoring for Kubernetes using Fluentd & Elasticsearch (AWS Elasticsearch…

DEVSECOPS : The New Infrastructure Age

How to monitor OpenCloud RDS data bases from Zabbix

Week 1

NIST Digital Library of Mathematical Functions

8 Tips on How to Be an Effective Software Engineer

What is web3:a simple definition?

#31 Allocation of Time

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bill Chen

Bill Chen

More from Medium

Editing Concept: Convert Figma/Sketch to HTML & CSS in pxCode

Upload multiple files in PHP with example

Upload multiple files in PHP with example

Setting up Wordpress on an Ubuntu server and using Lynis auditing tool

Create your own docker private registry without GUI simplest way