
Install node.js on VPS
In this article I will tell you what problems will have to be solved in order to get VPS with a really working node.js service. These are all elementary things, but maybe someone else will come in handy.
Problem: the repository may not have a package for the required version of node. Alternatively, you can have two projects requiring different versions of node.
Solution: on development machines in such cases, use nvm. It can also be easily used on a combat server, you just have to write a special script to start your server.
Problem: the application must have superuser privileges in order to read port 80 (for more details see CAP_NET_BIND_SERVICE, man capabilities ). But starting a node with these rights is considered unsafe.
Solutions:
Problem: node is single-threaded and cannot efficiently use multi-core processors.
Solutions:
The issue has been discussed in many places , but there is no consensus. On the one hand, nginx is a reliable, proven solution for years; on the other hand, node itself can do the same.
As a rule, one of those options is offered:
Problem: how to resume the server if it suddenly crashes?
Solutions:
Described here
Node installation
Problem: the repository may not have a package for the required version of node. Alternatively, you can have two projects requiring different versions of node.
Solution: on development machines in such cases, use nvm. It can also be easily used on a combat server, you just have to write a special script to start your server.
Access to port 80
Problem: the application must have superuser privileges in order to read port 80 (for more details see CAP_NET_BIND_SERVICE, man capabilities ). But starting a node with these rights is considered unsafe.
Solutions:
- use a proxy server such as Nginx or HAProxy .
- redirect requests from port 80 to any one with a large number using
iptables
. It is done practically by one command:iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
- start with the same superuser rights, but immediately after connecting to the socket, the process will programmatically lower its privileges. More details here .
Load balancing
Problem: node is single-threaded and cannot efficiently use multi-core processors.
Solutions:
- in fact, everything he can. See cluster API
- and there is also node-http-proxy or fugue .
- You can use Nginx (here it is written as: one , two ). Or HAProxy .
Use node directly or through Nginx?
The issue has been discussed in many places , but there is no consensus. On the one hand, nginx is a reliable, proven solution for years; on the other hand, node itself can do the same.
As a rule, one of those options is offered:
- node deals with everything right up to the distribution of static files.
- node stands for haproxy.
- node stands behind Nginx and is used only for page generation, everything else (static distribution, balancing, https work) is done by Nginx
Restarting a Fallen Process
Problem: how to resume the server if it suddenly crashes?
Solutions:
- If you handle exceptions correctly , there will be practically no crashes.
- Upstart (which you most likely will use to start the service) can restart fallen processes.
- Well, a bunch of solutions actually for monitoring and restarting: forever , monit , supervisord , daemontools .
Service update with git
Described here