Host Your Website - AWS EC2

When your website is almost finished, you should consider where to host it. Amazon EC2 is a good choice.

Amazon EC2 - Virtual Server Hosting Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.
You can consider Amazon EC2 as a 24*7 running server with a public IP address that belongs to you only. You have full access to that server. When creating an EC2 instance, you can make a configuration all the way from hardware to software. Amazon provides one year free use for small instance which means you can use a tiny server (less Memory, less SSD and normal CPU) free for one year. After that, Amazon begins to charge you like 10 dollars per month depending on how much you use.

 

Step 1: Create EC2 Instance

Use your amazon account to sign into AWS Console. Then choose ec2 and launch instance. The path may be different. aws1

I choose ubuntu since I can get much documents about it from internet.

aws2

You can configure the details about the storage and other things. However, the default is quite useful.

During the creating or initializing process, you may be required to create a key pair. Just follow the instructions and remember to download the pem file and keep it save. Do not lose it. That's the key and only key to access your instance.

Step 2: Configuration if needed

The Security Group is used to keep your server from unwanted access. You can define which port can be used to communicate inside/outside. aws3

For convenience, I open all the port and allow all traffic in and out. This is a really bad practice and just for study.

ews4

Step 3: Log into your server

Amazon prepared a quite well document on how to connect to your instance. aws5

If you are using mac or linux, ssh is already good to use. For those windows users, my suggestion is install a git-command on windows. This tool is designed to let you use git command on windows. However, it also provides a choice which can install other useful linux commands like ls or ssh.

You can google "git commands windows" or use the link here git-download.

Step 4: Deploy your project

First, you need to find a way to download your project resources to the server. Here is some options:

Using ftp

Build up a ftp server (vsftpd) in your ec2 and upload your code from any ftp client. Vsftpd is already included in ubuntu. The only work you need to do is some configuration. The configuration may be a little tricky and sometimes annoying. I prefer to use github.

Using github or other Gits

If you already use Git in your development, you just need to do a git clone on your ec2. Otherwise you need to put your resource on Git first, either source code or distribution code.

sudo apt-get install git can help you install git command on ubuntu.

Next, start the server. This step varies depending on what kind of framework you are using in your website. Your website may be a project of NodeJS, AngularJS or a even more simple project with only html, javascript and css. For AngularJS and simply web project, http-server is a simple server that can achieve your purpose. http-server can be installed via sudo npm install -g http-server. Before that, you may install npm on ubuntu via sudo apt-get install npm. In the same way, you can install Nodejs via sudo apt-get install nodejs.

After that, you can use http-server or nodejs to start server on ec2. However, if that is the only tool you're using, you need to keep the ssh client alive. Otherwise the server will be closed immediately when you lost the connection. forever can help you keep the server running in background.

forever start http-server

You need sudo if you want to use port 80:
sudo forever start http-server -p 80

When under sudo, it may can not find http-server. You may need to specify where http-server is.
sudo forever start /usr/local/bin/http-server -p 80
You can use which forever to check out where is http-server.

It's better to disable "show directory listings" when start http-server.
sudo forever start /usr/local/bin/http-server -p 80 -d false

It's almost the same way when you're using nodejs.

If you're seeking for some advance server, you can try nginx. It supports https and together with other routing configurations. With proper config, nginx can help you host multiple website in one single server. We will talk about it later.

Last Step: visit your website

You can find the public ip for your ec2 on Amazon Console.

What you need to do next:
Set up a domain so others can access your website through domain instead of ip address.