Node.js Installation tutorial

Introduction:

Node.js is mainly utilized for hosting third-party applications like the popular Ghost blogging platform or can be used to execute your own applications. Follow this guide to install and run Node.js on your server.

Step 1: Log into your server via SSH

Step 2: Download the Node.js binaries by entering the following command:

wget https://nodejs.org/dist/v4.4.1/node-v4.4.1-linux-x64.tar.xz

If you wish to download any later version, then change the version in the above command. The above command will get you the 4.4.1 LTS (long-term support) release.

Step 3: Once the download is completed, enter the following command to extract the downloaded file:

tar xvf node-v4.4.1-linux-x64.tar.xz

Step 4: Rename the location folder as nodejs for your convinience by entering the following command:

mv node-v4.4.1-linux-x64 nodejs

Step 5: Install the node and npm binaries, enter the following commands:

mkdir ~/bin
cp nodejs/bin/node ~/bin
cd ~/bin
ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm

The execution of the above command will install node.js and npm to your server.

To check the same, input the following commands:

node --version
npm --version

The above commands should establish the respective versions of the node.js and npm on your machine.

How to start a node.js application:

There are two default methods of starting the node.js application:

Npn Method:

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 &

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.

Direct node method:

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 &

How to stop a node.js application:

The nohup command used to run the execution syntax ensures that the application continues running even if you log out of the current terminal session.

To stop the node.js application:

pkill node

This command will terminate all the node.js functions running at that instant.

Integration of the node.js application to your webserver:

Depending on the type of Node.js application you are running, you may want to be able to access it using a web browser. 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:

Step1: Open a text editor in the terminal.

Step 2: Open the file in the directory home/username/public_html and access the file .htaccess

cd home/username/public_htm

Note: Replace username in the above command with your actual username.

Step 3: Add the following information to the .htaccess file:

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]

Replace XXXXX with the port on which your Node.js application listens.

Note: 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)

Step 4: 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 the node.js application fails to start, the port you have chosen 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.

Add A Comment