Answer the question
In order to leave comments, you need to log in
How to organize node.js applications on the server?
Good!
Interested in solving a seemingly simple problem. The prerequisites are as follows:
There is a VPS server with Debian 6. Root shell, everything is as it should be. Default settings (standard init.d, etc.). I want to host a couple of simple applications on node.js on it. I think to organize it like this:
/srv/www/[domain]/application/server.js
i.e. by application per domain (subdomain, etc.)
For example:
/srv/www/mysite.com/application/server.js
/srv/www/test.mysite.com/application/server.js
/srv/www/othersite .com/application/server.js
etc.
Naturally, I would like to make it so that:
1. Applications automatically start when the server is restarted.
2. Applications automatically started after a crash (which is a fairly common event for node.js, as I understand it).
3. It was possible to restart the application manually.
4. Various goodies, such as separate logs per application, etc.
5. Just add new applications. Ideally, generally speaking, something like how sites are added to apache. For example: node_enapp project1 && node_apps reload.
Is there such a thing, or is it still a dream?
BTW: now pretty much the same thing is done by writing buggy daemon wrappers and monit.
Thank you.
Update: Ideally, I would like to put some software and get happiness right away. :) But you can also step-by-step instructions for 5-10 minutes, which will lead to the implementation of the listed 5 points.
Update 2: Looks like supervisor is trending. I'm going to google. Thank you.
Answer the question
In order to leave comments, you need to log in
I'll tell you how I did it. There is a server, inside Linux Containers , in each container there is a separate node.js application. On the server itself, outside the containers, there is nginx, which proxies requests to containers.
Next, inside the nvm container with node.js and supervisor . Node.js runs under nobody inside the container.
Pieces of how it's all set up can be found here express(node.js) + capistrano + supervisord , here Install LXC (Linux Containers) and here Install hubot on ubuntu . The records are a year old, something could have changed a little.
Creating a new application is as simple as possible, I have a sample container in advance, which already has both supervisor and node.js in nvm. I just do lxc-clone to the container and prescribe ip with mac address.
Items 1-4 were decided by using upstart.
5 point - solved by a simple script (bash, python)
1. You can conveniently manage autorun, restart with the help of supervisor
2. Watchdog serves just for this
3. Supervisor can do just that
4. Supervisor can do just that
5. I think you can write a non-buggy wrapper here
I have a similar situation, only the sites are located in this way: /var/www/[name-site.ru]/.index.js.
To prevent the application from crashing in .index.js added:
process.on('uncaughtException', function (err) { console.log('Caught exception: ' + err); });
Sites are automatically started at system boot using a self-written script - /etc/init.d/nodejs.
I have forever(sudo npm forever -g) for 2,3,4, autostart is registered in /etc/init.d/local with the
lines
forever start app1/server.js
forever start app2/server.js
Log4js is better for logging - nothing special you don’t even need to do it, he knows how to redirect the console.
Although, if you use it from the very beginning, then of course it is better to call special methods (info, error, etc.).
5. write with your hands.
To monitor the health of the application, it is desirable to have an additional criterion, and not just running / not running. For example, periodically make him a request, and wait for a response. Restart on timeout.
Not supervisors are united - there is also Runit, I use it. There, in general, write a couple of lines on the bush and that's it. In general, node.js has a cluster that can do all this.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question