WordPress over AWS Cloud with serverless database RDS

Prathamesh Mistry
4 min readMar 9, 2021

WordPress is web publishing software you can use to create a beautiful website or blog. Since it was released in 2003, WordPress has become one of the most popular web publishing platforms. And today it powers more than 35% of the entire web — everything from hobby blogs to some of the most popular websites online.

A Basic WordPress blog required a WebServer and a MySQL server. this article will cover Creating an EC2 Instance, configured as a web server and using the AWS Relational Database Server (RDS) for the database.

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. That is a server in the cloud.

Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud. It is a serverless service, this means AWS will take care of managing the server for us, we just have to create our tables and databases in there.

Provisioning an EC2 Instance and RDS

We will be using Terraform to provision the EC2 instance and RDS on AWS. We will be launching this setup in a VPC with 2 Public Subnets. Terraform is an open-source infrastructure as a code software tool. Terraform is capable of provisioning and managing multi-cloud services.

Note: We are using Red Hat Enterprise Linux 8 as our OS

Installing Terraform

$ sudo wget https://releases.hashicorp.com/terraform/0.12.2/terraform_0.12.2_linux_amd64.zip$ sudo unzip ./terraform_0.12.2_linux_amd64.zip$ sudo mv terrform /usr/local/bin$ terraform -v
Terraform v0.12.2

Configure AWS-CLI

Create Workspace and Configure the providers for Terraform

$ mkdir wp_aws/
$ cd wp_aws/
$ cat > providers.tf
provider "aws" {
region = "ap-south-1"
}

Initialize Terraform

Directory Structure

[workstation@localhost wp_aws]$ tree -a
.
├── providers.tf
└── .terraform
└── plugins
└── linux_amd64
├── lock.json
└── terraform-provider-aws_v3.31.0_x5

Creating the File main.tf

[workstation@localhost wp_aws]$ vim main.html

Creating EC2 Instance

This will be our Web Server.

Creating Database Subnet Group

Since we are launching the Database in a VPC, we need to create a subnet group.

Creating RDS

We will be using the MySQL engine with 10Gib of Allocated Storage. The publicly accessible directive is to be removed when into production. While Launching the Database in a VPC, we must have a minimum of 2 Subnets in the database group.

Running Terraform

[workstation@localhost wp_aws]$ terraform apply
aws_db_subnet_group.default_db_group: Refreshing state... [id=main]
aws_instance.WordPress_Server: Refreshing state... [id=i-013b87393ec3e7b47]
aws_db_instance.default: Refreshing state... [id=terraform-20210309102313996500000001]
An execution plan has been generated and is shown below.
..
..
..
aws_db_instance.WordPress_DB: Still creating... [3m40s elapsed]
aws_db_instance.WordPress_DB: Creation complete after 3m49s [id=terraform-20210309121450755300000001]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
[workstation@localhost wp_aws]$

Complete Code:

# provision EC2resource "aws_instance" "WordPress_Server" {
ami = "ami-0eeb03e72075b9bcc"
instance_type = "t2.micro"
associate_public_ip_address = true
subnet_id = "subnet-017c9861ab204f3c0"
vpc_security_group_ids = ["sg-0745b7b7e1e819742"]
tags = {
Name = "WP_Instance_Terraform"
}
}
# create database group with vpc subnetsresource "aws_db_subnet_group" "default_db_group" {
name = "main"
subnet_ids = ["subnet-017c9861ab204f3c0","subnet-0e61bfbe6222dc041"]
tags = {
Name = "My DB subnet group"
}
}
# provision RDSresource "aws_db_instance" "WordPress_DB" {
allocated_storage = 10
engine = "mysql"
engine_version = "5.7.31"
instance_class = "db.t2.micro"
name = "mydb"
username = "myusername"
password = "mypassword"
vpc_security_group_ids = ["sg-0745b7b7e1e819742"]
publicly_accessible = true
db_subnet_group_name = aws_db_subnet_group.default_db_group.name
}

Setting Up the Web Server and Installing WordPress

Install HTTPd package and other dependencies for WordPress

$ sudo yum install httpd -y
$ sudo amazon-linux-extras inst all -y lamp-mariadb10.2-php7.2 php7.2

Download an Extract WordPress

Change sample configuration file to main configuration file and fill in all the necessary details

$ mv wordpress/wp-config-sample.php wordpress/wp-config.php
$ vim wordpress/wp-config.php

Copy the WordPress directory to DocumentRoot

$ sudo mv wordpress/* /var/www/html/

Stat and Enable the httpd Service

$ sudo systemctl start httpd
$ sudo systemctl enable httpd

Visit the website and Fill in the Details

Login and you are ready to Create your next Website is live!

Get the Complete and Optimized Code at

THANK YOU!

--

--

Prathamesh Mistry

Final Year Student, understanding the industrial approach and tools