Launch a Node.Js server when the operating system starts - node.js

I have a website written in Node.Js that I launch with this command :
cd /var/mywebsite
npm run dev
What should I put in /etc/init.d/rc.local ?
A solution is : https://causeyourestuck.io/2016/04/30/run-node-js-script-startup/
But I don't know how to create the app.js...
Thanks !

you could use continues integration tools such as puppet to help you do accomplish this. You can also integrate it with Jenkins for the full lifecycle.
https://forge.puppet.com/puppet/nodejs

Given that you are on Linux Ubuntu I think the best and most simple option is a CronTab
#crontab -e
#reboot /home/user/startServer.sh
then your shell/bash script should look something like this
#!/usr/bin/bash
node server.js
or
#!/user/bin/bash
npm run dev
Let me know if that helps, or if you run into issues. I haven't used linux in a few months :)
more info: How to run a shell script at startup

I would recommend a process manager like pm2.
npm i -g pm2
#install pm2
pm2 start <in your project directory>
#starts the project (can start multiple)
pm2 startup
#setup init scripts to start processes on reboot
pm2 save
#saves list of processes
Ref: http://pm2.keymetrics.io/docs/usage/startup/

If you want to use forever-service module, you can use it like this:
Install forever & forever-service packages globally:
sudo npm install -g forever forever-service
Then register your app as a service (default to app.js, but you can specify the name of your app if you need to):
sudo forever-service install your_app --script the_name_of_your_app --start

I have understood the problem and founded the solution !!
A big thank to #Brahma Dev and #TGrif for their help !
The problem was that on the server I have node.js install globally (via apt-get) and an other version installed a the user which launch npm run dev
And if I execute su myuser -c "node -v, su doesn't execute .bashrc before node -v.
So if I execute su myuser and then node -v, the version of node is different !
I have solved the problem by creating an sh script containing :
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
npm run dev
And now, su myuser -c "./start-server.sh" works !
So if I add su myuser -c "/var/www/dialoguea.co.tools/dialoguea/start-server.sh" & in /etc/rc.local the node.js server loads when ubuntu is restarted.

Related

Why installed Node.js and Yarn is lost after reconnect to EC2 linux instance

I have on AWS EC2 instance. When I install Node.js with this
AWS tutorial all works fine until i logout current session and log in, than Node.js and Yarn are gone. There is also note from AWS:
The node installation only applies to the current Amazon EC2 session. If you restart your CLI session you need to use nvm to enable the installed node version. If the instance is terminated, you need to install node again. The alternative is to make an Amazon Machine Image (AMI) of the Amazon EC2 instance once you have the configuration that you want to keep, as described in the following topic.
Is it possible to install Node.js so it is available in another session? I installed zsh and that works for all sessions.
I also created simple script that will install Node.js and Yarn. Commands in this script are from AWS Tutorial page that I mentioned early. It will install Node.js and Yarn, show that all works (it shows Yarn version and message from node command), but when I type npm --version or yarn --version it will shows message zsh: command not found: yarn (or similar message for npm).
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts
node -e "console.log('Running Node.js ' + process.version)"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
echo "installing YARN !!!!! "
npm install yarn --global
Why is my script not working?
If you use manual from AWS how to install Node.js than instalation is only made in your home directory that lives only for current session. To install Node.js and Yarn I used this post: https://stackoverflow.com/a/35165401/78935
Or you could simply use this:
sudo curl --silent --location https://rpm.nodesource.com/setup_16.x | bash
sudo yum -y install nodejs
sudo npm install yarn --global
yarn --version

Running npm install on Ubuntu with Octopus Deploy

We are using Octopus deploy to deploy an angularjs app. I'm running a post deployment script (bash) and in there I try to do npm install. This doesn't work, I get an error
npm: command not found
However if I login to the linux box as the Octopus user, go to the directory that Octopus Deploy is trying to run the script from, I am able to run npm install without error.
I've confirmed its the right user (running whoami before npm install in the post deployment script).
I've tried adding the npm executable to my PATH variables which didn't work. I've also put the full path to npm in my script which gives me a new error
/usr/bin/env: ‘node’: No such file or directory
Please run the following command as octopus user to access npm for other users.
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
Or use nvm to install node
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
source ~/.bashrc
source ~/.profile
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  
nvm ls-remote
nvm install 8.10.0 
node --version
npm --version
which node
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
https://github.com/creationix/nvm
If you are installing new node version, please do the following steps.
nvm list will show all installed node versions
nvm use v8.10.0
nvm alias default v8.10.0 set default node version for current user

Start node server on machine startup

I was trying to start node.js server on startup of the machine (ubuntu 16.04) with upstart by using the the following code in nodeserv.conf file:
#!upstart
description "Node.js server"
author "Sushant Kumar"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export APP_HOME = "/home/ubuntu/chatbot_server"
export HOME = "/home/ubuntu"
cd $APP_HOME
exec sudo -u ubuntu /usr/local/nodejs/bin/node $HOME/chatbot_server/server.js >> /var/log/chatbotserv.log 2>&1
end script
post-start script
echo "Node Started"
end script
, but I run the command
# start nodeserv
I get the followig error: >>start: Job failed to start.
Can anyone help me please where I am going wrong?
Edit: This server is hosted on AWS EC2 instance (if that helps, I don't think it's relevant, but just in case).
Have a look on PM2.
PM2 is a really powerful Node.js process manager.
After install your app, you can easily set it on startup with:
sudo systemctl start pm2-yourusername
You can do this by running your app as a service. You can use forever to ensure that a given script runs continuously. First of all you need to install forever. Then go to your project directory and install forever-monitor. Now you can start your app.
npm install forever -g
cd /path/to/your/project
npm install forever-monitor
forever start app.js
Now you need to use forever-service to build your node script as a service.Firstly, install forever-service and then install your app as a service.
npm install -g forever-service
forever-service install test
If you want to work on your script, you can replace this code in your script.
export HOME="/root"
exec /usr/local/nodejs/bin/node /home/ubuntu/chatbot_server/server.js >> /var/log/node.log 2>&1

PM2 command not found

I installed node.js and npm to my centOS 7 server. But i have problems with pm2.
Actually real problem is i don't have experiences in linux and i don't know how to change path.
Here is folder structure.
* bin
* code
* error_docs
* httpdocs
* lib64
* logs
* tmp
* var
* chat(my node.js folder)
* node_modules
* pm2
* sockjs
* server.js
* dev
* etc
* lib
* local
* sbin
* usr
I entered folder by typing cd chat and installed pm2 with npm install pm2.
After that I tried use pm2 for my server.js by typing pm2 server.js server returns "pm2 command not found". I can use node.js without any problem but pm2 not working.
How can i solve this?
Install PM2 globally:
run as root:
npm i -g pm2
or if user is sudo-er
sudo npm i -g pm2
and then go back to user (or stay in root if it was created by root user) and run it:
pm2 start server.js
PM2 the process manager for Node.js applications. PM2 basically manages applications (run them in the background as a service). So this is how we install PM2 globally with sudo permissions account
sudo npm install -g pm2
The -g option tells npm to install the module globally, so that it's available system-wide.
Once this is installed, check the installed path as:
whereis pm2
pm2: /opt/node/bin/pm2 /opt/node/lib/node_modules/pm2/bin/pm2
Now, we need to add this path in startup bash script. Add add the following line anywhere in ~/.bashrc file.
export PATH=$PATH:/opt/node/lib/node_modules/pm2/bin
Now re-login or source the bash script as follows(so that bash script runs and path is set)
source ~/.bashrc
and now it should run. check the status of pm2
pm2 status
In my case, I have MacOs Big Sur running with zsh shell.
The first thing you need to do is get the prefix of your npm-global path:
npm config get prefix
Then this will be return some thing like this:
/Users/your_user/npm-global
Copy this path, and add the /bin in the end -> /Users/your_user/npm-global/bin. Then we will export this path into the bash configs.
export PATH=$PATH:/Users/your_user/npm-global/bin
I believe all yours global npm packages will work fine now.
Error on using port 80 with PM2?
The wrong way of going about this is trying to run with sudo.
The correct way of doing this would be to login as root sudo su, then run pm2 start app.js --name "whatever" --watch.
Logging in as root, there's no need to configure any bashrc or profile files. However, as root, the script can use nodejs's exec() function dangerously. To avoid this, do the root stuff first with your script, then lower your privilege after some timeout:
// I use port 80 first.. at this point the script's UID is root.
app.listen(80);
// After 2 seconds we switch to UID `azureuser`, which obviously isn't root anymore.
setTimeout(function() {
process.setuid("azureuser");
}, 2000);
If you install through NPM and it does not work, you can create a symbolic link as well :
ln -s /<your-user>/.npm-global/lib/node_modules/pm2/bin/pm2 /usr/bin/pm2
After that, you're going to be able to call:
pm2
Install PM2 globally and run everything as a root user
sudo apt-get install npm
sudo npm i -g pm2
sudo ln -s /usr/bin/nodejs /usr/bin/node
You are good to go
If you used nvm to install node and npm, install pm2 for normal user.
run as root:
sudo su
vim ~/.bashrc
append below code, change NVM_DIR to you normal user's home folder:
export NVM_DIR="/home/[PLEASE CHANGE]/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \.
"$NVM_DIR/bash_completion"
# This loads nvm bash_completion
at last :
source ~/.bashrc
This option helped me:
sudo npm i -g pm2
yum install npm -y
npm config set strict-ssl false
npm install pm2 -g
sudo npm i -g pm2
It worked for me.
The same as #Henrique Van Klaveren answer but simpler with using substitution:
export PATH=$PATH:$(npm config get prefix)/bin

Nodejs forever, run on boot with --watch not working

I am new to linux and have just set up a server over at linode using ubuntu 12.04.
I have created a "myconfig.conf" file in /etc/init/ containing the following
start on startup
stop on shutdown
respawn
exec sudo -u myUser /usr/local/bin/forever start -w /home/myUser/myProject/server.js
When I reboot the linux server node has not been started (in some cases I can access the node server for 1 second before it dies). If I run this without the watch option ("-w") it works fine. I can also use the watch option when running manually without a problem, only happens when using the conf file. Is there some path I have to specify so that forever knows what files to watch?
Thanks in advance!
I use forever-service and nodemon and then use the chkconfig utility to have it start on reboot.
For example:
This forever-service command does the following: everytime a json or raml file in the applications dist/assets folder is modified, wait 10 seconds and then restart the node app (server.js script):
$ forever-service install raml --script server.js -f " -c nodemon" -o " --delay 10 --watch dist/assets -e json,raml --exitcrash" -e "PATH=/usr/local/bin:$PATH"
Then I can set the service to start on server reboot:
$ chkconfig --add raml
$ chkconfig raml on

Resources