> CentOS > CentOS教程 >

How to install NodeJS on CentOS 7.x

Node.js is a Javascript platform built to help developers to create fast and rich Javascript applications. It allows users to build apps that require back-end functions using Javascript on the server side. On this tutorial I will show you how to install NodeJS on CentOS 7 server using two methods: compiling from source files, and using rpm files from EPEL repo.

Tutorial requirements:

  • NodeJS must run on a VPS or Dedicated Sever, I can recommend you this great providers where you can buy cheap hosting with very good technical support:
    • A Small Orange
    • A2 Hosting
  • Basic compiler tools can be installed using this command:
yum install gcc gcc-c++

Installing NodeJS on CentOS 7.x using source files

Get the Node files, extract, compile and install:

wget http://nodejs.org/dist/v0.10.31/node-v0.10.31.tar.gz
tar xvpzf node-v*
cd node-v*

Configure & Compile NodeJS:

./configure
make
make install

Check if NodeJS was installed properly:

node --version

It should show you the last version number.

Installing NodeJS using EPEL repo

An easy method for most users is to use EPEL repo (Extra Packages for Enterprise Linux) that allows CentOS and RHEL users to get the lastest and most fresh software that the stable versions doesn’t have.

Add the repository:

wget http://download.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm
rpm -Uvh  epel-release-6*.rpm

Now install NodeJS using yum:

yum install nodejs

Check if the installation was ok:

node --version

If you want access to the NPM console in order to install more NodeJS modules, you can do it by typing:

yum install npm

Now you are ready to start working with NodeJS and run your JS applications on the server side.

 

Testing Hello World on NodeJS

This script responds with the classic “Hello World” when you run it.

Let’s create it:

nano -w example.js

Paste this code inside:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

To run the new webserver you just created, you only need to call the example.js file with the node commmand, as you see here:

node example.js

It should output something like this:

Server running at http://127.0.0.1:1337/

If you need more information, you can read about NodeJS at http://nodejs.org/ 



(责任编辑:IT)