How To Install Latest Nodejs and NPM on CentOS/RHEL 7/6

Node.js is a free open source platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. More about NodeJS project you can find on their website here: https://nodejs.org/en/

Today I am going to explain here briefly how to install latest NodeJS server with some packages (Grunt - http://gruntjs.com/installing-grunt).

Step 1 – Add Node.js Yum Repository
First, we will add a node.js yum repository in our system provided by the NodeJS official website. You also need development tools to build native addons to be installed on your system as described here https://nodejs.org/en/download/package-manager/

Since we base on CentOS 6 and 7 version we will push following commands:

Run as root on RHEL, CentOS or Fedora, for Node.js v6 LTS:

curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -  

Alternatively for Node.js 8:

curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -  

Alternatively for Node.js 0.10:

curl --silent --location https://rpm.nodesource.com/setup | bash -  

Then install, as root:

yum -y install nodejs  

Optional: install build tools

To compile and install native add-ons from npm you may also need to install build tools:

yum install gcc-c++ make  

or: yum groupinstall 'Development Tools' (which you can push on dev env server)

Step 2 – Check Node.js and NPM Version
After installing node.js application/server, verify and check the installed version. You can find more details about current version on the node.js official website.

node -v  

Results is going to be displayed as:
[root@second ~]# node -v
v6.10.3

Also, check the version of npm.

npm -v  

[root@second ~]# npm -v
3.10.10

Step 3 – Create Demo Web Server (Optional)

This is an optional step. If you want to test your node.js install. Let's create a web server with “Welcome Node.js” text. Create a file demo_server.js

$ vim demo_server.js and add following content

var http = require('http');  
http.createServer(function (req, res) {  
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Welcome Node.js');
}).listen(3001, "<ENTER YOUR SERVER PUBLIC IP HERE>");
console.log('Server running at http://<PUBLIC IP ADDRESS:3001/');  

Now start the web server using below command.

node --debug demo_server.js  

debugger listening on port 5858
Server running at http://PUBLIC-IP-ADDRESS:3001/

The web server has been started on port 3001. Now access http://:3001/ url in your browser.

Note: Make sure that port 3001 or whatever you wish to use is open on the server iptables.

You may also require packages. Much like ruby or python, these are easy to install such as the following examples:

npm install grunt  

More about grunt and how to install it you can read here:
http://gruntjs.com/installing-grunt

npm install grunt-cli  

More about grunt-cli and how to install it you can read here:
http://gruntjs.com/using-the-cli

After you install Node.js, you are ready to run Node.js applications. However, the exact steps to do this vary depending on the application configuration.

Method #1: Use npm

Many third-party and “production-ready” applications (such as Ghost) use the npm program to start the application, as shown by the following command:

nohup npm start --production &
The & places the command in the background, and the nohup command ensures that the application continues running even if you log out of the current terminal session.

For this method to work, there must be a valid package.json file for the application. The package.json file contains project metadata that the npm program reads to determine how to start the application, manage its dependencies, and more.

To view the official npm documentation for the package.json file, please visit https://docs.npmjs.com/files/package.json.

Method #2: Run node directly

For simple applications, or for any application that does not have a package.json file, you can run the node executable directly and specify the application filename. For example:

nohup node my_app.js &
However, you lose the benefits of using npm to manage the application.

As above, the & places the command in the background, and the nohup command ensures that the application continues running even if you log out of the current terminal session.

STOPPING A NODE.JS APPLICATION

To stop a currently running Node.js application, type the following command:

pkill node
This command immediately stops all running Node.js applications.

INTEGRATING A NODE.JS APPLICATION WITH THE WEB SERVER

Depending on the type of Node.js application you are running, you may want to be able to access it using a web browser. If you are using Apache and even better cPanel/WHM server you can adjust .htaccess file. To do this, you need to select an unused port for the Node.js application to listen on, and then define server rewrite rules that redirect visitors to the application. The following steps demonstrate how to do this:

In a text editor, add the following lines to the .htaccess file in the /home/username/public_html directory, where username represents your account username:

RewriteEngine On  
RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]  

In both RewriteRule lines, replace XXXXX with the port on which your Node.js application listens.

To run a Node.js application on a managed server, you must select an unused port, and the port number must be between 49152 and 65535 (inclusive).

Save the changes to the .htaccess file, and then exit the text editor. Visitors to your web site are redirected to the Node.js application listening on the specified port.

If your application fails to start, the port you chose may already be in use. Check the application log for error codes like EADDRINUSE that indicate the port is in use. If it is, select a different port number, update your application's configuration and the .htaccess file, and then try again.

MORE INFORMATION
For more information about Node.js, please visit http://nodejs.org.