Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NodeJS for Production

Kabir Baidhya
November 18, 2016
93

NodeJS for Production

Kabir Baidhya

November 18, 2016
Tweet

Transcript

  1. “If #nginx isn’t sitting in front of your node server,

    you’re probably doing it wrong.” —Bryan Hughes on Twitter
  2. First of all Set N O D E _ E

    N V to "production" It makes e x p r e s s : Cache view templates. Cache CSS files generated from CSS extensions. Generate less verbose error messages.
  3. Gzip Compression Use the c o m p r e

    s s i o n middleware in your e x p r e s s application i m p o r t e x p r e s s f r o m ' e x p r e s s ' ; i m p o r t c o m p r e s s i o n f r o m ' c o m p r e s s i o n ' ; l e t a p p = e x p r e s s ( ) ; i f ( p r o c e s s . e n v . N O D E _ E N V = = = ' p r o d u c t i o n ' ) { a p p . u s e ( c o m p r e s s i o n ( ) ) ; } Note: No need of this if you've enabled gzip compression in your reverse proxy server.
  4. Log Everything Setup a good logging tool such as w

    i n s t o n or b u n y a n . i m p o r t w i n s t o n f r o m ' w i n s t o n ' ; i m p o r t c o n f i g f r o m ' . . / c o n f i g / c o n f i g ' ; c o n s t l o g g e r = n e w ( w i n s t o n . L o g g e r ) ( { t r a n s p o r t s : [ n e w w i n s t o n . t r a n s p o r t s . C o n s o l e ( { c o l o r i z e : t r u e } ) ] } ) ; l o g g e r . l e v e l = c o n f i g . l o g g i n g . l e v e l ; l o g g e r . i n f o ( ' A p p l i c a t i o n e n v i r o n m e n t : ' , p r o c e s s . e n v . N O D E _ E N V ) However, you may want to send your logs to rotating file for production.
  5. Process Managers In production environment you need to run your

    application as a service or a daemon. You may use one of these to accomplish this: p m 2 s t r o n g ­ p m (strongloop) f o r e v e r PM2 is probably the best one till date.
  6. Automatic startup on boot You should setup your application to

    startup automatically when your system starts. You can accomplish this in p m 2 or s t r o n g ­ p m by exporting to init scripts like s y s t e m d or s y s t e m v . $ p m 2 s t a r t u p s y s t e m d $ p m 2 s a v e Or you may use s y s t e m d or other init systems directly if you're comfortable with them.
  7. Daemonize your application If you're a pro DevOps guy then

    you can directly use s y s t e m d or similar tool without a PM even. Remember this? $ s u d o s y s t e m c t l r e s t a r t n g i n x Or, $ s u d o s e r v i c e a p a c h e 2 r e s t a r t You can set up simiar service for your application usign s y s t e m d .
  8. Systemd Unit files All you need to create is a

    systemd unit file somewhat like this: [ U n i t ] D e s c r i p t i o n = t o d o a p p ­ w e b ­ 1 R e q u i r e s = t o d o a p p ­ w e b . t a r g e t [ S e r v i c e ] T y p e = s i m p l e E x e c S t a r t = / u s r / b i n / e n v n o d e d i s t / s e r v e r / i n d e x . j s W o r k i n g D i r e c t o r y = / h o m e / k a b i r / n o d e j s ­ s a m p l e ­ a p p U s e r = t o d o a p p G r o u p = t o d o a p p E n v i r o n m e n t F i l e = ­ / h o m e / k a b i r / n o d e j s ­ s a m p l e ­ a p p / . e n v E n v i r o n m e n t = P O R T = 8 0 9 0 S t a n d a r d I n p u t = n u l l S t a n d a r d O u t p u t = s y s l o g S t a n d a r d E r r o r = s y s l o g R e s t a r t = a l w a y s
  9. Managing your daemon You can use systemctl to manage pglistend.

    # S t a r t t h e s e r v i c e $ s y s t e m c t l s t a r t p g l i s t e n d # S t o p t h e s e r v i c e $ s y s t e m c t l s t o p p g l i s t e n d # C h e c k s e r v i c e s t a t u s $ s y s t e m c t l s t a t u s p g l i s t e n d # E n a b l e t h e s e r v i c e ( T h i s w i l l s t a r t t h e s e r v i c e o n b o o t u p ) $ s y s t e m c t l e n a b l e p g l i s t e n d # D i s a b l e t h e s e r v i c e ( D i s a b l e t h e s e r v i c e t o n o t s t a r t o n b o o $ s y s t e m c t l d i s a b l e p g l i s t e n d
  10. Using n o d e ­ f o r e

    m a n tool You can however use tools like f o r e m a n to export these unit files. Something like this: $ n f e x p o r t You can do some advance exports as well. $ s u d o n f ­ ­ p r o c f i l e = P r o c f i l e . p r o d e x p o r t ­ a t o d o a p p ­ t s y s t e m [ O K A Y ] L o a d e d E N V . e n v F i l e a s K E Y = V A L U E F o r m a t [ W A R N ] R e p l a c i n g : / e t c / s y s t e m d / s y s t e m / t o d o a p p ­ w e b ­ 2 . s e r v i c e [ O K A Y ] W r o t e : / e t c / s y s t e m d / s y s t e m / t o d o a p p ­ w e b ­ 2 . s e r v i c e [ W A R N ] R e p l a c i n g : / e t c / s y s t e m d / s y s t e m / t o d o a p p . t a r g e t [ O K A Y ] W r o t e : / e t c / s y s t e m d / s y s t e m / t o d o a p p . t a r g e t [ W A R N ] R e p l a c i n g : / e t c / s y s t e m d / s y s t e m / t o d o a p p ­ w e b ­ 3 . s e r v i c e [ O K A Y ] W r o t e : / e t c / s y s t e m d / s y s t e m / t o d o a p p ­ w e b ­ 3 . s e r v i c e [ W A R N ] R e p l a c i n g : / e t c / s y s t e m d / s y s t e m / t o d o a p p ­ w e b . t a r g e t [ O K A Y ] W r o t e : / e t c / s y s t e m d / s y s t e m / t o d o a p p ­ w e b . t a r g e t [ W A R N ] R e p l a c i n g : / e t c / s y s t e m d / s y s t e m / t o d o a p p ­ w e b ­ 1 . s e r v i c e [ O K A Y ] W r o t e : / e t c / s y s t e m d / s y s t e m / t o d o a p p ­ w e b ­ 1 . s e r v i c e
  11. s e r v e r 1 2 7 .

    0 . 0 . 1 : 8 0 9 0 ; # N o d e J S S e r v e r 1 s e r v e r 1 2 7 . 0 . 0 . 1 : 8 0 9 1 ; # N o d e J S S e r v e r 2 s e r v e r 1 2 7 . 0 . 0 . 1 : 8 0 9 2 ; # N o d e J S S e r v e r 3 } s e r v e r { l i s t e n 8 0 ; r o o t / h o m e / k a b i r / n o d e j s ­ s a m p l e ­ a p p / p u b l i c ; i n d e x i n d e x . h t m l i n d e x . h t m l ; s e r v e r _ n a m e t e s t . t o d o a p p . l o c a l ; l o c a t i o n / { # F i r s t a t t e m p t t o s e r v e r e q u e s t a s f i l e , t h e n # a s d i r e c t o r y , t h e n f a l l b a c k t o d i s p l a y i n g a 4 0 4 . t r y _ f i l e s $ u r i $ u r i / = 4 0 4 ; } p r o x y _ c o n n e c t _ t i m e o u t 1 3 0 0 ; p r o x y _ s e n d _ t i m e o u t 1 3 0 0 ; p r o x y _ r e a d _ t i m e o u t 1 3 0 0 ; s e n d _ t i m e o u t 1 3 0 0 ; l o c a t i o n / a p i { p r o x y _ p a s s h t t p : / / a p p _ s e r v e r / a p i ;