Deploy Node.js app on heroku

Signup/Signin on heorku

https://herokuapp.com/

Login from terminal (Make sure you have installed heroku cli – https://devcenter.heroku.com/articles/heroku-cli#download-and-install)

heroku login

Create app.js

const express = require("express");
const app = express();
const port = 8080;

app.get("/", (req, res) => {
    res.send("Hello Heroku");
})

app.listen(process.env.PORT || port, () => {
	console.log("listening 8080...");
});

process.env.PORT this will be provided by heroku server

Test locally by running following command

node app.js
OR
nodemon app.js

If you get any error e.g. module not found you can install those module using npm

npm install <module_name>

To find installed module version

npm view <module_name> version
e.g.
npm view express version

Create package.json

{
	"scripts" : {
		"start" : "node app.js"
	},
	"dependencies": {
		"express": "4.17.1",
		"mongoose": "5.13.3",
		"cors": "2.8.5"
	}
}

Run following command from terminal

#onetime
git init
#onetime
heroku create <yournewappname>

Run git commands

git add .
git commit -m 'msg'
#to verify origin
git config -l
git remote add origin heroku_git_url
git push origin master
#git push heroku master

Once app is deployed it will show you an url which you can access publicly from internet.

To see error logs

heroku logs --tail

Nginx

create project and verify with normal url

mkdir /var/www/html/myproject

vim index.html

<h1>Hello World</h1>

vim index.php

<?php

phpinfo();

Create server block conf file

vim /etc/nginx/conf.d/myproject.conf

server {
  listen 8082 default_server;
  server_name _;

  index index.php index.html;

  root /var/www/html/myproject;

  location ~ \.php$
  {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
  }
}

Test nginx configuration

nginx -t

restart nginx

sudo service nginx restart
OR
sudo systemctl restart nginx

For Laravel Project
Specify path till public directory