At A Glance Main Projects Tutorials Resume

Contact


Email: palen1c at gmail.com




AWS LAMP Stack With PHP 8 on Ubuntu and Terraform

Wed, 15 Feb 2023 11:56:58 EST

I have a need every now and then to quickly create a Linux, Apache, Mysql(MariaDB), and PHP stack within Amazon Web Services (AWS). Terraform is a perfect tool for this as it takes roughly a thirty minute task to a few minutes. Once you become familiar with Terraform, it is easy to see why the tool is being deployed in major companies across the world to help manage complex infrastructure.

The terraform version that is probably out of date by now.
I am typically utilizing quick LAMP stacks for use with the Drupal content management system. Drupal has been difficult in the last few years as they have been very aggressive with moving up PHP versions. Many of the default AMI on Amazon only offer PHP 7 with their default package repositories. The most recent versions of Drupal require PHP 8.

The major component needed with Terraform to go beyond just launching a default unpatched and unsetup virtual machine is the addition of a tailored initialization script that is run via the user_data Terraform property. Below you will find my initialization shell script that I use with Terraform to get Ubuntu setup as a PHP 8 LAMP stack as of February 2023. If you are just starting out with Terraform you will likely need to refer to other information to get started with the basics.

The terraform version that is probably out of date by now.


A few important notes:
  1. AMI ID’s are region specific. The AMI ID I am providing here will only work in us-east-1 (Virginia).
  2. Terraform will apply pretty sensible defaults. I already had subnets and security groups setup, so I explicitly provide those. I also specify to destroy the root_block_device on termination and only create a 10gig volume.
  3. I use AWS access keys in the variables.tf file. For my particular setup environment variables as seen in many tutorials do not make sense for this.
  4. I have a key specified in my AWS account named “Miner_Sample”. That key is specified in the Terraform file so it can apply the key for access to all of my AMI instances using SSH.
  5. I am using Terraform 1.3.6 on Windows 10. I have the Terraform AWS provider as well as the Windows Amazon CLI installed.
  6. When you run Terraform apply, it will take several minutes to execute all of these commands.
  7. These images constantly change. If you run into trouble you may need to ssh into the VM to see what went wrong.

Here is the bash initialization script. In this example it is named init_lamp_ubuntu_22.sh



#!/bin/bash
# First off we need swap space for memory. These come with no swap
# and I do not care how slow they get.
# In most cases you want no swap - and using a docker container or cluster
# etc that makes sense but not here
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Backup fstab
sudo cp /etc/fstab /etc/fstab.bak
# Make it permanent even though our VM probably wont be
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Default swappiness is 60 which is fine
# Update any packages - Debian which Ubuntu is based on will present prompts in the UI. We need to disable these.
# This is the reason for the DEBIAN_FRONTEND commands.
sudo DEBIAN_FRONTEND=noninteractive apt update -y
# update any package listings - this requires interactive screen input
sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y
# install apache
sudo DEBIAN_FRONTEND=noninteractive apt install apache2 -y
# mysql
sudo DEBIAN_FRONTEND=noninteractive apt install mysql-server -y
# php
sudo DEBIAN_FRONTEND=noninteractive apt install php libapache2-mod-php php-mysql -y
# Create a phpinfo file - you should delete this right away after ensuring it renders php
sudo echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php


Here is the main.tf file



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

 required_version = ">= 1.2.0"
}

provider "aws" {
 profile = "default"
 region = "us-east-1"
 access_key = var.aws_access_key
 secret_key = var.aws_secret_key
}

resource "aws_instance" "app_server" {
 ami = "ami-0574da719dca65348"
 instance_type = "t3.nano"
 subnet_id = ""
 vpc_security_group_ids = []
 key_name = "ENTER YOUR KEY NAME HERE"

 tags = {
  Name = var.instance_name
 }

user_data = "${file("init_lamp_ubuntu_22.sh")}"

root_block_device {
 delete_on_termination = "true"
 volume_size = "10"
 }


}


Here is the variables.tf file




variable "instance_name" {
 description = "Value of the Name tag for the EC2 instance"
 type = string
 default = "Ubuntu_LAMP"
}
variable "aws_access_key" {
 type = string
 description = "AWS access key"
 default = ""
}
variable "aws_secret_key" {
 type = string
 description = "AWS secret key"
 default = ""
}


Finally, here is the outputs.tf file



output "instance_id" {
 description = "ID of the EC2 instance"
 value = aws_instance.app_server.id
}

output "instance_public_ip" {
 description = "Public IP address of the EC2 instance"
 value = aws_instance.app_server.public_ip
}


I hope this information can help you get a basic setup running. If you are not already familiar with Docker, I would suggest digging into creating your own Docker images. Combining Docker with Terraform instance creation like this really invalidates the need for OS specific setup scripts beyond the basics like the one provided here.

Charles Palen has been involved in the technology sector for several years. His formal education focused on Enterprise Database Administration. He currently works as the principal software architect and manager at Transcending Digital where he can be hired for your next contract project. Charles is a full stack developer who has been on the front lines of small business and enterprise for over 10 years. Charles current expertise covers the areas of .NET, Java, PHP, Node.js, Javascript, HTML, and CSS. Charles created Technogumbo in 2008 as a way to share lessons learned while making original products.

Comments

No one has posted any comments yet, be the first

Comments are currently disabled.


Techno Gumbo RSS Feed

Related Links


Terraform

Ubuntu

Drupal

Docker