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.
- Do this in your aws console
- add a rule to your security group for
HTTTP
and the default port should be 80. Save the new rule.
- follow the connect to instance guide on the aws console
$ sudo adduser <USERNAME> sudo
$ sudo yum install nginx
$ sudo chkconfig --level 345 nginx on
$ 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
aslocalhost
- 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
$ sudo service nginx restart
$ sudo wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | sudo bash
$ sudo nvm install 5.9
- might be installed with current version of node via nvm
$ npm install -g pm2 --unsafe-perm
- 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
$ pm2 start server.js
- don't forget to run
npm install
at the root of your project folder
- add aws ip address to browser