Easily set-up WordPress on Amazon Web Service

Starting your own website / blog seems like a daunting task. Googling ‘starting your own website‘ will return many different options. The simplest option is using fully manager website builder website like Squarespace, Wix, WordPress.com. These sites provide the full suite of service including register the domain name, provide templates to speed up the design of your website, hosts all your files, etc. It is the simplest way to start your online presence. However, it limits the ability the perform customization.

The second type of option is using a hosting company like Bluehost, HostGator, GoDaddy.   These companies also provide most of the infrastructures you need to start a website mention above. By getting the starter plan, you are agreeing to share a physical server resource with other (hard drive, memories, etc). There is the concern of security and this approach limits your ability to scale up or make changes to the infrastructure.

Using IaaS (Infrastructure as a Service) is the other option that provides the most flexibility. Cloud service provider allows us to create a computing instance, experiment with it, throw it away, and start over. The flexibility offers by this set-up cannot be overlooked. Many cloud service providers offer promotional/free pricing for basic usage. So this option potentially can be the cheapest. The downside of this option is the higher technical skill requirement. This guide is written to provide guidance on how to set up a WordPress site on AWS. By the end of the guide, a WordPress site will be up and running and ready to greet your visitors.

The Project

Time Required: 3+ hours

Requirement: Some level of experience with command lines, willingness to explore and troubleshoot issues is a plus! The tutorial is done on a MacBook for now. If you’re on Windows or Linux, I will provide hints in the article where it maybe different.

A quick reference guide is provided.

TL;DR – Quick guide to setting up the site

  1. Create an account with AWS
  2. Create an EC2 instance with Ubuntu 18.04
  3. SSH into your instance
  4. Install docker, docker-compose, manage docker as non-root
  5. Purchase a domain name with Route 53
  6. Route traffic from your domain name to your instance
  7. Set up security group on your EC2 instance to handle incoming connection
  8. Clone this directory to your instance’s /opt/docker/ directory
  9. Follow the direction in the repository to run the script for setting up the SSL certificate
  10. Up the docker container
  11. Access your website with your domain name!

Create an account with AWS

There are few big names in the market for IaaS: Amazon Web Service, Microsoft Azure, Google Cloud.  Other players such as IBM, Oracle, and Alibaba are also available.  AWS was chosen for this tutorial because it is the most mature provider.  Another provider can be adopted for the guide as well.

Before we can start using the service, an account needs to be created with AWS.  Creating an account is free, it will ask you for your information such username, email address, address, contact information, and billing information.  Fill in all the information and create an account.

Create an AWS account

Create an EC2 Instance

Now comes the fun part! Once you have the AWS account created. AWS provides users with an AWS Management Console, this is where the EC2 instance will be created.   Once you’re logged in to AWS, click on the My Account > AWS Management Console.

Navigate to AWS Management Console

The AWS Management Console is the location where all the available services from AWS can be managed. It might look overwhelming with options, but at this step, EC2 will be used. Click on the EC2 option to bring up the EC2 dashboard. EC2 stands for Elastic Compute Cloud, hence EC2.

Open up EC2 Managment Console

EC2 is a core service provided by Amazon. An instance can be created using EC2, basically, you are creating your own computer on the cloud! The EC2 dashboard is where the user can manage instances, volume (storage), and many other configurations. To start the first instance, click on ‘Launch Instance’.

Dashboard for managing your EC2 instances

We will be using Ubuntu Server 18.04 LTS. Since this guide encourages us to explore, the Ubuntu Server is a basic option that only contains the operating system and core packages. Select the Ubuntu Server 18.04 option and leave the default 64-bit (x86) and hit Select. (If you want to save time, other options are available that includes additional packages such as Amazon Linux AMI that contains packages like Docker, PHP, and MySQL)

Select Ubuntu Server 18.04 64-bit (x86)

After clicking Select, the guide will bring you to Step 2 to choose the instance type. The different options present the different configuration for the instance. To learn more about options and pricing, click out Amazon’s documentation. The t2.micro is the free tier option, it has 1 cpu and 1 GB of memory. For a starter site, it is enough resource. At this point, you can configure the other options available to you but in the spirit of experimentation, we will leave the default setting and modify them later. Click on ‘Review and Launch’ to continue.

Choose the free option!

EC2 setup panel allows you to review the setting once more, the default is fine for now. Click ‘Launch’. A popup window will appear that ask you to create a key. This key is a series of numbers and characters that will allow you to securely log in to your instance. The same as the key you would need to open a vault. The first time you start an instance on AWS, you will need to create a new key. The key pair name should be the name of your site or something easily distinguishable. Then click on ‘Download Key Pair’. It is important that you download the key and store it in a safe location on your computer. Losing the key means that you won’t be able to log in to your instance!

Save it to your computer, possibly ~/.keys/my_site_key

After you download the key, place it in a secure location such as ~/.keys/my_site/key

With the key secured, click on ‘Launch Instance’. It will take a few minutes to get the instance set up. After a few minutes, the status of the instance should say running. The instance is now ready to accept connection and wait for commands.

Connect to your Instance using SSH

In the EC2 instance dashboard, the status of you instance should read running. Click on Instance on the side panel if you don’t see the EC2 dashboard. It is a good idea to give your instance a name. To create a name, hover over the name, a pencil icon will appear. Simply click on it to assign a name.

Everything is set! Let’s connect to your instance using SSH. Right click on your instance, click Connect. A popup window will appear providing information on how to connect to your instance. You will need to copy the user@ip-address in order to connect. It will start with ubuntu@ and end with amazonaws.com

This part of the tutorial is where Windows diverge. If you’re using Linux or MacBook, you can open up your terminal and type in the follow command. If you’re a Windows user, you will need to download a SSH client such as PuTTY to connect to your instance with SSH.

ssh -i /location/of/the/my_site_key ubuntu@ec2-18-123-456-789.us-east-2.compute.amazonaws.com

The –i flag tells ssh you’re supplying an identify-file for connecting. The identify-file is the key download earlier when the instance was created. Enter the path to the key. If it is successful, you are connected to your instance and the command prompt on your screen will change to display the command prompt for the instance.

Installing docker and docker-compose on your Instance

Install Docker

The next step is to install docker and docker-compose on your instance. The process is exactly the same as you would be installing docker on your local Ubuntu machine. The linked to the installation instructions are provided for the most up-to-date instructions.

sudo apt-get update  // to update package index
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

// install necessary package to use repository over HTTPS
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

// add docker's official GPG key
sudo apt-key fingerprint 0EBFCD88

// verify your output
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

// set up the stable docker repository
sudo apt-get update  // update package index
sudo apt-get install docker-ce docker-ce-cli containerd.io
// install latest version of docker community edition (CE) and containerd
sudo docker run hello-world
// if it runs, docker was successfully installed

Install docker-compose

The following commands will help you install docker-compose on your Ubuntu instance.

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

//download the latest version of docker-compose
sudo chmod +x /usr/local/bin/docker-compose

// apply executable permission to the binaray
docker-compose --version  
// if it runs, you have installed docker-compose successfully

After completing the above steps, you have successfully installed docker and docker-compose on your Ubuntu instance on AWS!

Purchase a Domain Name with Route 53

Route 53 is a Domain Name System (DNS) web service. DNS is basically a server that translates a domain name (learnthe.net) into an IP address to connects to the machine that has that IP address. Route 53 also provides domain name registration, where you can register awesome address (learnthe.net) to your name or your organization. Using Route 53 is very simple and straight forward. Direction provided my Amazon is very clear.

To start using Route 53, in your AWS Management Console, under the Networking and Content Delivery category, click on Route 53. Under register domain, you can type in the domain name that you want. Route 53 will check to see rather the domain name is available or not. If the domain name is available, you can start the registration process.

Make sure the correct email address is entered as it will be how important messages and updates are delivered to you. Once the registration is finished, Route 53 says it may take up to 3 days, but it took around 30 minutes for the domain name to get set up. Once you have the domain name set-up, the next step is to direct web traffic to the EC2 instance where the WordPress site will be hosted.

Set up Hosted Zone to Route Web Traffic

The information needed to set-up the hosted zone is the IPv4 Public IP for your EC2 instance. Navigate to your instance and left click on it. In the description tab on the bottom of your screen, information about your instance will show up. Copy the IPv4 Public IP, which should look like this (18.123.456.789). Next, navigate back to your Route 53 dashboard and find Hosted Zone and click on the domain you registered.

Name: Can be left blank, if you only need the root of your domain name to be redirected.

Type: A – IPv4 Address

Value: IPv4 Address for you instance (Example: 192.0.2.235)

Finally, click on Create Record Set.

Enter the correct information by following AWS instruction

With this step completed, when someone type in your domain name, Route 53 will direct that request to you EC2 instance. The last step is to set up the application to listen for the request and serve the user contents.

Set up Security Group to Handle Incoming Connection

This step up configure the security group for the instance and open up ports that can be used to connect to the instance from the internet. Navigate to the EC2 Management Console and click on Security Groups under Network and Security on the left panel. You will see a group call launch-wizard which is the default group that automatically created for you when you launch your EC2 instance. Then launch group only allows SSH connection to the instance, but let’s change that to open up other ports.

Create a new security group by clicking on Create Security Group. Give it a name and description as you see fit. Then add the following inbound rules for this group (HTTP: port 80, HTTPS: port 443, SSH: 22) like the following image.

Add the inbound rules for your security group

You can learn more about the different between HTTP and HTTPS connection on your own if you’re interested. Essentially, HTTP is for un-secure connections, HTTPS is for secure and encrypted connection (the lock you see when you visit sites).

Once you have the secure group created, it will need to be assigned to the EC2 instance. Navigate to the EC2 instance dashboard > right click on the instance > Networking > Change Security Groups. Then select the security group you just created.

Change the security group of your instance

Set up SSL certificate for our site

This step is provided by and all the credits go to mjstealey. This is why sharing knowledge is so important! A forked version of the repository is available on github. A few adjustments were made to set up the application on EC2. For the MariaDB service, it was configured to use a docker volume to store data on the host’s file system. Line 3 from default_https.conf.template was removed to get it to work.

listen [::]:80;

The best step is to follow the direction in either repository listed above. The section that will be most relevant is the needs here are:

  • Note – make sure you create all the neccessay directory in the application root folde
  • .env – change the passwords
  • HTTPS – copy over the HTTPS template to the default template and change learnthe.net (FQDN_OR_IP) to your domain name
  • Let’s Encrypt – to remove subdomain name if that’s the case

Some Tips

Here are some useful terminal commands that will help you getting the files into the right place

scp -i ~/.keys/learnthenet.pem ~/docker/wordpress-nginx-docker/.env ubuntu@ec2-18-123-456-890.us-east-2.compute.amazonaws.com:~

//  this copies the local env file to your instance using a secure connection, you will do this while you're login to you local machine.  Then, you can log in to EC2 and move the .env to the right place

mv ~/.env /opt/docker/wordpress-nginx-docker/.env
// the 'mv' command is similar to cut and paste

Once you all the files in the correct order, use this command to set up the SSL certificate.

letsencrypt/letsencrypt-init.sh learnthe.net

With the SSL certificate set up. Start the container with docker-compose.

sudo docker-compose up

Once your SSL certificate is set up and containers up and running. You can simply go to your domain name by typing it in the browser and see the set-up page for the WordPress, complete the set-up process and you’re ready to blog!

Conclusion

After trial and error, together, we learned to use some of the newer technology to host a WordPress site. AWS offers different services that allow users and start-up to set up IT infrastructure. EC2 is a flexible computing unit while Route 53 can easily handle domain registration and rerouting internet traffic. Using Docker images allow the easy portability between system. The idea is that each container is self-contained and has the packages it needs spin-up the application. Deploy application across platforms has never been easier!

The set-up provided by this guide might not be the most flexible when scaling the site up horizontally (creating multiple instances to handle increase user traffic). However, scaling up vertical on AWS is fairly easy. Increase the storage can be done by adjusting the volume size. EC2 CPU numbers and memory can also be increased. I hope you find this guide useful, please leave a comment below!

Share the knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *