How To Run Static Server In AWS EC2
Contents
Set up AWS account
You can create a student account with $100 credit for free if you have an .edu email.
Create EC2 instance
- Launch Instance, choose
ubuntu, follow the default settings. - In Security Groups, add the following rule below.
- Create a key pair, and save to your computer.
1 | HTTP Anywhere |
Log in to AWS EC2
- Go to your key directory.
- Enter
ssh -i node.pem ubuntu@ec2-11-111-111-111.compute-1.amazonaws.comto log in - Maybe you will see it is a public key, and can’t login. Change the property of key file by using
chmod 600 key.pem, check file’s property by usingls -l
Use fileZilla to upload files to AWS
- Preferences > Settings > Connection > SFTP, Click "Add key file”
- Browse to the location of your
.pemfile and select it. - File > Site Manager, Add a new site with the following parameters:
- Host: Your public dns name of ec2 instance, or the public ip address of the server
- Protocol: SFTP
- Logon Type: Normal
- User: Please see blow
- Press Connect Button
User type:
- For Amazon Linux, the default user name is ec2-user.
- For RHEL5, the user name is often root but might be ec2-user.
- For Ubuntu, the user name is ubuntu.
- For SUSE Linux, the user name is root.
- For Debian, the user name is admin. Otherwise, check with your AMI provider."
Setup Ubuntu environment
- Enter
sudo apt-get update - Enter
sudo apt-get install libssl-dev g++ make - Install npm by going to nodejs.org
- Test if success by creating a
vim test.jsfile - Run
node test.js, and go to11.11.11.11:9000to see if works
test.js source code for Node.js
1 | var http = require('http'); |
Use Express to server static folder
- Install Express by using
npm install express - Modify
test.jsfile by using the code below - Run
node test.js, and go to11.11.11.11:9000to see if works
test.js source code for Express:
1 | var express = require('express'); |
Server static folder by using Express
- Folder directory as below
- Create
test.jsas it below - Run
node test.js, and go tohttp://localhost:8080/to check if success
1 | .tv |
test.js:
1 | var express = require('express'), |
Run Server Backend Forever
- Using
sudo nohup node test.js & - delete
nohup.logfile - Stop the task? Check the task ID by using
ps -ef, findtest.jsID, and usingkill IDto stop
Additional Information
- Express wouldn’t let you visit the folder directly. If you need to visit a file, try
http://localhost:8080/js/application.js, otherwise you may getCannot GET/
Reference
- Connect to Amazon EC2 file directory using Filezilla and SFTP
- Cloud Computing for Education
- Using Express to serve static files, return Cannot GET?
This is the end of post