Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rogerbush8/783571720327d5d34ef0 to your computer and use it in GitHub Desktop.
Save rogerbush8/783571720327d5d34ef0 to your computer and use it in GitHub Desktop.
Install Simple Node.js (Express) WebServer on bare AWS EC2 Linux AMI (201409, Fedora 19 "like")
#### Installs Nodejs and Express on a new AWS EC2 instance, running Linux AMI.
# "Webserver" is about 10 lines of code in Express.js/Node
# This works on RHEL systems (i.e. yum).
# To install, do this on target machine (make sure to get the link from RAW gist):
#
# $ sudo su -
# $ curl https://gist...../raw/... | bash -s
#### Step 1: Note OS
# Installing Nodejs depends on which OS you're on. Since I was on a AWS AMI
# I needed to determine what Linux this was closest to, so I could determine
# the simplest install instructions:
#### Step 2: Install nodejs and npm
yum -y update
yum -y --enablerepo=epel install nodejs npm
npm install npm -g
npm update -g
# Ignore bunch of dependency warnings...
#### Step 3: Create WebServer App and Install Express on Target Instance
# We will now create a simple Nodejs app, the usual way, by creating an empty
# directory, initializing a package.json, creating a .js program, and installing
# the express dependencies. The "web server" is about 10 lines of Javascript
# code using Expressjs.
mkdir webserver
cd webserver
npm init
npm install express --save
# Create Javascript program "main.js" (copy and paste from below):
$ cat > main.js <<EOF
var express = require ('express');
var app = express ();
app.get ('/', function (req, res) {
res.send ("Web server works!\n");
});
console.log ("Web server starting, attempting to listen on privileged port 80...");
app.listen (80);
console.log ("Listening on port 80");
EOF
#### Step 4: Start webserver and test
# Since the webserver will be listening on port 80, which is privileged
# we need to start with "sudo":
#
# To start: $ sudo node main.js
#### Step 4: Test locally
# We'll now make a curl request on the webserver
# In another shell window do:
# $ curl http://localhost/
# Web server works!
# You can test from another vm instance, providable the port and vm
# are reachable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment