Steps to deploy a Node.js app to AWS EC2 ubuntu using PM2, NGINX as a reverse proxy and an SSL from LetsEncrypt
$ ssh -i <pem file> ubuntu@<public IP>
$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
$ sudo apt install nodejs
$ node --version
There are a few ways to get your files on to the server, I would suggest using Git
$ git clone yourproject.git
$ cd yourproject
$ npm install
$ npm start (or whatever your start command)
# stop app
$ ctrl+C
$ 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)
$ 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
$ sudo apt install nginx
$ sudo nano /etc/nginx/sites-available/default
# to uninstall NGINX
$ sudo apt-get purge nginx nginx-common