How to install Node.js on a VPS or a Dedicated server

Written by Marius Nenerica-Neamtu on Jun 2nd, 2021 Views Report Post

Node.js is a cross-platform JavaScript runtime environment. It’s the technology of choice for creating and hosting many different web-based applications. Programmers also use it to create web servers and networking tools. In this tutorial, we'll walk you through how to install node.js on a CentOS-based VPS or Dedicated server.

We will see into the next questions:

  1. Install Node.js through PuTTY
  2. Run Node.js Apps
  3. How to stop an application
  4. Connect your Website with a Running Node.js App

For the following instructions, we used the CentOS operating system with cPanel installed. However, the same steps apply to virtually all Linux-based OS, provided they have Apache as their web server.

First of all, it is required to access the server in question via SSH as a root user.

In case you are using a Unix-based OS (Linux or macOS), you can easily run the Terminal Application (a command line emulation program and connect to the server) using the command:

ssh root@serverip -pPORT

This command has the following values:

Serverip - the IP address of your VPS or Dedicated server PORT - 22.

If you're using Windows OS, you can do this through an SSH client. You can find a list of free SSH clients here.

Install Node.js through PuTTY

In the next example, we used the PuTTY SSH client to install Node.js:

  1. Open PuTTY to launch the configuration screen. Here, you should fill out the following fields:

Host Name: the IP of the server; Port: your server’s port (22 by default); Connection type: SSH.

It should look something like this:

  1. The PuTTY Security Alert screen prompt (pictured below) appears the first time you connect. Click Yes:

You are now looking at the SSH prompt login screen:

  1. When prompted with login as:

Enter your username - root and press Enter.

After that, enter your root password and press Enter.

PLEASE NOTE: your password won’t be visible upon entry. It is an intentional security feature.

Completing these three steps logs you into your SSH server. From here, you can install Node.js.

  1. To download Node.js version 10 LTS archive in your root home directory, type in the following command:

cd ~ wget https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64.tar.xz

We've used this version as an example. If you need a different version, you can select an alternative from the official versions here.

  1. It’s now time to unpack the downloaded archive. Use the command:

tar xvf node-v10.16.3-linux-x64.tar.xz

  1. Replace node-v10.16.3-linux-x64.tar.xz with the name of the archive you have downloaded in your case.

Executing this command launches a long prompt with the names of the files that are getting extracted. No worries; this is absolutely normal. It means that the unpacking process was successful.

  1. Rename the extracted folder to nodeext with the following command:

mv node-v10.16.3-linux-x64 nodeext

  1. Install the node and npm binaries using the following commands:

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

  1. Node.js and NPM should now appear on the server. You can double-check they installed with the following commands:

node --version npm --version

Run Node.js Apps

This guide describes the installation of Node.js and NPM on the server. Naturally, you would like to use them with an actual application. There are different possible configurations for Node.js apps; as such, there are multiple ways to run them.

To run a Node.js application that is production-ready and has a package.json file included, you can use the following command:

nohup npm start --production &

In the case you are running a Node.js app that does not have a package.json file included, you can use the following prompt:

nohup node my_app.js &

In this case, you won’t be able to manage this application with npm.

How to stop an application

To stop a running Node.js application, you can execute the following command via SSH:

pkill node

This command kills any Node processes on the server.

Connect your Website with a Running Node.js App

Apache is standard for cPanel installations. If you run Apache,you can use a specific .htaccess file code to make your site work with the Node.js app. Follow these steps:

  1. Open up your cPanel and go to File Manager >> public_html folder.

NOTE: public_html is the folder of your site if the domain you are using is the primary domain of the given cPanel account. In case your domain is an addon domain, please check the cPanel >> Domains menu to see what folder it uses.

  1. Click on the Settings button on the upper-right and make sure that the Show Hidden Files (dotfiles) option is checked. Click Save after that.

  2. Click on the .htaccess file in the folder and click Edit:

In case the .htaccess file does not exist yet, create it using the + File option on the upper-left and create the file.

  1. Enter the following code:

DirectoryIndex disabled 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]

Now, replace XXXXX with the port your Node.js application is working on.

  1. Click Save Changes on the upper-right.

That’s it! Source

Comments (0)