Skip to content

Instantly share code, notes, and snippets.

@mahendra0859
Last active March 23, 2020 05:29
Show Gist options
  • Save mahendra0859/b9abd0e56cb03f54ac2a9ccf854983ff to your computer and use it in GitHub Desktop.
Save mahendra0859/b9abd0e56cb03f54ac2a9ccf854983ff to your computer and use it in GitHub Desktop.
Node app deploy with nginx & SSL

Node.js Deployment - NGINX, SSL With Lets Encrypt

Steps to deploy a Node.js app to AWS EC2 ubuntu using PM2, NGINX as a reverse proxy and an SSL from LetsEncrypt

1. Create an EC2 instance and login using SSH

$ ssh -i <pem file> ubuntu@<public IP>

2. Install Node/NPM

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

$ sudo apt install nodejs

$ node --version

3. Clone your project from Github

There are a few ways to get your files on to the server, I would suggest using Git

$ git clone yourproject.git

4. Install dependencies and test app

$ cd yourproject
$ npm install
$ npm start (or whatever your start command)
# stop app
$ ctrl+C

5. Setup PM2 process manager to keep your app running

$ sudo npm i pm2 -g
$ pm2 start app (or whatever your file name)

Documentation : https://pm2.keymetrics.io/

# Other pm2 commands
$ pm2 show app
$ pm2 status
$ pm2 restart app
$ pm2 stop app
$ pm2 logs (Show log stream)
$ pm2 flush (Clear logs)

You should now be able to access your app using your IP and port. Now we want to setup a firewall blocking that port and setup NGINX as a reverse proxy so we can access it directly using port 80 (http)

6. Setup ufw firewall

$ sudo ufw enable
$ sudo ufw status
$ sudo ufw allow ssh
# For Port 22
$ sudo ufw allow http
# For Port 80
$ sudo ufw allow https
# For Port 443
$ sudo ufw allow 8081
# For Port 8081

7. Install NGINX and configure

$ sudo apt install nginx

$ sudo nano /etc/nginx/sites-available/default

# to uninstall NGINX

$ sudo apt-get purge nginx nginx-common
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment