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.com
to 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
.pem
file 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.js
file - Run
node test.js
, and go to11.11.11.11:9000
to see if works
test.js
source code for Node.js1
2
3
4
5
6
7var http = require('http');
var port = 9000
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type' : 'text/plain'});
res.end('Hello World!\n');
}).listen(port);
console.log('Listening on port: ', port);
Use Express to server static folder
- Install Express by using
npm install express
- Modify
test.js
file by using the code below - Run
node test.js
, and go to11.11.11.11:9000
to see if works
test.js
source code for Express:1
2
3
4
5
6
7
8
9
10var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!\n');
});
var port = 9000;
app.listen(port);
console.log('Listening on port: ', port);
Server static folder by using Express
- Folder directory as below
- Create
test.js
as 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.log
file - Stop the task? Check the task ID by using
ps -ef
, findtest.js
ID, and usingkill ID
to 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