How to Launch an Instance and Host a Website With AWS EC2
Take a look at how this tutorial that demonstrates how to create a website and host it with AWS EC2.
Join the DZone community and get the full member experience.
Join For FreeIn this tutorial, we will learn how to launch an EC2 Server, the Ubuntu Linux Virtual Server and host a website by serving index.html.
What is AWS?
Amazon Web Services (AWS) is an Amazon subsidiary which offers cloud-computing services at very affordable rates, therefore making its customer base.
Key AWS Services are EC2, RDS, S3, and CloudFront. There are a total of 140+ services which covers compute, storage, databases, analytics, networking, mobile, developer tools,
management tools, IoT, security, and enterprise applications.
Amazon EC2 Instance
Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the AWS Cloud. In other words, it provides us with a virtual server with different capacities to best suit your needs. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in traffic with ease
An on-demand EC2 instance from AWS is one in which the user can rent the virtual server per hour and use it to deploy his/her own applications.
Step 1: Launch an Instance
Log into the AWS console.
From the top right corner of the EC2 dashboard, choose the AWS Region in which you want to launch the EC2 server.
From the dashboard, choose Launch Instance.
Choose Amazon Machine Image (AMI)
Amazon Machine Image or more commonly known as AMI which includes an operating system, and required software/Applications which depends on AMI.
For our example, we will use Ubuntu Server 18.04 LTS (HVM), SSD Volume Type (This image is free tier eligible).
Click Select to choose AMI.
Choose EC2 Instance Types
In the next step, we need to choose the instance type. For our case, we will use t2.micro which is free tier eligible.
Configure the Instance
For our case, we can leave default settings.
Number of Instance – How many Instances you wish to launch, one in our case.
Purchasing Options – Launching in On-Demand pricing (We can also choose spot instance)
Read more on Spot Instances.
I will write a detailed description on full settings later.
Click on Next: Add Storage.
Add Storage
Elastic Beanstalk (EBS) Volume is a Virtual Hard Disk in the cloud.
Volume type Root indicates that Root volume will be partition from where our OS will boot.
We can add additional partitions if we want by clicking Add New Volume.
Delete on Termination means the EBS will be deleted if we delete the EC2 instance.
Add Tags (optional)
Add tags provides a key-value pair to mark your instance, It can be used for better cost analysis.
It is very useful in case of large infrastructures with many Instances.
Click Next: Create a Security Group.
To read more on Tags read here.
Configure Security Groups
Security Groups are like firewalls that define what type of traffic we want to allow.
Since we wish to SSH (port – 22) into our EC2 Server and allow web traffic through HTTP (port – 80) and HTTPS (port – 443).
We will also set the source as My IP for SSH and for web traffic i.e., HTTP and HTTPS we will put Anywhere.
Click Review and Launch.
Review Instances
Review the instance settings and hit launch.
We will get a dialog where we will create a Public and Private key pair. The private key is stored in Instance and the public key is given to user do not let anyone know your public or private key otherwise they may use it to compromise your virtual machines.
Click on Download Key Pair to download key pair and keep it safe.
After a successful launch of instance, you will be taken to the Instance screen.
When the status of Server change from pending to running, you can SSH into your Server to perform access.
SSH into our machine and install the webserver.
Check your public address from instance details or assign an elastic IP.
After allocating address you must attach it to your Server.
Choose an Instance from the drop-down menu and click Associate to attach it to Server.
Now we need to SSH into our server (Here we are using AWS Dashboard but we can also use putty or terminal to access SSH, more on that later ).
ssh -i <path_to_key_pair_file> ubuntu@<public_ip_from_dashboard>
Let’s sign it to superuser (root) by –
sudo su
Now let’s update all services/packages and install apache web server –
apt-get update
apt-get install apache2
Now let’s start the service:
service apache2 start
Server index.html
You must have your files to be served
By default apache2 web server serves a index.html from location /var/www/html, now lets copy your code and paste in this directory (/var/www/html).
We need to reload apache2 web server now. Type the following command:
service apache2 reload
Now let’s check on the browser if our website through index.html is served or not.
Copy public address or Elastic IP of your Server from EC2 console by selecting your Server and paste it on the browser.
Published at DZone with permission of Vipul Singh. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments