Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

AMS Node Meetup December presentation Phusion Passenger

1,109 views

Published on

Phusion Passenger is an app server for Node.js, Ruby and Python. It simplifies deployment and administration, increases your server's efficiency and helps identifying and solving problems.

In this talk Hongli Lai demonstrates how Passenger simplifies things by integrating with Nginx and by replacing Forever, PM2, Cluster and all sorts of other tools. Hongli also shares what other benefits Passenger has to offer, and what you can expect from future developments.

Published in: Software
  • Be the first to comment

  • Be the first to like this

AMS Node Meetup December presentation Phusion Passenger

  1. 1. Deploying with Phusion Passenger
  2. 2. Our Node.js app is done. How do we deploy to production? Can’t be that hard. Let’s ask Google.
  3. 3. Make sure your app keeps running (restart on crash) Step 1 forever  start  app.js
  4. 4. Start your app on system boot Step 2 systemd, SysV init, upstart, etc.
  5. 5. #!/bin/bash   DIR=/var/www/YOUR_NODE_APP_GOES_HERE   PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin   NODE_PATH=/usr/local/lib/node_modules   NODE=/usr/local/bin/node       test  -­‐x  $NODE  ||  exit  0       function  start_app  {      NODE_ENV=production  nohup  "$NODE"  "$DIR/YOUR_APP_SERVER_FILE.js"  1>>  "$DIR/logs/YOUR_APP_NAME.log"   2>&1  &      echo  $!  >  "$DIR/pids/YOUR_APP.pid"   }       function  stop_app  {      kill  `cat  $DIR/pids/YOUR_APP.pid`   }       case  $1  in        start)              start_app  ;;          stop)              stop_app  ;;          restart)              stop_app              start_app              ;;          *)              echo  "usage:  YOUR_APP_NAME  {start|stop}"  ;;   esac   exit  0
  6. 6. Reverse proxy from Nginx! Step 3 • Serve files faster and better • Improves security: better HTTP sanitizer! • Easier SSL configuration
  7. 7. server  {          listen  0.0.0.0:80;          server_name  yourdomain.com  yourdomain;          root  /yourapp/public;   }
  8. 8. server  {          listen  0.0.0.0:80;          server_name  yourdomain.com  yourdomain;          root  /yourapp/public;          location  ~*  .(css|js|gif|jpe?g|png)$  {                  add_header  Pragma  public;                  add_header  Cache-­‐Control  "public,  must-­‐revalidate,  proxy-­‐revalidate";          }   }
  9. 9. upstream  app_yourdomain  {          server  127.0.0.1:3000;   }   server  {          listen  0.0.0.0:80;          server_name  yourdomain.com  yourdomain;          root  /yourapp/public;          location  ~*  .(css|js|gif|jpe?g|png)$  {                  add_header  Pragma  public;                  add_header  Cache-­‐Control  "public,  must-­‐revalidate,  proxy-­‐revalidate";          }          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;              proxy_pass  http://app_yourdomain/;              proxy_redirect  off;          }   }
  10. 10. Multi-core! Step 4 Cluster module
  11. 11. var  cluster  =  require('cluster');   var  http  =  require('http');   var  numCPUs  =  require('os').cpus().length;   if  (cluster.isMaster)  {      //  Fork  workers.      for  (var  i  =  0;  i  <  numCPUs;  i++)  {          cluster.fork();      }      cluster.on('exit',  function(worker,  code,  signal)  {          console.log('worker  '  +  worker.process.pid  +  '  died');      });   }  else  {      //  Workers  can  share  any  TCP  connection      //  In  this  case  its  a  HTTP  server      http.createServer(function(req,  res)  {          res.writeHead(200);          res.end("hello  worldn");      }).listen(8000);   }
  12. 12. How can we see application statistics? Uhhh.... sign up with proprietary monitoring services?
  13. 13. Configure forever Configure init Configure Nginx Use Cluster module Sign up for proprietary monitoring service
  14. 14. Node.js ≠ boilerplate
  15. 15. Passenger: rethinking the workflow
  16. 16. server  {          listen  0.0.0.0:80;          server_name  yourdomain.com;          root  /yourapp/public;   }
  17. 17. server  {          listen  0.0.0.0:80;          server_name  yourdomain.com;          root  /yourapp/public;          deploy_my_damn_app  on;   }
  18. 18. server  {          listen  0.0.0.0:80;          server_name  yourdomain.com;          root  /yourapp/public;          deploy_my_damn_app  on;   } ✓ Restarting on crash ✓ Start during system boot ✓ Nginx reverse proxy ✓ Multi-core
  19. 19. server  {          listen  0.0.0.0:80;          server_name  yourdomain.com;          root  /yourapp/public;          deploy_my_damn_app  on;   } ✓ Restarting on crash ✓ Start during system boot ✓ Nginx reverse proxy ✓ Multi-core bash$  show-­‐my-­‐app-­‐status
  20. 20. server  {          listen  0.0.0.0:80;          server_name  yourdomain.com;          root  /yourapp/public;          deploy_my_damn_app  on;   } ✓ Restarting on crash ✓ Start during system boot ✓ Nginx reverse proxy ✓ Multi-core bash$  show-­‐my-­‐app-­‐status        passenger_enabled  on; bash$  passenger-­‐status
  21. 21. #  passenger-­‐status   -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  General  information  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐   Max  pool  size  :  9   Processes          :  3   Requests  in  top-­‐level  queue  :  0   -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  Application  groups  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐   /u/apps/bubbleconf/current#default:      App  root:  /u/apps/bubbleconf/current      Requests  in  queue:  0      *  PID:  25408      Sessions:  0              Processed:  4              Uptime:  4h  17m  2s          CPU:  0%            Memory    :  95M          Last  used:  34m  30s  a   /u/apps/passenger_website/current#default:      App  root:  /u/apps/passenger_website/current      Requests  in  queue:  0      *  PID:  20696      Sessions:  0              Processed:  27291      Uptime:  6h  10m  0s          CPU:  8%            Memory    :  155M        Last  used:  0s  ago
  22. 22. #  passenger-­‐config  system-­‐metrics   -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  General  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐   Kernel  version        :  3.14.1-­‐x86_64-­‐linode39   Uptime                        :  225d  15h  52m  20s   Load  averages          :  0.10%,  0.10%,  0.13%   Fork  rate                  :  0   -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  CPU  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐   Number  of  CPUs        :        4   Average  CPU  usage  :    14%    -­‐-­‐    10%  user,      0%  nice,      4%  system,    86%  idle      CPU  1                      :      0%    -­‐-­‐      0%  user,      0%  nice,      0%  system,  100%  idle      CPU  2                      :    40%    -­‐-­‐    40%  user,      0%  nice,      0%  system,    60%  idle   I/O  pressure            :      0%      CPU  1                      :      0%      CPU  2                      :      0%   Interference  from  other  VMs:      0%      CPU  1                                        :      0%      CPU  2                                        :      0%   -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  Memory  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐   RAM  total                  :      6008  MB   RAM  used                    :      1940  MB  (32%)   RAM  free                    :      4068  MB   Swap  total                :      1711  MB   Swap  used                  :        172  MB  (10%)   Swap  free                  :      1539  MB
  23. 23. Nginx Forever/PM2 Cluster Init script Monitoring software
  24. 24. Lets you focus on what is really important
  25. 25. Keeps you out of the dark
  26. 26. Identify and solve problems
  27. 27. How it works Easy to migrate to?
 Easy to setup?
  28. 28. $  brew  install  passenger   $  brew  install  nginx  —with-­‐passenger   …follow  instructions… OS X …add  our  APT  repo…   $  sudo  apt-­‐get  install  passenger  nginx-­‐extras Debian/Ubuntu
  29. 29. $  node  app.js   $  passenger  start        —app-­‐type  node  —startup-­‐file  app.js Standalone mode
  30. 30. Nginx mode server  {          listen  0.0.0.0:80;          server_name  yourdomain.com;          root  /yourapp/public;          passenger_enabled  on;   }
  31. 31. Is it any good?
 
 Is it stable?
 
 Who uses it?
  32. 32. 6 years
  33. 33. • Host multiple apps at once • Improve security with privilege separation • Dynamic worker management • etc… Many more features not mentioned
  34. 34. • Zero-downtime rolling restarts • Error resistant deploys • Mass deployment • Resource control • Unstuck stuck apps • Memory limiting • Priority support Paid offering
  35. 35. The future
  36. 36. Easily setup dev environment with one command • Repeatable • Shareable
  37. 37. Easily setup and push to production with one command • Repeatable • Shareable
  38. 38. Debugging
  39. 39. www.phusionpassenger.com

×