Installing Node.js on a VPS Server

Installing Node.js on a VPS Server

Node.js is an open-source platform that helps Javascript users execute their code outside a web browser. It is a free solution that runs well on all kinds of operating systems – Linux, Windows, Unix, macOS, and more. In terms of hosting, VPS servers provide a perfect environment to integrate Node.js apps with developer tools and APIs.

Still, unless you’ve opted for a Managed VPS hosting plan, you will need to perform the Node.js installation on your own.

Table of contents:

What is Node.js

Why Node.js

What is Node.js Used For?

Installing Node.js and NPM from the Ubuntu Official Repository

Choosing to Install Node.js by Code or GUI

Install Node.js Through PuTTY

Running Node.js Apps

How to Stop an Application

Connect your Website with a Running Node.js App

Conclusion

FAQ


What is Node.js

Node.js is a cross-platform and event-driven JavaScript run time environment for server-side JS applications. It is built on Chrome’s V8 JavaScript engine alongside other development frameworks like MongoDB, Express.js, and AngularJS.

Node.js comes with the Node Package Manager (NPM). This handy portion gives you access to an extensive set of open-source libraries and helpful resources.

Why Node.js

Node.js is a lightning-fast scripting language. Since it is built on Google’s engine, it is likely only to improve further in performance over time. NPM also allows you to work smarter and more efficiently through pre-created code blocks.

Many projects, especially more complex ones, require several different languages like C, PHP, and Assembly. In such cases, you’ll need to carefully manage these languages all at the same time.

Тhis is not the case for Node.js, however.

With it, you can easily write an entire application from client to server to database. It can be the perfect programming language for coding a mobile game or chat application, for example. This is due to its event loop feature and multiple concurrent connections support. The solution lets you build robust, scalable, and fast web applications with no extra hassle.

No wonder Node.js is so popular among businesses of all sizes.

What Node.js is Used For?

Developers use Node.js to create web servers and networking tools. Due to its I/O handling capabilities, Node.js is perfect for writing an environment meant for asynchronous and event-driven I/O. Its very architecture allows for massive scalability.

In addition, Node.js is a perfect platform when it comes to getting fast server feedback. 

Back in the days, this was done with a hack – a series of frequent requests are sent to check on the status progress. However, this methodology proved inefficient and difficult – part of the reason why Node.js was created.

Installing Node.js and NPM from the Ubuntu Official Repository

Node.js can be installed in several different ways. They will differ depending on which Linux distribution you are using on your VPS. This section is intended to demonstrate the process of installing Node.js and NPM from the official Ubuntu repository

It should be said that while apt-get is a useful package manager, it does not reliably install the latest stable version of Node for Ubuntu. Therefore, we advise trying the installation from the NodeSource Debian and Ubuntu binary distributions repository (formally known as Launchpad PPA). 

Note: The latest Long-term Support (LTM) of Node is 14.x and NPM is 6.x as of current writing.

Update Your VPS

Before installing Node.js it is advisable to update the package index for Ubuntu virtual server. You can do so with this command:

$ sudo apt-get update

Install in Your Home Directory

Log in as the user through which you want to run Node.js. Make sure you go to your home directory if you’re not already there. You can do this and verify it with the following instructions:

$ cd ~ 

$ pwd

The first command moves your location to the home directory, while the second returns a path ending with your username – just so you can reconfirm it.

Installing Node.js

You need to execute the following three commands together. They will allow you to download and install the latest stable Node.js version for Ubuntu. Note the three instructions are combined into a single line:

$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash | sudo apt-get install -y nodejs’

Install Build Essential

Build Essential includes several important packages that are necessary for the installation. Chances are, it’s already installed together with Node.js with the steps above. Just to be on the safe side, type the following to ensure that Build Essential is installed:

$ sudo apt-get install build-essential

Check the Version Number of Node

This is an important step. You will need to check the version number of Node and the NPM to make sure you’ve installed the latest version – 14.x for Node.js and 6.x for NPM.

$ nodejs -v 

The above command should return a value of 14.x. 

$ npm -v

Following the above command, you should see a value of 6.x. 

Deciding to Install Node.js by Code or GUI

If you decide the command line installation of Node.js demonstrated above seems too much of a headache, you can also make use of the Ubuntu Package Manager. This is a sort of directory where you can simply select applications you want to install and perform the setup in a similar style to the Windows application installer

Note that in the context of a VPS server, not everyone will have the Graphic User Interface enabled. In fact, many VPS account owners choose not to have the GUI portion installed for performance reasons. GUIs tend to consume more server resources that could otherwise be allocated to your website.

In all fairness, there are ways of utilizing a graphic interface without too much load on your server. You could install the GUI just to make use of the package manager and later uninstall it when finished.

Install Node.js Through PuTTY

You can also choose to install Node.js via PuTTY, one of the most common SSH clients. To do so, you will need to first activate PuTTY, something your hosting provider can help you with. Once done, there are a few more steps:

Log in to your SSH Server

Run PuTTY to launch the configuration screen. You will need to input the following fields:

  • Host Name – the IP of the server
  • Port – your server’s port (22 by default)
  • Connection type – SSH

If you are connecting for the first time, you’ll be prompted to another screen – the PuTTY Security Alert. This window is simply there to inform you the server’s host key is not yet cached in the registry. You can proceed and click Yes.

You have now arrived at the SSH prompt login screen. Here, you need to input your root username and password. You should already have the credentials as they are usually in the welcome email once you purchase a VPS server. Take note that your password won’t be visible as you type it in – this is an intentional security feature.

Download and UnPack Node.js

To download Node.js and unpack the archive, enter the following instructions:

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

tar xvf node-v14.15.1-linux-x64.tar.xz

This should be the latest version as of current writing, but should you wish to install a different version for some reason, this is where you make amendments accordingly.

Replace Node with Archive Name

You will need to replace the node-v14.15.1-linux-x64.tar.xz with the name of the archive you have downloaded. Bear in mind that executing this command launches a long display with file names that are getting extracted. This is normal and simply indicates the unpacking process is underway. 

Perform the Installation

Next, we will rename the extracted folder tp “nodeext” with the following command:

mv node-v14.15.1-linux-x64 nodeext

Following that, install the Node and NPM binaries:

mkdir ~/bin

cp nodeext/bin/node ~/bin

cd ~/bin

ln -s ../nodeext/lib/node_modules/npm/bin/npm-cli.js npm

Running Node.js Apps

After having successfully installed Node.js in your server, next in mind would be if you want to use them with an actual application. Since there are multiple possible configurations for Node.js Apps, it only makes sense there are various ways to run them.

Assuming you already have a Node.js application that is production-ready and has a package.json file you can start the deployment: 

nohup npm start –production &

If you need to run a Node.js app that doesn’t have a package.json file included, use the following command instead:

nohup node my_app.js &

Note that if you choose to run an app that does not have an included package.json you won’t be able to manage it with NPM.

How to Stop an Application

To terminate a running application, we need to kill the process. Luckily, there is an easy command to stop any Node.js processes on the server:

pkill node

Connect your Website with a Running Node.js App

Because of the various combinations of technologies а VPS can handle, there are many ways to connect your website to a Node.js app. Since Apache is a common web server used, we will use this as the showcase platform. 

What we’ll be looking to do is to utilize the .htaccess file to perform the connection between the website and the Node.js app. There are a few ways you can access the .htaccess file. The easiest method is by making use of your cPanel or SPanel interface to edit the file directly. 

Add the following code to your .htaccess file:

DirectoryIndex disabled

RewriteEngine On

RewriteRule ^$ http://127.0.0.1:XXX/ [P,L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ http://127.0.0.1:XXX/$1 [P,L]

Replace “XXX” with the port number of your Node.js application. Once done, remember to save the changes to your .htaccess file before exiting the editor.

Conclusion

Aside from the obvious benefits Node.js brings to the table, it communicates JavaScript to the back-end. This allows full-stack JavaScript applications to run so it’s a must-have on any VPS server. Despite the seemingly technical nature of the installation, the process isn’t overly complex if you know the right commands.

Frequently Asked Questions

Must I pay to use Node.js?

Node.js is open-source and is free for use. Having said this, if you develop your Node.js project within a proprietary integrated development environment (IDE), there will likely be a fee involved as it is a commercial product. 

What is NPM?

NPM is short for Node Package Manager. It serves as a repository for JavaScript packages that developers incorporate into their projects. It also gives web devs the capability to manage a specific version of each package.

Can I use Node.js with shared hosting?

In some circumstances, it may be possible to use Node.js with shared hosting. This will largely be dependent on the hosting provider. Still, VPS hosting is a much better choice if you’re looking to maximize your performance and security. 

What database can be used with Node.js?

You can use any type of database with Node.js without any issues. Just make sure the database package is equipped with the necessary drivers to allow Node.js to interact with it. One good example would be MongoDB as it is designed to work well with data in JSON format specifically.

Timothy

Author

Timothy Shim is a writer, editor, and tech geek. Starting his career in the field of IT, he today spends his time building effective web content. His specializations are in web hosting and related tools, Virtual Private Networks, along with business applications of web technology.

Write a Comment

Required*