Publishing Website in EC2 Instance

In this article, we are going to discuss about how to publish the website with Amazon EC2 instance. Let assume that you don't have any EC2 instance created in your aws admin console. We will follow the instructions below through out this article.

+ Create your first Amazon EC2 instance
+ Choose a trusted Ubuntu Image to run on EC2 (Amazon Machine Image)
+ Choose an instance type
+ Configure the instance details
+ Add storage
+ Add Tags
+ Create your first Security Group (Amazon's network firewall rule)
+ Create an Amazon SSH key
+ Connect to an EC2 instance via SSH
+ Publish Website in EC2 instance

## 1.Create your first Amazon EC2 instance
* Go to your EC2 management console from the aws admin console and click on Instance tab to see how many instances are running ![imageedit_2_3298940394.png](https://qiita-image-store.s3.amazonaws.com/0/211892/1f46d36f-8812-373f-ecfe-d50dc636ac2b.png)
In my case, I don't have any instances running. So It gonna display as shown in the picture above.

## 2. Choose a trusted Ubuntu Image (AMI) to run on EC2
Click on the button "Launch Instance" and then go to "AWS Market Place" tab to select one of the Images from there ![imageedit_1_8225173391.png](https://qiita-image-store.s3.amazonaws.com/0/211892/b20a8dc8-ea21-886e-fb24-3b673a4e0847.png)
There are some official images out there and also some unofficial images as well. So how do we know which image should we place trust in? To do that, we have to go to <a href="https://cloud-images.ubuntu.com/locator/ec2/">https://cloud-images.ubuntu.com/locator/ec2/</a> and search for the appropriate image for your current region. ![imageedit_1_7259315703.png](https://qiita-image-store.s3.amazonaws.com/0/211892/011b68aa-332d-eb03-b152-1ca89d2694b2.png)
Here, I choose version 17.04 amd64 and instance type <b>hvm:ebs-ssd</b>, and the reason I choose <b>hvm:ebs-ssd</b> is that I want a hardware virtual machine using SSD storage and the root device type is <b>EBS (Elastic Block Store)</b>. It basically mean that something that is not actually backed by persistence storage, everything is cleared on startup. So you gonna lose everything if you stop and start the instance again.

So next, let copy the AMI-ID from this website and search in the <b>Community AMIs</b> tab from the aws admin console. ![imageedit_3_3973024971.png](https://qiita-image-store.s3.amazonaws.com/0/211892/53082d31-76d5-2c52-fc18-dfdb97925aa6.png)
Then, go ahead and select this ubuntu image for our EC2 instance.

## 3. Choose an instance type
After select the image for the instance, it will ask you to choose an instance type. and I choose <b>t2.micro</b> type. ![imageedit_5_7796110711.png](https://qiita-image-store.s3.amazonaws.com/0/211892/4870860a-ff6d-8af7-1781-0d7d7b01e525.png)
Then, go ahead and click on <b>Next:Configure instance Details</b>

## 4. Configure Instance Details
In case you want to create a separate VPC or Subnet, you can do it here, but in my case, I'm gonna use all the default setting.
![imageedit_7_9750133942.png](https://qiita-image-store.s3.amazonaws.com/0/211892/ad724313-93f9-5790-4065-276b670a6232.png)
Now, let go ahead and add storage.

## 5. Add Storage
In here you can add additional storage capacity to your instance and you can also change the volume type as well. In my case, I'm gonna use the default setting given as shown in the picture below.![imageedit_9_3939967632.png](https://qiita-image-store.s3.amazonaws.com/0/211892/d5267489-56ce-f9b4-3910-de3fb14b63ff.png)
Now, let go ahead and Add tag.

## 6. Add Tags
Here, you can add some tag as you want. In my case, I add Name tag with key <b>My-Server</b>
![imageedit_11_5385223519.png](https://qiita-image-store.s3.amazonaws.com/0/211892/3e8db065-4ace-0ef0-2d38-95afa88cca2e.png)
Then, go ahead and Configure Security Group.

## 7. Configure Security Group
In this section, we can add the security group name, which my case is <b>my-appserver-security-group</b> and you can give some description. and then we have to add rule for <b>HTTP</b>. You can restrict the accessibility IP range as well. But In my case, I'm gonna open it to the world.
![imageedit_13_4409902662.png](https://qiita-image-store.s3.amazonaws.com/0/211892/a93d5fb7-b1ef-732f-6fba-9254541d6291.png)


Since, this security group is created for web security group, then port 80 should be allowed. Let's say it is database security group, you probably allow port <b>3360</b> or <b>4321</b> instead. Now let go and review before we can launch.

## 8. Create an Amazon SSH key
The first time you launch the instance, it gonna ask you for a key pair. It is simply just an SSH key pair. So we gonna create a new one and download it, but we can only download this key pair once we create. So in my case, I name it as <b>myserver-keypair</b>![imageedit_15_8609214123.png](https://qiita-image-store.s3.amazonaws.com/0/211892/2af23efc-2530-8125-425c-e68fb8af1080.png) 
You need to download the key pair and put it in somewhere on you local computer. we will need it whenever we want to connect to our EC2 instance via SSH.![imageedit_18_3279974717.png](https://qiita-image-store.s3.amazonaws.com/0/211892/aec6f263-681e-0724-e232-560a54a81e75.png)
We have to wait until the instance is running before we can connect.

## 9. Connect to an EC2 instance via SSH
Now we can connect to our EC2 instance via SSH. To access you instance:

* Open your SSH client
* Locate the private key file which my case is <b>myserver-keypair.pem</b>  and your key must not be publicly accessible. you can use this command:
    * <pre>chmod 400 myserver-keypair.pem</pre>
* Use SSH command to connect
    * <pre>ssh -i "myserver-keypair.pem" ubuntu@ec2-18-221-45-235.us-east-2.compute.amazonaws.com</pre> ![imageedit_20_9085397556.png](https://qiita-image-store.s3.amazonaws.com/0/211892/d6263fd8-6018-d0ba-69a9-542deaae235b.png)

## 10. Publish website in EC2 instance
After successfully connected to the EC2 instance, we have to install LAMP stack (Linux Apache MySQL PHP).
To make sure that you have the latest version of all installed packages, Please enter this command on your terminal:
<pre>sudo apt-get update && apt-get upgrade</pre>
After having the latest version of all installed package, let's go ahead and install LAMP stack by following this command:
<pre>sudo apt-get install apache2 php mysql-server</pre>
Then follow the instruction in the installation process to get everything up and running.
You may have to allow permission to the public directory by enter these following command:
<pre>sudo chown -R ubuntu:ubuntu /var/www/html
sudo chmod -R 755 /var/www/html</pre>
Once everything is ready, you should see the Apache Ubuntu Page by navigating to the public IP address or public domain name of your instance.![imageedit_2_4123368658.png](https://qiita-image-store.s3.amazonaws.com/0/211892/3688dd2f-6711-014c-af00-618eaaf45332.png)
Sound Great! After that you have to transfer your website to the instance by using any FTP client, which my case I use <a href="https://filezilla-project.org/">FileZilla</a>.
Open File Zilla and add your keypair by going to Edit > Preferences > SFTP > Add key file and then go head and connect to the instances. ![imageedit_4_4113908167.png](https://qiita-image-store.s3.amazonaws.com/0/211892/d32459cf-1f2a-838d-3eaa-39f7ab27bf2e.png)
After that, navigate to <b>/var/www/html/</b>and upload your website in here. In my case, I create a new directory call <b>sample</b> and upload my index.html file there. COOL!
Now, you can either go to your public dns or public IP address of your instance to access your website. <a href="http://ec2-18-221-45-235.us-east-2.compute.amazonaws.com/sample/">http://ec2-18-221-45-235.us-east-2.compute.amazonaws.com/sample/</a>

References: <a href="https://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html">https://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html</a>