How To Run Static Server In AWS EC2

Posted by Wei Xia on December 15, 2016 | Hits:

Amazon Web Services (AWS) - Cloud Computing Services [中文版]

Set up AWS account

You can create a student account with $100 credit for free if you have an .edu email.

Create EC2 instance

  1. Launch Instance, choose ubuntu, follow the default settings.
  2. In Security Groups, add the following rule below.
  3. Create a key pair, and save to your computer.
HTTP                         Anywhere
HTTPS                        Anywhere
Custom TCP Rule    8000      Anywhere
Custom TCP Rule    9000      Anywhere

Log in to AWS EC2

  1. Go to your key directory.
  2. Enter ssh -i node.pem to log in
  3. 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 using ls -l

Use fileZilla to upload files to AWS

  1. Preferences > Settings > Connection > SFTP, Click “Add key file”
  2. Browse to the location of your .pem file and select it.
  3. 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:

  1. For Amazon Linux, the default user name is ec2-user.
  2. For RHEL5, the user name is often root but might be ec2-user.
  3. For Ubuntu, the user name is ubuntu.
  4. For SUSE Linux, the user name is root.
  5. For Debian, the user name is admin. Otherwise, check with your AMI provider.”

Setup Ubuntu environment

  1. Enter sudo apt-get update
  2. Enter sudo apt-get install libssl-dev g++ make
  3. Install npm by going to
  4. Test if success by creating a vim test.js file
  5. Run node test.js, and go to to see if works

test.js source code for Node.js

var http = require('http');
var port = 9000
http.createServer(function (req, res) {
	res.writeHead(200, {'Content-Type' : 'text/plain'});
	res.end('Hello World!\n');
console.log('Listening on port: ', port);

Use Express to server static folder

  1. Install Express by using npm install express
  2. Modify test.js file by using the code below
  3. Run node test.js, and go to to see if works

test.js source code for Express:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
	res.send('Hello World!\n');

var port = 9000;
console.log('Listening on port: ', port);

Server static folder by using Express

  1. Folder directory as below
  2. Create test.js as it below
  3. Run node test.js, and go to http://localhost:8080/ to check if success


var express = require('express'),
    app = express();

app.use(express.static(__dirname + '/public'));

app.get('/', (req, res) => {
  return res.status(200).send('This is the root of my express application');


Run Server Backend Forever

  1. Using sudo nohup node test.js &
  2. delete nohup.log file
  3. Stop the task? Check the task ID by using ps -ef, find test.js ID, and using kill ID to stop

Additional Information

  1. 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 get Cannot GET/


This is the end of post