Launch an EC2 instance with a php application

Abhishek Verma
TheLoudCloud
Published in
1 min readJul 11, 2020

--

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts.

The command below when passed as user data will run the commands to install a php application instead of you manually running the commands.

  • Launch an Ec2 instance with Amazon Linux 2 AMI.
  • Select the default t2.micro instance.
  • Select default VPC. Make sure that the Auto-assign Public IP is enabled. This will assign public IP address to the instance getting launched.
  • Scroll down to the user data section and pass the following:
#!/bin/bash
yum update -y
amazon-linux-extras install -y php7.2
yum install -y httpd
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
cd /var/www/html
wget https://raw.githubusercontent.com/AbhishekGit-AWS/beanStalk/master/index.php
  • This script will setup and configure the php application. The php file being used here displays the EC2 instance details.
  • Make sure your security group has HTTP and SSH inbound rules.
  • Allow enough time for the instance to launch and execute the script.
  • Copy the EC2 public IP address(or public DNS address). This will open up the php application.

The above setting hosted the php application from github, you can also host your own code by transferring your local code to the EC2 instance.

--

--