Shift from on-premises to EC2: Scripting an Apache installation on AWS.
Scenario
Your company wants to start shifting from using on premises servers to using servers in the cloud. Rather than purchasing all the infrastructure in a data center they ask you to create an EC2 instance in AWS to host their new website.
Objectives
1. Launch an EC2 Amazon Linux t2.micro (free tier) in a public subnet of your **Default VPC**. (You should NOT be creating a custom VPC for this project)
2. Create a security group that allows inbound traffic on HTTP for 0.0.0.0/0 and allows inbound traffic on SSH from your ip address.
3. SSH into your EC2 instance and install Apache with a custom webpage using a BASH script. (The webpage can be a simple Hello World or something more complex. We are not testing your HTML skills)
Requirements
1- Amazon Web Services account (Free tier)
2- Linux command line knowledge
3.PowerShell
AWS
- Sign into the AWS Management Console and navigate to the EC2 dashboard.
2. Click on the “Launch Instance” button to start creating a new EC2 instance.
3.We are then going to start with naming the instance, choose Amazon Linux AMI, and select the t2. micro instance type (which is eligible for the free tier).
4.Next, we are going to be creating a new key pair so we can connect to our instance.
5. We can leave the network settings alone and leave them in default.
We can launch the instance now.
Once the instance status is running, we can connect the instance.
Select the instance and then hit connect.
We will connect using SSH client and you will be given directions on how to SSH into the instance that was just created.
Terminal Work
We are going to be working in the power shell terminal. First, we need to use the cd command to change into the directory where your key pair was saved. We can then SSH to EC2 instance on your terminal.
We need to be a root user for the following steps. We can do so by executing the following command:
sudo su -
We then need to create a bash script to install Apache onto the OS and create a webpage by using vim.
We need to change the permissions to be able to read, write, and execute everything we have done thus far and be able to have the bash script run.
chmod 744 Apache_server.sh
Run the bash script.
./Apache_server.sh
We need to obtain the public IPv4 address, it should be saved or in your instance summary.
Copy the IP address into the web browser and you should get a webpage that looks similar to this.
ADVANCED
1. SSH into your EC2 instance and create a scripts directory.
2. Move your BASH script created earlier from its current location to the new scripts directory.
3. Create a new repository in your GitHub for your BASH Scripts. (You can use this repo in the future for any new scripts you create or if you need to use one for a future project)
4. Use Git commands to add your BASH scripts to the newly created GitHub repo. DO NOT COPY AND PASTE THE SCRIPT INTO GITHUB USING THE BROWSER.
GitHub Repository
We are going to create a new repository in GitHub for our Bash script.
We then need to make sure git is installed on our terminal or install Git. As seen below, Git was not installed, and I went ahead and installed it.
git
yum install git
Cloning
I used the SSH Keys to clone from GitHub. You can find more about it using Connecting to GitHub with SSH — GitHub Docs.
git@github.com:jchapa30/Bash-Scripts.git
Create Directory
Now to make a new directory (Scripts) and move the BASH script (Apache_Scripts.sh) created earlier from its current location to the new scripts' directory.
mkdir Scripts
mv Apache_scripts.sh /root/Scripts
We then have to move the contents of the scripts directory to the repository associated with GitHub.
mv /root/Scripts / Apache_scripts.sh Bash-scripts
BASH to GitHub
Now that we have moved the file into the repository associated with GitHub, we are going to create a branch, checkout, add, and check the status of the branch with the following commands.
We can now commit to the changes that we have made.
git commit -m "Comment.."
Now we check to see if GitHub was updated. You can see below, there was an update made to the Apache_Scripts.sh.
Overall, we have learned how to launch an EC2 instance, connect to your instance using SSH connect, install Apache to include a custom webpage using a BASH script, and create a repository in GitHub for your BASH scripts.