Install nodejs on centos 7
Node.js is an open source JavaScript runtime built on Chrome’s V8 JavaScript engine, and can be used to build different types of server-side applications. npm is a package manager for JavaScript, with hundreds of thousands of packages it is the world’s largest software registry.
we need to add yum repository to install nodejs. Before that you need to install few packages like below
yum install gcc gcc-c++ make
then you will need to add NodeSource yum repository on your system. Add it by using curl running following command.
[root@lampblogs ~]# curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash -
[root@lampblogs ~]# yum install -y nodejs
[root@lampblogs ~]# curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
[root@lampblogs ~]# yum install yarn
now you can check versions like below
[root@lampblogs ~]# node --version
v12.11.1
[root@lampblogs ~]# npm --version
6.11.3
Test installation
For testing purpose we can create one test file
vi hello_world.js
sample test file
[root@lampblogs ~]# vi hello_world.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
}).listen(3000, "192.168.0.10");
console.log('Server running at http://192.168.0.10:3000/');
Replace 192.168.0.10 with your system ip and save the file
Now run below command to check its running or not
[root@lampblogs ~]# node hello_world.js
then you can access http:ip_address:3000 in your browser,you will see ‘Hello World’ like below.
Run nodejs server in Background
if you run 'node hello_world.js' it will run until terminal is closed.once terminal is closed nodejs server also will stop.To avoid this you can start nodejs server with nohup command like below
[root@lampblogs ~]# nohup node hello_world.js &
Now exit the terminal and check with ps command
sample output
[root@lampblogs ~]# ps -ef | grep node
root 7843 2644 3 18:38 pts/0 00:00:00 node hello_world.js
root 7854 2644 0 18:38 pts/0 00:00:00 grep --color=auto node
Run nodejs server at background with forever
There are few tools to help you run Node apps, most notably forever & pm2.
For now we will see forever
Forever is a Node JS package which can make a node script execute forever even after the node script process is killed. It will execute the node js script in a new process when the old process has been stopped suddenly.
For this install Node forever package. Below command will install the forever package globally.After install, run npm list command to see the installation path.
[root@lampblogs ~]# npm install forever -g
[root@lampblogs ~]# npm list forever -g
Start node http web server with forever start command.
[root@lampblogs ~]# forever start hello_world.js
From below output message, you can see first forever start the http web server in process with id 8652, after you kill that process, forever start another process ( id is 8820 ) to run the node http web server immediately. So the http web server will run at background continuously.
[root@lampblogs ~]# ps -ef | grep node
root 8644 1 3 18:48 ? 00:00:00 /usr/bin/node /usr/lib/node_modules/forever/bin/monitor hello_world.js
root 8652 8644 1 18:48 ? 00:00:00 /usr/bin/node /root/hello_world.js
root 8680 2644 0 18:49 pts/0 00:00:00 grep --color=auto node
[root@lampblogs ~]# kill -9 8652
[root@lampblogs ~]# ps -ef | grep node
root 8644 1 1 18:48 ? 00:00:00 /usr/bin/node /usr/lib/node_modules/forever/bin/monitor hello_world.js
root 8820 8644 6 18:50 ? 00:00:00 /usr/bin/node /root/hello_world.js
root 8829 2644 0 18:50 pts/0 00:00:00 grep --color=auto node
you can 'forever list' command to check all running forever process.
you can stop the running node script with 'forever stop' command.
[root@lampblogs ~]# forever stop hello_world.js
That's it.Now we have successfully installed and learned how to run nodejs server in background.