12

I have a VPS in bluehost, I installed node and npm through SSH and I've managed to run a node app in www.mywebsite.com:3000, so I have two questions:

1) How do I run the app.js so when it 'listens' to www.mywebsite.com ? I mean when I go to www.mywebsite.com I see my index.ejs and everything like in my localhost instead of going to www.mywebsite.com:3000.

2) When I managed to run the node app in www.mywebsite.com:3000 it would close if I stopped the SSH conn, it doesn't stay 'forever', how do I start the server once and keep it up? Like with Heroku, where one would upload the package.json with the script "node app.js" and the server will always be up and running.

How I ran the node app in the port 3000:

First I ran npm init, then I installed express npm install --save express

app.js:

var express = require("express");
var app = express();

app.get("/", function(req,res){
     res.send("hi");
});

app.listen(3000, functions(){
     console.log("Server Started");
});
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
LuisEgan
  • 952
  • 2
  • 13
  • 28
  • 1
    Re: #1 you should create a reverse proxy from port 80 or 443 using another server like apache or nginx to port 3000 (or whatever port you specify to run the nodejs app on). #2: speaking of [forever](https://github.com/foreverjs/forever) -- pm2 is also a good alternative – Explosion Pills Jan 11 '17 at 18:03
  • I actually used the port 3000 to the test, is it necessary to use a reverse proxy to deploy a nodejs app, or just if I want it in a specific port? – LuisEgan Jan 11 '17 at 19:02
  • I used forever and now app.js stays up. I also changed the app.js to listen to port 80 but it doesn't work. There is already an index.html in the public_html directory and it is loading that, how do I get it to load app.js first? – LuisEgan Jan 11 '17 at 19:32
  • You should use a reverse proxy for port 80/443 (SSL Termination). If port 3000 is open to the world based on your firewall / network settings you can still access it directly as well. If you want to run a test vs. production version of the app I would do that on separate servers. – Explosion Pills Jan 11 '17 at 19:32
  • ok, I understand what your are saying but I don't know how to do it.. though it doesn't answer if that is necessary or good practice. How do I use a reverse proxy in centos 6? – LuisEgan Jan 11 '17 at 22:33

3 Answers3

10

i had the same issue but i resolved it

follow these steps:

1- open your ssh access from cpanel security then click manage ssh keys

2- click generate a new key then enter your password (you should remember your password as you will need it) then click generate key

3- after generating your new public key it will be listed in your public keys list click on manage then authorize your key

4- click on the private key for the public one you have just created (you will find private keys list under public keys)

5-insert your password then convert your key to ppk format then download it

6- download and install putty for ssh access

7- open putty then enter your host name and select ssh from the radio buttons list

8- after that on the left side under connection list open ssh list then click on auth

9- import your private key then click open

10- a new prompt will open enter your domain username (you'll find it in general information section in your cpanel) and your password which you used for converting your key to ppk format

11-if you could not connect to ssh, contact with bulehost support to enble your ssh access

12- after you are connected install nvm from this link https://github.com/nvm-sh/nvm

13- use the command nvm install for installing your required node version like nvm install 12.14.0

14- after that create your server folder then use npm for installing your required packages

15- first npm init then npm install****=>(your packages)

16- compress your server files then upload them to your server folder

17- go back to your putty ssh then navigate to your server folder

18- now write your command like node app.js it should work fine

19- for running nodejs as a backend service you can use forever (https://www.npmjs.com/package/forever )

Hossam
  • 367
  • 3
  • 9
  • Thanks for all of this @Hossam. I'm at install nvm. connected with Putty, I did a git clone and can see the nvm folder on my cpanel directory. I also ran the command "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash" as outlined in the instructions. However, I can't install anything in the sense of I create a server folder, cd into it, run npm install and I get the error "-jailshell: npm: command not found". I think that I did not get nvm installed, or I haven't got node (npm) installed. Any tips to help me through this? – Casivio Oct 31 '20 at 19:18
1

Node 15 requires extra files or updates in bluehost. Node 10 works just fine.

Ahmet Emrebas
  • 566
  • 6
  • 10
0

Try this to make your app work on www.mywebsite.com :

    app.listen(80, functions(){
     console.log("Server Started");
});

What webserver are you using?

Kneel
  • 1
  • 2
  • 1
    I'm using bluehost. If I do that how will the server run app.js? I mean, when I do it locally I have to type from my terminal "node app.js" so the server starts – LuisEgan Jan 11 '17 at 18:57
  • 2
    This looks like a comment not an answer. Can you please add this to the comment section of the original question? – soundslikeodd Jan 11 '17 at 18:58