;

How to install NodeJS and run node applications on CentOS 7

Try it in our public cloud & Get $5 Credit
CLAIM NOW

How to install NodeJS and run node applications on CentOS 7

NodeJS is a server runtime for JavaScript. Many newer apps and services find that the asynchronous, single-process approach espoused by Node is easier to reason about and scale. While PHP still remains a popular choice for distributing commodity web apps, Node is becoming a big contender for uses more complicated than the average blog or wiki engine. Here we’ll get Node up and running on CentOS 7. We’ll also set up a simple app to demonstrate how you’d go about running any other Node-based code.

Getting Started

Confirm that you have the following before you follow this guide:
• 1 Node (Cloud Server or Dedicated Server) running CentOS 7.
• Root access to the node.

You can run Node-based applications as root, but any compromise will give an attacker the ability to run code and perform actions that affect the entire server. For best results, each Node-based app should run under a dedicated user account.

Tutorial

Start by updating your server to ensure that it has all available security patches and bugfixes. Perform the update step regularly to keep your system secure and performant. We’ll also install packages that Node expects your server to have.

yum update
yum install gcc-c++ make

There are a few ways you might install the Node runtime. The first method involves using a Yum repository.

There are a few different versions of Node. Which you install will depend upon the requirements of the code you are attempting to run, or in some instances, on your environment’s ability to upgrade and run new versions of Node.

If you need stability above all else, and can’t easily upgrade components, stick with the Node Long-Term Support (LTS) release. This is currently V4.x. Here is how you’d enable and install Node’s LTS release via Yum:

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

Node V5 is a more recent version with additional features and APIs. Here is how Node V5 is installed via Yum:

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

Node V6 will eventually become the new LTS release. V6’s biggest feature is significant compatibility with Ecmascript 2015, new functionality that vastly improves and simplifies the JavaScript language.

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

Finally, V0.10 is an older version of Node required by some older apps and frameworks. In general, don’t use this repository unless you know that you need it.

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

If all of this seems complicated, you can just install the latest stable version of Node as shipped by CentOS. If you don’t know which version to use, try this method to get a Node runtime that will almost certainly work for you.

yum install nodejs

Another way to install Node is via the Node Version Manager. NVM can install several versions of Node concurrently, switching between them as required. Use this method only if you are familiar with Node, or if you are comfortable administering servers and installing language runtimes without package repositories.

Start by installing NVM’s dependencies.

yum groupinstall 'Development Tools'
yum install wget

Next you’ll need the NVM installation script available on GitHub:

cd /root
wget https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh
bash install.sh

When you’ve finished the installation, you’ll need to log out and in again so the modifications it made to your startup shell scripts take effect.

Use NVM to list all available versions of NodeJS, then select the one you wish to install.

nvm ls-remote

You’ll see many versions. At the time of writing, the latest version of Node is 6.2.2. Use this version, or whatever the latest version NVM lists, unless you specifically need an earlier release.

(....)
v4.4.2
v4.4.3
v4.4.4
v4.4.5
v4.4.6
v4.4.7
v5.0.0
v5.1.0
v5.1.1
v5.2.0
v5.3.0
v5.4.0
v5.4.1
v5.5.0
v5.6.0
v5.7.0
v5.7.1
v5.8.0
v5.9.0
v5.9.1
v5.10.0
v5.10.1
v5.11.0
v5.11.1
v5.12.0
v6.0.0
v6.1.0
v6.2.0
v6.2.1
v6.2.2

Use this command to install the version you choose to run:

nvm install 6.2.2

To ensure that the installation worked, let’s have NVM list the Node versions it has installed. You should see the version you chose earlier.

nvm ls
-> v6.2.2
default -> 6.2.2 (-> v6.2.2)
node -> stable (-> v6.2.2) (default)
stable -> 6.2 (-> v6.2.2) (default)
iojs -> N/A (default)

You’ve installed a version of Node, but since NVM can install several versions at the same time, you must pick which is currently active. Here we’ll activate the release you’ve just downloaded.

nvm use 6.2.2

Now we verify that the activation worked.

root@nodejs:~# node -v
v6.2.2

Node is now installed. To ensure that it works, we’ll deploy a simple web server that displays a message when accessed. The application will use the popular Express web framework.

First, create a directory that will contain the application.

mkdir /home/nodeapp
cd /home/nodeapp

Next, create the script that will run the server.

nano nodetest.js

The script should contain this code:

!/usr/bin/env node
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Globo.Tech says Hello!');
});
app.listen(6915, function () {
console.log('Basic NodeJS app listening on port 6915.');
});

Save and exit. Next we need to install the Express framework so your script can use it.

npm install express

Run the script you’ve just created.

node /home/nodeapp/nodetest.js

You’ll see a message on the console confirming that the server is listening for incoming connections.

node nodetest.js
Basic NodeJS app listening on port 6915.

Given that we have setup an environment path in the beginning of our script, we can make it executable to run it without specifying "node" in front of the script.
chmod +x /rhome/nodeapp/nodetest.js
./nodetest.js

Visit the URL for the server you just created. If you see the message “Globo.Tech says Hello!” in your browser, your Node stack is correctly set up!

Conclusion

You can now run any number of modern Node apps on this server. Share this article with anyone who might be looking for a great platform on which to develop modern, internet-facing services.

If found this guide helpful, please be sure to share it with friends. You can also take a look at our other guides where we cover topics such as networking, databases, and server applications.