1

I have a domain and a cloud server (running ubuntu 16.04 OS) and I trying to host a nodeJS project (with ExpressJS and AngularJS) on cloud server.

I have currently installed node, nginx on my cloud server. My app is currently running on localhost even on server.

This is my node server.js file I'm having.

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.set('port', (process.env.PORT || 3000));
app.use(express.static(__dirname + '/app'));
app.set('views', __dirname + '/app');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.use(bodyParser.urlencoded({
   extended: true
}));
app.use(bodyParser.json());


app.get('*', function(req, res){
    res.render('index.html');
});

app.listen(app.get('port'), function() {
});

console.log('Magic happens on port ' + app.get('port'));

Can someone help me by giving me detailed steps on how to host my nodejs project on cloud server with nginx.

My project directory structure is as follows

-project_directory_name
  |-app(folder_where_my_html_css_javascript_code_is_placed)
  |-node_modules
  |-package.json(file)
  |-server.js (node/express file)

I have placed my project_directory_name under the root (/) directory in my server.

Thank you in advance.

Ashwath S H
  • 481
  • 1
  • 4
  • 13

1 Answers1

1

Step of deployment:

  1. clone your code on any desired location.
  2. install npm and bower(if you have).
  3. install forever sudo npm install forever --global
  4. forever start server.js

Above will help you to start service of your node application.now your node app run.

Hosting nginx: Node.js + Nginx - What now?

 location / {
    proxy_pass http://127.0.0.1:3000/;
    proxy_read_timeout 120s;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

add this to nginx config file. just simple run your node app

Hope this may help you!

Community
  • 1
  • 1
Ankit Rana
  • 383
  • 6
  • 24
  • I tried the above stack overflow link, but that didn't help me. Can you tell me the location what I need to mention in nginx configuration file? – Ashwath S H Jul 18 '16 at 17:14
  • you don't need to specify any location, you basic need proxy pass where you can pass your localhost application port to 80 port – Ankit Rana Jul 18 '16 at 17:19
  • The link you have shared says to create server.js file under /var/www/yourdomain/server.js. So if I place my server.js file over here how about my project_folder? Where to place it and how will it work? – Ashwath S H Jul 18 '16 at 17:24
  • no need to move any file, just simple run with forever js and access app – Ankit Rana Jul 18 '16 at 17:32
  • I'm new to hosting... so you mean to say my server.js and my project_folder can be under root (/) directory and I can run with foreverjs? – Ashwath S H Jul 18 '16 at 17:35
  • just like run app on your local , same run on remote. so follow the same procedure that you follow on local but instead on write node server.js, you need to call forever start server.js, because forever.js create daemon service so when you can log out your session from sever ,your app still running. – Ankit Rana Jul 18 '16 at 17:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117629/discussion-between-foovirus-and-ashwath-s-h). – Ankit Rana Jul 18 '16 at 17:41
  • I'm getting this when I run 'forever start server.js` as I have shown in the snippet below. `root@localhost:/Coming_Soon_Site# forever start server.js` `warn: --minUptime not set. Defaulting to: 1000ms` `warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms` `info: Forever processing file: server.js` – Ashwath S H Jul 19 '16 at 04:50
  • after following those steps if I hit my website on browser its still loading Apache2 Ubuntu Default page itself. – Ashwath S H Jul 19 '16 at 05:20
  • are you use nginx or apache? see this link http://stackoverflow.com/questions/5009324/node-js-nginx-what-now?noredirect=1&lq=1 – Ankit Rana Jul 19 '16 at 12:19
  • Thanks a lot for you answers.. Now I'm able to host my website successfully. – Ashwath S H Jul 19 '16 at 15:34