Deploy Node.js on AMI EC2 Instance - nodox/aws GitHub Wiki

We are going to deploy a node.js application using nginx as a reverse web server proxy. I assume this is a newly created instance with no prior configurations. This tutorial is also backend server agnostic; you can use express or what ever server tool is out there for your node.js application.

Create new AMI ec2 instance

  • Do this in your aws console

Open instance to outside traffic

  • add a rule to your security group for HTTTP and the default port should be 80. Save the new rule.

SSH into instance

  • follow the connect to instance guide on the aws console

Add non-root user to sudoers

$ sudo adduser <USERNAME> sudo

Install nginx

$ sudo yum install nginx

Configure nginx to start on reboot

$ sudo chkconfig --level 345 nginx on

Edit nginx for port forwarding

$ sudo vim /etc/nginx/nginx.conf

server {
        listen       80;
        server_name  localhost;
        #server_name  your-ip-address;
        root         /usr/share/nginx/html;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;

        location / {
		proxy_pass http://localhost:8080;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection 'upgrade';
		proxy_cache_bypass $http_upgrade;
        }
  • For this we are forwarding to port 8080 as localhost
  • This part might look different for you but the location section should have these proxy settings. Nginx should be configured to listen to server 80 by default

Restart nginx

$ sudo service nginx restart

Install NVM for node and npm

$ sudo wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | sudo bash

Install node v5.9

$ sudo nvm install 5.9

Install npm

  • might be installed with current version of node via nvm

Install pm2 to daemonize your application and run it in the background

$ npm install -g pm2 --unsafe-perm

Add node project to ec2 instance

  • either create a simple server to ftp your project to the instance
  • make node server listen on port 8080, thats where we are forwarding requests to

Start your application with node or pm2

$ pm2 start server.js

  • don't forget to run npm install at the root of your project folder

Enter your ip address in browser

  • add aws ip address to browser

Made possible by this reference

Tutorial to deploy node on nginx to aws

⚠️ **GitHub.com Fallback** ⚠️