![Page 1: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/1.jpg)
Deploying Node.js With Forever and Nginx
“a no-downtime tale”
![Page 2: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/2.jpg)
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
![Page 3: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/3.jpg)
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 .
![Page 4: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/4.jpg)
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
![Page 5: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/5.jpg)
Forever.js setup and commands • It's an npm package.
o sudo npm install forever -g
• Main commands o start - Start SCRIPT as a daemon o stop - Stop the daemon SCRIPT o stopall - Stop all running forever scripts o restart - Restart the daemon SCRIPT o restartall - Restart all running forever scripts o list - List all running forever script
![Page 6: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/6.jpg)
Let's try it! • Identify the script to run
o Inspecting package.json is often a good idea o 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
![Page 7: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/7.jpg)
Ok, a bit more of Forever.js (featuring up and inspecting)
• How to keep the log o -l LOGFILE - Logs the forever output to LOGFILE o -o OUTFILE - Logs stdout from child script to OUTFILE o -e ERRFILE - Logs stderr from child script to ERRFILE
• Some other useful commands o -p PATH - Base path for all forever related files (pid files, etc.)
§ To make your line shorter o -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?)
![Page 8: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/8.jpg)
• 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, the cherry, "no downtime"
![Page 9: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/9.jpg)
Now we're all set, let's get serious • Nginx
o Awesome o Event-driven o Asynchronous o 10k-friendly
• Not quite hard to setup (ubuntu) o sudo apt-get install nginx
![Page 10: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/10.jpg)
Configure Nginx • Sites management
o Config files are best kept per-site o 1 file per site in
§ /etc/nginx/sites-available/ (standard) o To activate a site, make a symbolic link to
§ /etc/nginx/sites-enabled/ o Restart gracefully
§ sudo service nginx reload o Done.
![Page 11: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/11.jpg)
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 o upstream node_boxes {
server 127.0.0.1:3000; }
• Proxy Will be used as the relay
![Page 12: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/12.jpg)
Architecture • 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
Traffic
Node.js box Node.js box Node.js box Node.js box
![Page 13: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/13.jpg)
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; }
}
![Page 14: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/14.jpg)
Load balancing? 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;
} o sudo service nginx reload
![Page 15: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/15.jpg)
What's next? • WebSockets • Load balancing • What about the Databases/services? • Centralize your logs • Performance analysis • Cloud
![Page 16: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/16.jpg)
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
![Page 17: Nicolas-Embleton - Deploying node.js with forever and nginx](https://reader038.vdocument.in/reader038/viewer/2022100600/555e9df7d8b42a0d738b597c/html5/thumbnails/17.jpg)
About us Author: Nicolas Embleton Find me at: [email protected] 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