This article covers a version of CentOS that is no longer supported. If you are currently operating a server running CentOS 6, we highly recommend upgrading or migrating to a supported version of CentOS.
Reason: CentOS 6 reached end of life (EOL) on November 30th, 2020 and no longer receives security patches or updates. For this reason, this guide is no longer maintained.
See Instead:
This guide might still be useful as a reference, but may not work on other CentOS releases. If available, we strongly recommend using a guide written for the version of CentOS you are using.
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.
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
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.
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 express-generator 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.
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).
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 ./bin/www
Now we're able to access our first express app at your VPS IP. For example http://123.456.78.90:3000/.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Getting a little stale. Please update your docs to latest Node LTS or generalize for latest stable for timelessness. https://github.com/creationix/nvm#install-script
Could not get this tutorial to work out on a newly provisioned centos 7.3 VPS installing nodejs v7.5.0. Fails during the “make” phase with
How to install node.js 5.x on centos 6.x
Please refer http://itblog.mobi/2015/12/26/how-to-install-nodejs-5-3-on-centos-6-6/
Hi, thanks for this tuto. I have an issue at the end. After the following command line (the last one): “supervisor ./bin/www”, the following error is displayed: "“Starting child process with 'node ./bin/www Port 3000 is already in use Program node ./bin/www exited with code 1"”
And my Apache server does not start anymore (impossible to start it), all my websites are blocked. Can you help?
thanks
To keep a node server up and running, Forever may be a better choice: http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/
Great tutorial, thanks!
Am I the only one getting error at “express hello” step? I created a user as in the tutorial, then switched to that user, entered express hello and get this error:
install dependencies: $ cd hello && npm install
run the app: $ DEBUG=hello ./bin/www
/usr/local/lib/node_modules/express-generator/bin/express:259 if (err) throw err; ^ Error: EACCES, mkdir ‘/usr/src/node-v0.10.4/hello’
Edit: My mistake, I was trying to create the directory at the installation path “/usr/src/node-v0.10.4/”. I changed to /home/myuser/ and typed express command and voilà. Thanks for the tutorial by the way.
Hi! These instructions are unfortunately a bit out of date, as they recommend installing node 0.10.4 when the latest is now 0.10.35. Any chance you might be able to update them?
Also, the nodesource team have put together a repository with precompiled binaries for both Debian and Redhat based systems – that might be a simpler approach than compiling from source:
https://github.com/nodesource/distributions#usage-instructions-1
Excellent! Everything worked for me!
@maildost: Are you in
/usr/src/node-v0.10.4
? Try runningcd /usr/src/node-v0.10.4
first and then./configure
.If that doesn’t work, run
chmod +x /usr/src/node-v0.10.4/configure
and try again.