Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating a web server on AWS using EC2
________ ________ ___ __ ___
|\_____ \|\ __ \|\ \|\ \ |\ \
\|___/ /\ \ \|\ \ \ \/ /|\ \ \
/ / /\ \ __ \ \ ___ \ \ \
/ /_/__\ \ \ \ \ \ \\ \ \ \ \
|\________\ \__\ \__\ \__\\ \__\ \__\
\|_______|\|__|\|__|\|__| \|__|\|__|
Web servers on GCP, Azure Cloud.

Creating a web server on AWS using EC2 - Hands-on

Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.

I'm currently preparing AWS Certified Solution Architect - Associate certificate.
The following gist is intended to AWS users to learn more about Amazon Elastic Compute Cloud.
I performed this setup on my Ubuntu 18.04.2 LTS.
To check your OS version, execute $ lsb_release -a in your Terminal.

Installations

None. Just log into your AWS management console, https://console.aws.amazon.com.
You'll need to perform several tasks in your CLI regarding SSH keygen, so make sure you check the following prerequisites.

Prerequisites

First, make sure Oracle jdk is installed. I recommend java 1.8.0
To uninstall effectively your current jdk, perform this:
$ sudo apt-get remove openjdk*
$ sudo apt-get remove --auto-remove openjdk*
$ sudo apt-get purge openjdk*
$ sudo apt-get purge --auto-remove openjdk*

To install java 1.8.0, open Terminal Ctrl+Alt+T and run the command:
$ sudo add-apt-repository ppa:webupd8team/java // adds PPA repository
$ sudo apt-get update // updates package list
$ sudo apt-get install openjdk-8-jdk // installs openjdk

java-8.png
$ javac -version // shows your new java version

Author

  • Isaac Arnault - AWS Cloud series - Related tags: #EC2 #WebServer

EC2

  • $ https://console.aws.amazon.com. // log into your AWS management console

I'm using MFA to secure my root account access coupled with Google Authenticator on my Android smartphone.

You can bypass this step and login normally.

🔴 See output

isaac-arnault-AWS-1.jpg

Go to Services > EC2

  • In Create Instance section, click on Launch Instance
🔴 See output

isaac-arnault-AWS2.png

  • Select Amazon Linux 2 AMI (HVM), SSD Volume Type

  • Instance type: choose t2.micro (Free tier eligible). Instance comes with 1vCPU and 1 GiB (memory).

  • Click on "Next: Configure instance details"

  • Configure instance details : leave all fields as they're by default, just Enable termination protection.

🔴 See output

isaac-arnault-AWS3.png

Next : Add Storage

Leave default configuration.

Next: Add Tags

You can leave tags blanks, here I'm using some tags for my own needs.

🔴 See output

isaac-arnault-AWS4.png

Next : Configure Security Group

Click on "Add rule" to allow Port 80 on Http. Ignore the warning and click on "Review and Launch" then "Launch".

🔴 See output

isaac-arnault-AWS-5.png

  • Now you are about to create a key pair to securely SSH into your instance. Select Create a new key pair, name it as you want then Download Key Pair. This should download a .pem file. Mine is MYKP1.pem.

  • Create a new folder named "SSH" on your "Desktop", then move the .pem file to the newly created folder.

  • Change the permissions to .pem file, ie: $ chmod 400 MYKP1.pem.

  • Connect to your EC2 instance using your IPv4 Public IP Address:

$ ssh ec2-user@myipv4.public.address -i MYKP1.pem

  • Then type "yes" when prompted by the CLI.

Now, you should be connected to your EC2 instance through SSH.

🔴 See output

isaac-arnault-AWS-9.png


Important

  • If connection failed, you are probably blocked by a proxy (you are trying to connect from a Public Libraby for example) not allowing your device using Port 22, which is the default port used by the SSH.

Another method : Secure Shell App (Web)

  • Now we are going to use another method if you want to connect to your EC2 instance, if you can't / do not want to use your CLI. We are going to use Secure Shell App extension in our Chrome browser.

Search for "Secure Shell App" in your Search Engine and Download it as below :

🔴 See output

isaac-arnault-AWS-10.png

  • Once installed, you should see this. Then click on "Secure Shell App" icon.
🔴 See output

isaac-arnault-AWS-11.png

  • If everything goes well, enter the following information into your Secure Shall App as follows : your username (ec2-user) and your hostname (provided by your EC2 instance).

Go in the SSH folder where you stored the .pem file from your CLI and generate a public key from the .pem using $ ssh-keygen -y -f MYKP1.pem > MYKP1.pub.

  • Now you should have 2 files, MYKP1.pem and MYKP1.pub. Duplicate MYKP1.pem file and rename the new file to MYKP1 (just remove .pem extension). In the Identity section of the prompt, upload both the MYKP1.pem and MYKP1 file .
🔴 See output

isaac-arnault-AWS-12.png

  • There you go, if everything went fine you should be prompted by the Secure Shell App, type "yes" and you should see the same display as you would normally see in your CLI.
🔴 See output

isaac-arnault-AWS.png


  • Let's get back to your installation using your CLI. The following steps are not performed in Secure Shell App, but you can perform them there.

  • Elevate your privileges to root using $ sudo su and perform $ yum update -y to update your CLI with the latest available packages.

  • Install Apache HTTP Server from your CLI. This will basically turn your EC2 instance to a web server, use $ cd /var/www/html to make your web server's files accessible by Port 80.

  • Let's create a sample index.html file using $ nano index.html

🔴 See output

isaac-arnault-AWS-13.png

  • To start the httpd service, use $ service httpd start.
🔴 See output

isaac-arnault-AWS-32.png

  • There you go, type your IPv4 public address in your browser and you should see your Web Server online.
🔴 See output

isaac-arnault-AWS-16.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.