How to run node.js app in supervisor behind nginx on Debian Wheezy

By | 2014/08/27

For Linux distros without systemd, supervisor can be used to run your node.js app. I’ll show you how to set this up all behind nginx!


Debian packages needed:

$ sudo apt-get install nginx-extras supervisor

1. First, setup nginx with the proxy_pass directive specifying your node.js app port.

Here is an example nginx file I use for the site lilybin.com which is a node.js app:

/etc/nginx/sites-available/default

server {                                                                                
        listen 80;                                                                      
        listen [::]:80;                                                                 
        server_name lilybin.com www.lilybin.com;                                        
        access_log  /var/log/nginx/access.log;                                          
        root /var/www/lilybin.com;                                                      
                                                                                        
        location / {                                                                    
        proxy_pass http://127.0.0.1:3001;                                               
        proxy_set_header Host $host;                                                    
        proxy_set_header X-Real-IP $remote_addr;                                        
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                    
        proxy_read_timeout 600;                                                         
        include proxy_params;                                                           
    }                                                                                   
                                                                                        
                                                                                        
}  


2. Next, create a supervisor config file for your app. Here is an example where to start the app one would typically type ‘nodejs server.js’. Edit or customize as desired.

/etc/supervisor/conf.d/lilybin.conf

[program:lilybin-nodejs]
directory=/var/www/lilybin.com
command=/usr/bin/nodejs server.js
autostart=true
autorestart=unexpected
user=www-data
startsecs=10
stdout_logfile=/var/www/lilybin.com/log/supervisor.log
redirect_stderr=true


3. Finally, start supervisor and nginx

$ sudo service supervisor start 
$ sudo service nginx start 

Done!

nodejs_app