Linux install Nodejs and implement reverse proxy with Nginx _linux

Source: Internet
Author: User

This paper introduces the Linux installation Nodejs and implements the reverse proxy with Nginx, as follows:

What is Nodejs?

Node.js is a JavaScript runtime environment (runtime). It actually encapsulates the Google V8 engine. The V8 engine executes JavaScript very quickly and performs very well.

Node.js is optimized for special use cases, providing an alternative API that makes V8 run better in a browser-less environment.

Local Install (OS X)

Version selection

    • V4.4.4, long-term support version, mature and reliable
    • V6.2.0 stable version, latest features

I'm still inclined to use the latest version here.

Download installation package

Https://nodejs.org/dist/v6.2.0/node-v6.2.0.pkg

Double-click the Install installation package

The next step, the installation is complete.

Simple execution

Node-v

v6.2.0

Run locally (OS X)

Create a demo file

Const HTTP = require (' http ');
const hostname = ' 127.0.0.1 ';
Const PORT = 3000;

Const SERVER = Http.createserver ((req, res) => {
 Res.statuscode =;
 Res.setheader (' Content-type ', ' text/plain ');
 Res.end (' Hello world\n ');
};

Server.listen (port, hostname, () => {
 console.log (' Server running at http://${hostname}:${port}/');
});

Write to File Example.js

Execution file

Node Example.js

At this point, the command line output server running at Http://127.0.0.1:3000/

At the same time in the browser input http://127.0.0.1:3000/, page output Hello World

Close the terminal and the page is no longer available.

Express Framework

We use the Express framework to build a demo of the website project.

NPM Install Express

Node_modules1

Creating Demo.js Files

var express = require (' Express ');
App = Express (); 
App.use (express.static (__dirname + '/public ')); 
App.listen (8081)

Create a public folder in a sibling folder with a static file 1.jpg

Enter yun_qi_img/1.jpg in Browser

View Response headers,x-powered-by:express

Server Installation (CentOS 7)

Installing node

Curl--silent--location Https://rpm.nodesource.com/setup | Bash-
yum-y install Nodejs
Yum install NPM

About the version of node

My last step through node installed version number is v0.10.42, the first thought wrong, after the data found that node has maintained a total of 4 versions

    • v0.10.42 (LTS)
    • v0.12.10 (LTS)
    • 4.4.5 LTS
    • 6.2

Oh clatter, really messy.

Write Demo Example

This part of the process is consistent with the above.

Install Forever and run

NPM Install forever-g

Forever start App.js

Configure Nginx

cd/usr/local/nginx/conf/vhost/

VI demonode.coderfix.cn.conf
server {
listen;
server_name demonode.coderfix.cn;
  Location/{
  proxy_pass http://127.0.0.1:8899;
  }
}

Nginx resolution domain name, forwarding to the local Nodejs 8899 port ~

Configure domain name resolution and access

http://demonode.coderfix.cn/

This completes the deployment of Nodejs and Nginx.

Problems that may arise

The Nodejs service causes the error to be more open

events.js:72
  throw er;//unhandled ' error ' event
     ^
error:listen eaddrinuse at
  errnoexception (net.js : 884:11) at
  server._listen2 (net.js:1022:14) in Listen (Net.js:1044:10) at
  Server.listen (net.js:1110:5) At
  object.<anonymous> (foldername/app.js:33:24) at
  module._compile (module.js:456:26)
  at Object.module._extensions.. JS (module.js:474:10) at
  module.load (module.js:356:32) at
  function.module._load (module.js:312:12)
  At Function.Module.runMain (module.js:497:10) 1

Turn off the process before you start, and then it will be all right.

PS aux | grep node

kill-9 * * *

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.