April 15, 2013

Beginner

How To Install And Run A Node.js App On Centos 6.4 64bit

Tagged In: Cent Os, Node.Js

Introduction


This article outlines the steps necessary to run a "Hello world" in node.js + express, running on a 64bit Centos 6.4 installation. We will be building the latest version of source (at this moment, v0.10.4) from the upstream provider.

As is written on their homepage, Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. This is a fast, event driven platform and server-side Javascript engine used for building web applications. DigitalOceans' droplets are a cost-effective way to install and start studying server-side Javascript and bulding or deploying web applications with Node.js.

Setup a VPS


To get started, we will need a droplet - the smallest instance will do just enough - and a SSH client (ie. Putty on Windows, Linux systems and Mac Os X usually have it out of the box).

When we receive our initial root password, we can ssh into the instance. SSH into the VPS and change the root password, if you haven't already.

It would probably be a good idea to also update software repository to the latest versions:
yum -y update

This will update installed software on our VPS to the latest versions.

Yum can take a few minutes, but when it's done, we need to prepare for software installation. We're going to build Node.js from the latest source available at the moment of writing this (v0.10.4). To do that, we'll need "Development Tools". It's a group of tools for compiling software from sources.
yum -y groupinstall "Development Tools"

This command will pull a "Development Tools" group with the applications needed to compile node.js.

Also, we'll install GNU screen - a piece of software that allows us to connect to our VPS, start a session and detach from it. We could disconnect and connect later, or from another workstation, and pick up where we left. It's very handy, especially during development of an app, when we want to learn stuff.
yum -y install screen

Node.js installation


Now that we're ready to install Node.js from sources. First, we'll move to /usr/src directory - the usual place to hold software sources.
cd /usr/src

Now, we pick the latest compressed source archive from Node.js website at http://nodejs.org/download/.
wget http://nodejs.org/dist/v0.10.4/node-v0.10.4.tar.gz

We could and should replace the url and use the more recent version of node.js, if there is one. Next, we are uncompressing the source files and move into that directory.
tar zxf node-v0.10.4.tar.gz
cd node-v0.10.4

Now the source for Node.js is extracted and we're in the source directory. We can now prepare our compiler commands, by executing the configure script:
./configure

It will read the properties of our system to prepare compiler flags. Ie. it could be a system architecture (32/64bit, CPU specific flags etc). With it, we're ready to actually compile the source now. To do that, just type:
make

This is probably the most time-consuming task here: on my example VPS it took about 6 minutes and 34 seconds to complete. When we're done, we need to make this available system-wide:
make install

The latest command will place the compiled binaries in system path, so all users could use it without any further setup. By default, node binary should be installed in /usr/local/bin/node.

Install Express.js


We now have Node.js installed and complete, we can start developing right away, deploy an already done application or we can proceed to create our Express.js application. First, we'll use npm, nodes' module manager, to install express middleware and supervisor - a helpful module that keeps our app started, monitors for file changes (ie. when we're developing the app) and restarts the VPS when needed.
UPDATE: To be able to run an executable in /usr/local/bin through sudo, you have add /usr/local/bin to your secure_path using visudo.
sudo visudo
Look for secure_path, and append the following to it: ":/usr/local/bin". Once you have done that, you're now ready to install the express and supervisor modules.
npm -g install express supervisor

npm -g install will install the express and supervisor modules from npm software repository and make it available to the whole system. The -g switch in this command means "global" - the express and supervisor commands will be available accross the whole system.

Add non-privileged user


You should now, for security reasons, create a regular system user and run node under non-privileged account. To do this, add the user first. You can replace "exampleuser" with whatever name your prefer.
useradd exampleuser

We have a new system user. Add a decent password for the new user:
passwd exampleuser

Log out, and log back in as the new user.

This changes our login shell from root (system user) to exampleuser (non-privileged user who can compromise the system with less damage).

Creating an express app


Express is powerfull framework, and to create our first application, all we have to do is type:
express hello

The command will create a "hello" directory and setup some basics for a new application. Now we should enter this directory and install express dependencies:
cd hello && npm install

npm install part of the command will read all the module dependencies from a generated package.json file and install it from npm software repository. We should start a new screen session so we can leave node app running:
screen

Finally, we can start our application with the help of supervisor that we installed earlier.
supervisor app

Now we're able to access our first express app at your VPS IP. For example http://123.456.78.90:3000/.

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

22 Comments

Write Tutorial
  • Gravatar fc23229 12 months

    After the make install step, when I run npm it said npm:command not found. Do I have to start node somehow before that?

  • Gravatar karl 11 months

    To those having problems with 'command not found' when executing 'sudo npm -g install express supervisor': After half an hour of troubleshooting with guys on #node.js (thanks!) we managed to identify the source of the problem. http://www.elroyjetson.org/dev-notes/centos/install-node-js-on-centos-6-2 So basically you need to add: /usr/local/bin to your secure_path using visudo. > sudo visudo Add to secure path: /usr/local/bin > sudo npm -g install express supervisor should work now.

  • Gravatar Kamal Nasser 11 months

    Thanks @karl, I have updated the article.

  • Gravatar phacer 9 months

    Thanks for a great tutorial. One problem though, after starting the app using "supervisor app", and if I closed that terminal window, how to I stop the process without restarting my server?

  • Gravatar Kamal Nasser 9 months

    @phacer you can run "pgrep supervisor" to get the PID of the process, and kill it by running "kill PID" (replacing PID with the actual PID).

  • Gravatar aerosmythe 9 months

    the nodejs wiki recommends installing through epel: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager#rhelcentosscientific-linux-6 > Node.js and npm are available from the Fedora Extra Packages for Enterprise Linux (EPEL) repository. If you haven't already done so, first enable EPEL and then run the following command to install node and npm: su -c 'yum install npm'

  • Gravatar viper-s 9 months

    I prefer "forever" instead screen. npm -g install forever

  • Gravatar rparker 8 months

    I cant run screen as example user on centos 6. I get cannot open terminal '/dev/pts/0'

  • Gravatar rparker 8 months

    @viper-s how would you run the app with forever?

  • Gravatar Kamal Nasser 8 months

    @rparker: Did you ssh into the server as the user or did you run 'su user' as root? You shouldn't run screen as root anyway -- create a user for the app: https://www.digitalocean.com/community/articles/initial-server-setup-with-centos-6

  • Gravatar tonybrown67 8 months

    will this work on an ubuntu server?

  • Gravatar Kamal Nasser 8 months

    @tonybrown67: No, we have a separate article for ubuntu: https://www.digitalocean.com/community/articles/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps

  • Gravatar tonybrown67 8 months

    @Kamal, thanks I'll look into that one than and thanks again

  • Gravatar tonybrown67 8 months

    @Kamal, I have node setup and running, my problem is getting my app to run without going to www.dnsname:3000 and keeping my ssh open all day lol. I was looking for a detailed tut on deploying my app to an ubuntu server, I have a node/express app that I want to deploy

  • Gravatar Kamal Nasser 8 months

    @tonybrown67: Install 'screen':

    sudo apt-get install screen
    SSH in as your nodejs user, run
    screen
    and then run
    node yourapp.js
    . Press Ctrl+A, D to detach screen. Log out as usual and nodejs should still be running. You can return to your previous screen session by running
    screen -r

  • Gravatar tonybrown67 8 months

    thanks @Kamal, I appreciate the help

  • Gravatar Corey Butler 8 months

    I wouldn't advise using screen for long running processes of any kind (not just node). Check node-linux at https://github.com/coreybutler/node-linux (FD: I'm the author). It will generate an init.d script allowing your node process to be managed by the OS (like other daemons). For example, `service my-node-app start|stop|restart`.

  • Gravatar johnfraize 5 months

    What a very helpful page. Thanks

  • Gravatar mandarmbhagwat 2 months

    I followed this page and all went well till "npm -g install express supervisor". It gave me below error : [root@bus000356 node-v0.10.4]# npm -g install express supervisor npm http GET https://registry.npmjs.org/express npm http GET https://registry.npmjs.org/supervisor npm http GET https://registry.npmjs.org/express npm http GET https://registry.npmjs.org/supervisor npm http GET https://registry.npmjs.org/supervisor npm http GET https://registry.npmjs.org/express npm ERR! Error: CERT_UNTRUSTED npm ERR! at SecurePair. (tls.js:1283:32) npm ERR! at SecurePair.EventEmitter.emit (events.js:92:17) npm ERR! at SecurePair.maybeInitFinished (tls.js:896:10) npm ERR! at CleartextStream.read [as _read] (tls.js:430:15) npm ERR! at CleartextStream.Readable.read (_stream_readable.js:294:10) npm ERR! at EncryptedStream.write [as _write] (tls.js:344:25) npm ERR! at doWrite (_stream_writable.js:211:10) npm ERR! at writeOrBuffer (_stream_writable.js:201:5) npm ERR! at EncryptedStream.Writable.write (_stream_writable.js:172:11) npm ERR! at write (_stream_readable.js:547:24) npm ERR! If you need help, you may report this log at: npm ERR! npm ERR! or email it to: npm ERR! npm ERR! System Linux 2.6.32-431.3.1.el6.i686 npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "-g" "install" "expr ess" "supervisor" npm ERR! cwd /usr/src/node-v0.10.4 npm ERR! node -v v0.10.4 npm ERR! npm -v 1.2.18 npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /usr/src/node-v0.10.4/npm-debug.log npm ERR! not ok code 0 [root@bus000356 node-v0.10.4]# sudo visudo [root@bus000356 node-v0.10.4]# npm -g install express supervisor npm http GET https://registry.npmjs.org/express npm http GET https://registry.npmjs.org/supervisor npm http GET https://registry.npmjs.org/supervisor npm http GET https://registry.npmjs.org/express npm http GET https://registry.npmjs.org/supervisor npm http GET https://registry.npmjs.org/express npm ERR! Error: CERT_UNTRUSTED npm ERR! at SecurePair. (tls.js:1283:32) npm ERR! at SecurePair.EventEmitter.emit (events.js:92:17) npm ERR! at SecurePair.maybeInitFinished (tls.js:896:10) npm ERR! at CleartextStream.read [as _read] (tls.js:430:15) npm ERR! at CleartextStream.Readable.read (_stream_readable.js:294:10) npm ERR! at EncryptedStream.write [as _write] (tls.js:344:25) npm ERR! at doWrite (_stream_writable.js:211:10) npm ERR! at writeOrBuffer (_stream_writable.js:201:5) npm ERR! at EncryptedStream.Writable.write (_stream_writable.js:172:11) npm ERR! at write (_stream_readable.js:547:24) npm ERR! at flow (_stream_readable.js:556:7) npm ERR! at Socket.pipeOnReadable (_stream_readable.js:588:5) npm ERR! If you need help, you may report this log at: npm ERR! npm ERR! or email it to: npm ERR! npm ERR! System Linux 2.6.32-431.3.1.el6.i686 npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "-g" "install" "expr ess" "supervisor" npm ERR! cwd /usr/src/node-v0.10.4 npm ERR! node -v v0.10.4 npm ERR! npm -v 1.2.18 npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /usr/src/node-v0.10.4/npm-debug.log npm ERR! not ok code 0

  • Gravatar Kamal Nasser 2 months

    @mandarmbhagwat: That looks like an issue on npm's end. IIRC they've been having problems lately, try again and see if it works now.

  • Gravatar contact about 1 month

    When I tried to do this on a freshly created CentOS droplet I got an error: "wget: Command not found" So you should also do a "yum install wget" before trying to install node

  • Gravatar mdabydeen 7 days

    Got an error while build NodeJS g++ : Command not found. I am not sure if this wasn't a part of the "Development Tools", but can be resolved with: yum -y install gcc-c++

Leave a Comment

Create an account or login:
Ajax-loader