SlideShare a Scribd company logo
Deploying Node.js
With Forever and Nginx
“a no-downtime tale”
Node.js

•  Runs as a standalone server, listen its own
• 
• 

port
Just need to proxy the request to Node.js
and relay the result back
Installing is easy (let's try in MacOS/Ubuntu)
o 

sudo apt-get update
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
Sample server with Compound.js

•  sudo npm install compound -g
•  compound init todo-list-app
•  cd todo-list-app && npm install
How to start the project:
node .

• 
Making Node.js run "forever"

•  Using Forever.js
•  Forever.js is a "daemonized version" of your
• 

Node app.
Will check if the app is killed and re-spawn it
Forever.js setup and commands

•  It's an npm package.
o 

sudo npm install forever -g

o 
o 
o 
o 

start
- Start SCRIPT as a daemon
stop
- Stop the daemon SCRIPT
stopall - Stop all running forever scripts
restart - Restart the daemon SCRIPT

•  Main commands

o  restartall - Restart all running forever scripts
o  list
- List all running forever script
Let's try it!

•  Identify the script to run
o 
o 

Inspecting package.json is often a good idea
Example with compound.js
§  , "main": "server.js"

o 

Command: "node ." loads package.json, finds "main"
script and executes it.

•  Run on Forever:
o 

forever start /full-path-to-script/server.js
Ok, a bit more of Forever.js
(featuring up and inspecting)

•  How to keep the log
o 
o 

-l LOGFILE - Logs the forever output to LOGFILE
-o OUTFILE - Logs stdout from child script to OUTFILE

o 

-e ERRFILE - Logs stderr from child script to ERRFILE

o 

-p PATH

•  Some other useful commands
§ 
o 

- Base path for all forever related files (pid files, etc.)

To make your line shorter

-c COMMAND - COMMAND to execute (defaults to node)
§ 

For when you have "more" than just "node" to run (server init params for example)

•  List running processes
o 

forever list (easy, right?)
Now, the cherry, "no downtime"

•  forever restart /full-path-to-script/script
•  If you're not sure about the script path, just
do "forever list" and take it up from there
Now we're all set, let's get serious

•  Nginx
o 

Awesome
Event-driven
Asynchronous
10k-friendly

o 

sudo apt-get install nginx

o 
o 
o 

•  Not quite hard to setup (ubuntu)
Configure Nginx

•  Sites management
o 
o 
o 
o 
o 

Config files are best kept per-site
1 file per site in
§  /etc/nginx/sites-available/ (standard)
To activate a site, make a symbolic link to
§  /etc/nginx/sites-enabled/
Restart gracefully
§  sudo service nginx reload
Done.
Let's configure our proxy

•  cd /etc/nginx/sites-available/
•  sudo view compoundapp.com
o 

(name it based on your FQDN as a best practice)

•  Create a proxy block

upstream node_boxes {
server 127.0.0.1:3000;
}
Proxy Will be used as the relay
o 

• 
Architecture
• 
• 
• 

Traffic

Traffic goes through
the load balancer
nginx redirects to
the "upstream"
node_box
Want to scale? Just
add more boxes
and update nginx
config.

nginx load balancer

Node.js box

Node.js box

Node.js box

Node.js box
Configs: Load balancer
server {
# server_name yourdomain.com yourdomain; # set the domain name to bind to
access_log /var/log/nginx/compountapp.com_access.log; error_log /var/log/nginx/
compountapp.com_error.log;
# reroute all and pass the correct headers
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# then proxy the request
proxy_pass http://node_boxes/;
proxy_redirect off;
}
}
Load balancing?
o 

o 

upstream node_boxes {
server 50.20.14.8:3000;
server 50.20.14.9:3001;
server 50.20.14.10:3002;
server 50.20.14.11:3000;
}
sudo service nginx reload
What's next?

•  WebSockets
•  Load balancing
•  What about the Databases/services?
•  Centralize your logs
•  Performance analysis
•  Cloud
Resources
• 

http://stackoverflow.com/a/5015178/519649
o 

• 
• 

Config node.js as a proxy

https://github.com/nodejitsu/forever
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
o 

Install Node.js via package manager
About us
Author: Nicolas Embleton
Find me at: nicolas.embleton@gmail.com
Presentation made for “Javascript Ho Chi Minh City” meetup group
You can find us at:
•  http://meetup.com/JavaScript-Ho-Chi-Minh-City/
•  https://www.facebook.com/JavaScriptHCMC
•  https://plus.google.com/communities/116105314977285194967

More Related Content

PPTX
uWSGI - Swiss army knife for your Python web apps
PDF
Backing up thousands of containers
PPTX
EuroPython 2014 - How we switched our 800+ projects from Apache to uWSGI
PPTX
Ansible intro
PDF
IT Automation with Ansible
PDF
PDF
Introduction to ansible
ODP
Continuous delivery of Windows micro services in the cloud
uWSGI - Swiss army knife for your Python web apps
Backing up thousands of containers
EuroPython 2014 - How we switched our 800+ projects from Apache to uWSGI
Ansible intro
IT Automation with Ansible
Introduction to ansible
Continuous delivery of Windows micro services in the cloud

What's hot (20)

PDF
Warsztaty ansible
PDF
Build and deployment
PDF
Using Nagios to monitor your WO systems
PDF
Multiple django applications on a single server with nginx
PDF
docker build with Ansible
PDF
Ansible
PDF
A quick intro to Ansible
PDF
Infrastructure = Code
PDF
Ansible, best practices
PPT
Learn basic ansible using docker
PDF
Depende, ¿de qué depende? - Plain Concepts Dev Day
PDF
Glauber Costa on OSv as NoSQL platform
PDF
The Secrets of The FullStack Ninja - Part A - Session I
PDF
OSv at Cassandra Summit
PDF
Install openstack
PDF
Salt Stack - Subhankar Sengupta
PDF
Odoo Performance Limits
PDF
OpenNebula, the foreman and CentOS play nice, too
PPTX
PDF
#OktoCampus - Workshop : An introduction to Ansible
Warsztaty ansible
Build and deployment
Using Nagios to monitor your WO systems
Multiple django applications on a single server with nginx
docker build with Ansible
Ansible
A quick intro to Ansible
Infrastructure = Code
Ansible, best practices
Learn basic ansible using docker
Depende, ¿de qué depende? - Plain Concepts Dev Day
Glauber Costa on OSv as NoSQL platform
The Secrets of The FullStack Ninja - Part A - Session I
OSv at Cassandra Summit
Install openstack
Salt Stack - Subhankar Sengupta
Odoo Performance Limits
OpenNebula, the foreman and CentOS play nice, too
#OktoCampus - Workshop : An introduction to Ansible
Ad

Similar to Nicolas-Embleton - Deploying node.js with forever and nginx (20)

PDF
ITB2019 NGINX Overview and Technical Aspects - Kevin Jones
PDF
NGINX ADC: Basics and Best Practices
PDF
Running node.js as a service behind nginx/varnish
PDF
Running node.js as a service behind nginx/varnish
PDF
NGINX ADC: Basics and Best Practices – EMEA
PPTX
One click deployment
PDF
Nginx cheat sheet
PDF
How to deploy node to production
PDF
NGINX: Basics and Best Practices EMEA
PPTX
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
PDF
How to Get Started With NGINX
PDF
Introduction to Node.js
PDF
An Introduction to node.js
PPTX
NGINX: High Performance Load Balancing
PPTX
NGINX: Basics & Best Practices - EMEA Broadcast
PPTX
NGINX 101 - now with more Docker
PPTX
NGINX 101 - now with more Docker
PPTX
NGINX Installation and Tuning
PDF
Install nginx on ubuntu 21.04 server
PDF
AMS Node Meetup December presentation Phusion Passenger
ITB2019 NGINX Overview and Technical Aspects - Kevin Jones
NGINX ADC: Basics and Best Practices
Running node.js as a service behind nginx/varnish
Running node.js as a service behind nginx/varnish
NGINX ADC: Basics and Best Practices – EMEA
One click deployment
Nginx cheat sheet
How to deploy node to production
NGINX: Basics and Best Practices EMEA
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
How to Get Started With NGINX
Introduction to Node.js
An Introduction to node.js
NGINX: High Performance Load Balancing
NGINX: Basics & Best Practices - EMEA Broadcast
NGINX 101 - now with more Docker
NGINX 101 - now with more Docker
NGINX Installation and Tuning
Install nginx on ubuntu 21.04 server
AMS Node Meetup December presentation Phusion Passenger
Ad

More from JavaScript Meetup HCMC (8)

PDF
Writing testable js [by Ted Piotrowski]
PDF
Building workflow in Javascript: Build the awesome with Gulp.
PDF
[Js hcm] Java script- Testing the awesome
PDF
Knockout js (Dennis Haney)
PDF
Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, ...
PDF
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
PDF
Nicolas Embleton, Advanced Angular JS
PDF
Khanh-Nguyen - Gearman - distributed process solution
Writing testable js [by Ted Piotrowski]
Building workflow in Javascript: Build the awesome with Gulp.
[Js hcm] Java script- Testing the awesome
Knockout js (Dennis Haney)
Debugging JavaScript (by Thomas Bindzus, Founder, Vinagility & Thanh Loc Vo, ...
3D Web Programming [Thanh Loc Vo , CTO Epsilon Mobile ]
Nicolas Embleton, Advanced Angular JS
Khanh-Nguyen - Gearman - distributed process solution

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Modernizing your data center with Dell and AMD
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Network Security Unit 5.pdf for BCA BBA.
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Modernizing your data center with Dell and AMD
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Monthly Chronicles - July 2025
Digital-Transformation-Roadmap-for-Companies.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Advanced methodologies resolving dimensionality complications for autism neur...
Spectral efficient network and resource selection model in 5G networks
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
Reach Out and Touch Someone: Haptics and Empathic Computing
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Understanding_Digital_Forensics_Presentation.pptx

Nicolas-Embleton - Deploying node.js with forever and nginx

  • 1. Deploying Node.js With Forever and Nginx “a no-downtime tale”
  • 2. Node.js •  Runs as a standalone server, listen its own •  •  port Just need to proxy the request to Node.js and relay the result back Installing is easy (let's try in MacOS/Ubuntu) o  sudo apt-get update sudo apt-get install python-software-properties python g++ make sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs
  • 3. Sample server with Compound.js •  sudo npm install compound -g •  compound init todo-list-app •  cd todo-list-app && npm install How to start the project: node . • 
  • 4. Making Node.js run "forever" •  Using Forever.js •  Forever.js is a "daemonized version" of your •  Node app. Will check if the app is killed and re-spawn it
  • 5. Forever.js setup and commands •  It's an npm package. o  sudo npm install forever -g o  o  o  o  start - Start SCRIPT as a daemon stop - Stop the daemon SCRIPT stopall - Stop all running forever scripts restart - Restart the daemon SCRIPT •  Main commands o  restartall - Restart all running forever scripts o  list - List all running forever script
  • 6. Let's try it! •  Identify the script to run o  o  Inspecting package.json is often a good idea Example with compound.js §  , "main": "server.js" o  Command: "node ." loads package.json, finds "main" script and executes it. •  Run on Forever: o  forever start /full-path-to-script/server.js
  • 7. Ok, a bit more of Forever.js (featuring up and inspecting) •  How to keep the log o  o  -l LOGFILE - Logs the forever output to LOGFILE -o OUTFILE - Logs stdout from child script to OUTFILE o  -e ERRFILE - Logs stderr from child script to ERRFILE o  -p PATH •  Some other useful commands §  o  - Base path for all forever related files (pid files, etc.) To make your line shorter -c COMMAND - COMMAND to execute (defaults to node) §  For when you have "more" than just "node" to run (server init params for example) •  List running processes o  forever list (easy, right?)
  • 8. Now, the cherry, "no downtime" •  forever restart /full-path-to-script/script •  If you're not sure about the script path, just do "forever list" and take it up from there
  • 9. Now we're all set, let's get serious •  Nginx o  Awesome Event-driven Asynchronous 10k-friendly o  sudo apt-get install nginx o  o  o  •  Not quite hard to setup (ubuntu)
  • 10. Configure Nginx •  Sites management o  o  o  o  o  Config files are best kept per-site 1 file per site in §  /etc/nginx/sites-available/ (standard) To activate a site, make a symbolic link to §  /etc/nginx/sites-enabled/ Restart gracefully §  sudo service nginx reload Done.
  • 11. Let's configure our proxy •  cd /etc/nginx/sites-available/ •  sudo view compoundapp.com o  (name it based on your FQDN as a best practice) •  Create a proxy block upstream node_boxes { server 127.0.0.1:3000; } Proxy Will be used as the relay o  • 
  • 12. Architecture •  •  •  Traffic Traffic goes through the load balancer nginx redirects to the "upstream" node_box Want to scale? Just add more boxes and update nginx config. nginx load balancer Node.js box Node.js box Node.js box Node.js box
  • 13. Configs: Load balancer server { # server_name yourdomain.com yourdomain; # set the domain name to bind to access_log /var/log/nginx/compountapp.com_access.log; error_log /var/log/nginx/ compountapp.com_error.log; # reroute all and pass the correct headers location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; # then proxy the request proxy_pass http://node_boxes/; proxy_redirect off; } }
  • 14. Load balancing? o  o  upstream node_boxes { server 50.20.14.8:3000; server 50.20.14.9:3001; server 50.20.14.10:3002; server 50.20.14.11:3000; } sudo service nginx reload
  • 15. What's next? •  WebSockets •  Load balancing •  What about the Databases/services? •  Centralize your logs •  Performance analysis •  Cloud
  • 16. Resources •  http://stackoverflow.com/a/5015178/519649 o  •  •  Config node.js as a proxy https://github.com/nodejitsu/forever https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager o  Install Node.js via package manager
  • 17. About us Author: Nicolas Embleton Find me at: nicolas.embleton@gmail.com Presentation made for “Javascript Ho Chi Minh City” meetup group You can find us at: •  http://meetup.com/JavaScript-Ho-Chi-Minh-City/ •  https://www.facebook.com/JavaScriptHCMC •  https://plus.google.com/communities/116105314977285194967