Deploying my node.js app from Github to a VPS - node.js

I have an node.js application on Github. I have never done any VPS deployment before and I am learning on the go.
I am using the VPS by Hostinger.in, the OS being used is Ubuntu 14.04. So far this is what I have done:
Connected to their SSH successfully from my Terminal
Installed node.js on the server [https://www.hostinger.com/tutorials/vps/how-to-install-node-js-on-ubuntu]
Installed Git on the server [https://www.hostinger.com/tutorials/how-to-install-git-on-ubuntu]
I could not find any online resources for deploying my node.js to Hostinger VPS so I am following the ones written for DigitialOcean.
The one tutorial I followed is this: https://code.tutsplus.com/tutorials/setting-up-continuous-integration-continuous-deployment-with-jenkins--cms-21511
I cloned my repository doing:
git clone https://github.com/myusername/node-project.git
and it seems it was deployed (didnt give me any errors).
All the installations I did on the server I did as the root/admin user. So far I have not created any separate user to perform any of these tasks.
The server hostname given to me is dangerous-pigs.com. Now I am assuming my node.js application is deployed, but when I go to dangerous-pigs.com it shows me server not found error.
I also installed forever for my node app and when I run
forever start app.js
it says:
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: app.js
error: Cannot start forever
error: script /root/app.js does not exist.
Which means the app is either not installed or installed somewhere other than the root folder.
There is a lot going on and I am confused where to start fixing issues.
How can I deploy the app to running it on the dangerous-pigs.com?
Update
So it seems I have to go inside the project folder in root and do the
npm install --production
after which I did
node app.js
The server seems to be running but, I can only access my application if I do to the actual IP provided by the service.
So if I type http://93.188.163.249:8000 --> that's my application.
How do I change it to point to a domain?

After some more research this is what I found:
Currently by default Apache2 runs on port 80. To run nodejs on port 80 first I need to install libcap2-bin in my Ubuntu server by doing:
sudo apt-get install libcap2-bin
after which I do
sudo setcap cap_net_bind_service=+ep /usr/bin/nodejs
the above command works if you have a mac, for windows the command maybe
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
but please confirm before doing it.
Also your nodejs server needs to be stopped before you make these changes else it will not work. In my case I had forgotten to stop my node server and kept running the sudo setcap command but it did not changed the port (for obvious reasons).
If you are using forever to run node then do:
forever stopall

Related

nodejs - install production build of reactjs app on offline server, then set up to run as a service

I installed the following package globally using
npm install -g serve
on my development PC which has internet. It is currently the only package installed globally on my PC.
https://github.com/vercel/serve#readme
After I compile and create a build folder, i run the following command to have the production version of code run using HTTPS.
serve -s build --listen 3000 --ssl-cert "/my/cert/server.crt" --ssl-key "/my/key/server.key"
Ok, so here comes the problem. I have to run my code on a server that is not connected to the internet. I've copied and moved the global node_modules folder over to the server and confirmed the permissions are the same between the server and dev pc. I also confirmed that the package is seen as installed on the server by:
npm list -g --depth=0
When I run the serve command above on the server, it has no clue what serve is. CentOS complains about
bash: serve not found...
So, I added an alias to the ./~bashrc file on the server (which I also had to do on my dev pc and reloaded:
vim ~/.bashrc
export PATH="$(npm bin -g):$PATH"
source ~/.bashrc
I found the export command in another SO post, and it worked on my dev pc, however linux still does not recognize the serve command. I also need to know how to determine how to run this command from within a service, which means I also need the absolute path of the "serve" command.
I'm kinda stuck on this, since all the articles online only talk about how to run "npm start" as a service, which I can do for a development build of software with no issues. I cannot find anything on how to set up a service for a production build.
I dont 100% need to use serve, but my other coworker is using it for his project, which has internet access.
I've even gone as far as trying to piece together the location of the main js file in the module:
/path/to/node_modules/serve/build/main.js -s build --listen 3000 --ssl-cert "/my/cert/server.crt" --ssl-key "/my/key/server.key"
This will allow me to start the service, but then I get all kinds of cross site scripting errors from my apache backend server.
Any help would, as always, be appreciated! Thanks!
Disregard,
When I ran npm install -g serve on the pc with internet, it created a soft link to the serve module. I discovered this by running the following on my dev pc:
which serve
I created that soft link on the offline server, and it appears to be working.

Nodejs automatically shuts after running for some time

My environment is ubuntu 16.04 with npm 5.0.0 and node 8.0.0 installed. I created an express application and started it with nohup npm start & in the application root directory. However, a couple of minutes later the app became unreachable and the process was lost. I tried restarting several time but the process always automatically exits. How can I start a long-running nodejs app?
Use pm2 daemon for setting Up a Node.js Application for Production on Ubuntu 16.04
Follow commands
sudo npm install -g pm2
pm2 start server.js //Yor main node server.js
pm2 stop server //no need of giving .js , .js only required at start of process
pm2 restart server //for any changes are done to restart
For more You can follow digital ocean tutorials for more details from below link
Digital_Ocean_Link

How to install forever command without sudo

I have a node server on a VPS through Dreamhost. I want to run my node process forever. Currently, I can run the process but when I close my terminal it ends the process and the website no longer works. What I have read online is that I need to do forever node app.js but I am not given sudo access and can not install the command forever globally. How can I either install forever without sudo access or keep my node process running even after I close my terminal?
You should be able to activate Passenger on your VPS and have that manage your nodejs. Details on DreamHost KB https://help.dreamhost.com/hc/en-us/articles/215769578-Passenger-overview

Permanent socket.io server on Ubuntu Linux

I'm starting a socket.io server by entering a command:
node server.js
But that sometimes stops, or I need to have a terminal window open for it to run.
How can I set this up on a Linux server (Ubuntu) so there is a permanent server running in the system (like Apache) and if it stops it restarts automatically?
You can use PM2
after install the npm package you can use pm2 command line :
pm2 start server.js
You can use too nodemon or forever to detect when your server files changed. It will restart automatically your server and you don't need anymore to stop / start your node application.
Note than pm2 is used for production and nodemon for development
You can also use tmux (no need for any installations) by writing the following in the command line:
tmux
cd /path/to/application
node server.js
To exit the session while keeping the application running use:
Ctrl+b
d
You can also use upstarter and turn your node application into an ubuntu service. That's the thing I use in production.
To install:
npm install -g upstarter
To use:
sudo upstarter
And the remainder is just user prompts. In order to start/stop/restart your upstarter-generated service:
sudo start/stop/restart <name-of-the-service>
Upstarter also has this one big advantage over PM2/Forever: It can be used with non-node applications.

nodejs 'forever' just doesn't do anything

I am trying to run my nodejs app on an Amazon EC2 instance using forever. I installed nodejs npm, and then ran sudo npm install forever -g.
The installation didn't return any errors, so i go ahead and try running my app using forever start server.js, but it won't do anything - no output what-so-ever, not even an error.
I also tried forever --help and just forever, but none of them giving me any response...
When running my app regularly using nodejs - nodejs init.js then it works as expected, but i need to get it running using forever so it won't shut down when i disconnect from the server.
Edit :
Since the only problem i was having was that nodejs was closing when i closed the terminal session to my EC2 server, I solved this by using the linux nohup command like this :
nohup sudo nodejs server.js &
This kept nodejs running in a child process even after I closed the terminal window.
Thanks for the help though! :)
I was also not receiving any stdout input from any forever command and this fix nailed it:
sudo ln -s /usr/bin/nodejs /usr/local/bin/node
Hope that helps someone.
I don't know if this will help, but as an alternative, you can use upstart. Create a script like this:
#this is the upstart service task. move it to /etc/init/nodeapp.conf
description "server for Node App"
author "Neels Grobler"
chdir /root/servers/nodeapp
exec node init.js 1>stdout.log 2>stderr.log
respawn
And run it by typing start nodeapp. To stop type stop nodeapp. You can also change the script so that the node app starts on system boot etc. More info at upstart.ubuntu.com
You should be using forever start server.js not just forever server.js
Several guesses.
If you install using sudo npm install forever -g, you might need to sudo to run forever.
Put a full path of server.js when you run forever such as forever start /home/you/source/server.js or sudo forever start /home/you/source/server.js
On windows, when you run forever like:
forever start app.js
you can find generated log file in file system at:
C:\Users\USERNAME\.forever\SomeLogFileHere.txt
The log files are regenerated for every running script with unique identicator of each file. You can always check which file belongs to which process by:
forever list

Resources