Deploy Wordpress on Ubuntu 22.04 server using terraform.

Deploy Wordpress on Ubuntu 22.04 server using terraform.

I will walk you through the process of deploying a fresh WordPress installation on Ubuntu server 22.04 on AWS using Terraform. You can just replicate the same process and you will always achieve the same results.

You can use this installation to run test deployments or small workloads if your WordPress site does not need a multi-tiered architecture. This tutorial deploys WordPress into a default VPC in the chosen region.

Prerequisites

You should have to terraform installed on your system, so you can run it globally, if not you can use this tutorial to set it up.

We will also be deployed on AWS, it is assumed you already have AWS credentials configured on your local system, which Terraform will use to authenticate and deploy into AWS. If not you can read up on this tutorial to use and set it up.

STEP 1

Create a folder, with a single file named main.tf. At the top of the file, initialize it to use AWS with the region you want to deploy it to.

terraform {
 required_providers {
   aws = {
     source  = "hashicorp/aws"
     version = "~> 4.0"
   }
 }
}

# Configure the AWS Provider
provider "aws" {
 region = "us-east-1"
}

STEP 2

Since we will be using the default VPC that comes with every region (provided you have not physically deleted it from the console). We will just only create a custom security group that we will attach to our EC2 instance.

In this code block, we named our SG, wordpress_sg, you can easily use another name. We also created two allowed inbound rules for HTTP and SSH with IPv4 and IPv6. Then a general allow outbound rule for all ports.

# Create a security group
resource "aws_security_group" "wordpress_sg" {
 name        = "allow_http"
 description = "Allow HTTP and SSH traffic"


 ingress {
   description      = "SSH from everywhere"
   from_port        = 22
   to_port          = 22
   protocol         = "tcp"
   cidr_blocks      = ["0.0.0.0/0"]
   ipv6_cidr_blocks = ["::/0"]
 }


 ingress {
   description      = "HTTP from everywhere"
   from_port        = 80
   to_port          = 80
   protocol         = "tcp"
   cidr_blocks      = ["0.0.0.0/0"]
   ipv6_cidr_blocks = ["::/0"]
 }


 egress {
   from_port        = 0
   to_port          = 0
   protocol         = "-1"
   cidr_blocks      = ["0.0.0.0/0"]
   ipv6_cidr_blocks = ["::/0"]
 }

STEP 3

Create the EC2 instance that will run the application, in this section, we will select the AMI we want to use, which is the latest Ubuntu 22.04, you can easily swap and use Amazon-Linux ami.

We will also select the instance type, and add the security group we just created. Most importantly we will be adding user_data, a bash script which will install the LAMP server, configure the server and install WordPress on server launch.

Due to the many lines of code in the bash script, it's much better to create a separate userdata.tpl file containing the bash script and reference it in the ec2 instance.

# Create a web instance with user data to install wordpress
resource "aws_instance" "web_server" {
 ami           = "ami-007855ac798b5175e" #ubuntu 22.04
 instance_type = "t2.micro"
 user_data       = file("userdata.tpl")
 vpc_security_group_ids = [aws_security_group.wordpress_sg.id]
 tags = {
   Name = "Wordpress_Starter"
 }
}


output "public_ip" {
 value       = aws_instance.web_server.public_ip
 description = "The public address of the IP"
}

You can reference the exact bash script used to install the WordPress from this link, just copy and paste. https://raw.githubusercontent.com/tunjiaramide/wordpress-install-bashscript/main/wordpress.sh

STEP 4

Once all is added, run the following terraform commands
Terraform init
Terraform validate
Terraform apply –auto-approve

You will initialize the project using terraform init, then run the validate command to make sure you have not made any typo error, then you deploy the application running terraform apply –auto-approve to proceed without extra input

After it is successfully deployed, you will see something like this on your terminal.

Outputs:
public_ip = "3.84.154.239"

AWS takes time to provision the server, so you must wait for it to have 2/2 status checks.

You can get the code here - https://github.com/tunjiaramide/wordpress-monolithic-terraform-deploy