Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EliEladElrom/5447080 to your computer and use it in GitHub Desktop.
Save EliEladElrom/5447080 to your computer and use it in GitHub Desktop.
Install nodejs on EC2 - Ubuntu Server 18.04 / 20.04 LTS

nodejs on EC2 Ubuntu Server 18.04 22.04 LTS

SSH instance

In a nutshell:

sudo apt-get update

sudo apt-get -y upgrade

sudo apt-get dist-upgrade

sudo apt-get install build-essential

sudo apt-get install libssl-dev

sudo apt-get install git-core

sudo apt-get install python

Install nodejs:

sudo apt install nodejs

Install NPM and update to latest:

sudo apt install npm

sudo npm install -g npm@latest

Make a web folder:

cd ~

mkdir ~/www

cd ~/www

Install forever globally:

cd ~

sudo npm install -g forever

if you get this error npm does not support Node.js v10.19.0, upgrade node:

curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -

sudo apt-get install -y nodejs

You can forward the port from 8081 to 80

export PORT=8081

sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8081

Keep for future references - to remove iptable just use -D

sudo iptables -D PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8081

Install yarn:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt update

sudo apt install yarn

Install Netstat to see open ports:

sudo apt install net-tools

sudo netstat -plunta | grep LISTEN

Restart server for updates to complete - All done :)

@EliEladElrom
Copy link
Author

Clear all ip tables;

sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
sudo iptables -t mangle -F
sudo iptables -t mangle -X
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

Set http / https port forwarding;

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8000
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8081

Setting openssl certificate;
http://greengeckodesign.com/blog/2013/06/15/creating-an-ssl-certificate-for-node-dot-js/
Create CSR for none self signed certificate: http://www.rackspace.com/knowledge_center/article/generate-a-csr-with-openssl

Hello world;


var fs = require('fs'),
    http = require('http'),
    https = require('https'),
    express = require('express');

var port = 8000;

var options = {
  key: fs.readFileSync('/home/ubuntu/ssl/server.key'),
  cert: fs.readFileSync('/home/ubuntu/ssl/server.crt'),
};

var app = express();

var server = https.createServer(options, app).listen(port, function(){
  console.log("Express server listening on port " + port);
});

app.get('/', function (req, res) {
    res.writeHead(200);
    res.end("hello world\n");
});

@EliEladElrom
Copy link
Author

EliEladElrom commented Sep 16, 2014

bashrc on nodejs server:

> vim ~/.bashrc
RUN -> . ~/.bashrc

see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)

for examples

alias stopnode='sudo forever stop 0'
alias startnode='sudo forever start /home/ubuntu/www/server.js'
alias cdr='cd /home/ubuntu/www'
alias vimb='vim ~/.bashrc'
alias runb='. ~/.bashrc'
alias taillog='tail -f /home/ubuntu/www/data/eventslogs'

enhance commands

alias l='ls -ltra'
alias c='clear'
alias cls='clear'
alias ll='ls -ltra'
alias killnode='sudo killall -2 node'

export

export PORT=8081
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8081

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment