NodeJS

From Gentoo Wiki
Jump to: navigation, search
Warning
This is a work in progress!

About Installation

This guide will walk through installing Node.js behind nginx and using Monit to keep Node instances alive. Since Node.js is a single-process application, we will launch multiple instances of the application and load balance using nginx.

Installation

USE flags

USE flags for net-libs/nodejs A JavaScript runtime built on Chrome's V8 JavaScript engine

inspector Enable V8 inspector
npm Enable NPM package manager
snapshot Enable snapshot creation for faster startup
systemtap Enable SystemTAP/DTrace tracing

Emerge

root #emerge --ask monit nginx nodejs

npm

Node.js has a USE flag to include npm, the Node.js package manager. You will need it to install a Node.js application's dependencies, which are defined in a file named package.json.

The current stable version of Node.js, 6.11.x, comes with npm 3.10.x.

If you need a newer version like npm 5.6.x, use Node.js 8.9.x (the next LTS release) or later. You can configure which version to pull in by setting keywords, e.g.:

FILE /etc/portage/package.keywords/nodejs
~net-libs/nodejs-8.9.4

Also see Knowledge_Base:Accepting_a_keyword_for_a_single_package.

Configure Monit

FILE /etc/monit.d/my-appAuto restart NodeJS App
check process mysql with pidfile /var/run/my-app/mysqld.pid
    start program = "/bin/bash -c 'rc-service mysql start'"
    stop program  = "/bin/bash -c 'rc-service mysql stop'"

Configure Nginx

FILE /etc/nginx/nginx.confNginx Config
http {
    upstream myapp1 {
        least_conn;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}